예제 #1
0
    def test_readInputLines(self):
        def application(environ, start_response):
            input = environ['wsgi.input']
            out = 'X'.join(input.readlines())
            start_response("200 OK", {})
            return [out]
        
        d = self.assertResponse(
            (WSGI(application), 'http://host/', {}, None, None, '', "a\nb\nc"),
            (200, {"Content-Length": 7}, "a\nXb\nXc"))

        d.addCallback(lambda d: self.assertResponse(
            (WSGI(application), 'http://host/', {}, None, None, '', "a\nb\n"),
            (200, {"Content-Length": 5}, "a\nXb\n")))
        return d
예제 #2
0
 def assertEnv(self, uri, env, version=None):
     keys = env.keys()
     keys.sort()
     envstring = ''.join(['%s=%r;' % (k, v) for k, v in env.items()])
     self.assertResponse(
         (WSGI(self.envApp(*keys)), uri, None, None, version),
         (200, {}, envstring))
예제 #3
0
 def test_didntCallStartResponse(self):
     def application(environ, start_response):
         return ["Foo"]
     
     return self.assertResponse(
         (WSGI(application), 'http://host/'),
         (500, {}, None)).addBoth(self.flushErrors, RuntimeError)
예제 #4
0
 def test_errorfulResource(self):
     def application(environ, start_response):
         raise TestError("This is an expected error")
     
     return self.assertResponse(
         (WSGI(application), 'http://host/'),
         (500, {}, None)).addBoth(self.flushErrors, TestError)
예제 #5
0
 def test_returnList(self):
     def application(environ, start_response):
         write = start_response("200 OK", {})
         return ["Foo", "Bar"]
     
     return self.assertResponse(
         (WSGI(application), 'http://host/'),
         (200, {"Content-Length": 6}, "FooBar"))
예제 #6
0
 def test_calledStartResponseLate(self):
     def application(environ, start_response):
         start_response("200 OK", {})
         yield "Foo"
     
     return self.assertResponse(
         (WSGI(application), 'http://host/'),
         (200, {"Content-Length": None}, "Foo"))
예제 #7
0
 def test_errorfulResource2(self):
     def application(environ, start_response):
         write = start_response("200 OK", {})
         write("Foo")
         raise TestError("This is an expected error")
     
     return self.assertResponse(
         (WSGI(application), 'http://host/'),
         (200, {"Content-Length": None}, "Foo"), failure=True
         ).addBoth(self.flushErrors, TestError)
예제 #8
0
 def test_readAllInput(self):
     def application(environ, start_response):
         input = environ['wsgi.input']
         out = input.read(-1)
         start_response("200 OK", {})
         return [out]
     
     return self.assertResponse(
         (WSGI(application), 'http://host/', {}, None, None, '', "This is some content"),
         (200, {"Content-Length": 20}, "This is some content"))
예제 #9
0
    def test_responseCode(self):
        """Test that WSGIResource handles strange response codes properly."""
        def application(environ, start_response):
            status = '314'
            response_headers = [('Content-type','text/html')]
            writer = start_response(status, response_headers)
            return []

        return self.assertResponse(
            (WSGI(application), 'http://host/'),
            (314, {"Content-Length": 0}, ''))
예제 #10
0
 def assertEnv(self, uri, env, version=None, prepath=''):
     """
     Check the value of the rendering envirnment against
     the string returned by the testing WSGIApp.
     """
     keys = env.keys()
     keys.sort()
     envstring = ''.join(['%s=%r;' % (k, v) for k, v in env.items()])
     return self.assertResponse(
         (WSGI(self.envApp(*keys)), uri, None, None, version, prepath),
         (200, {}, envstring))
예제 #11
0
    def test_getContainedResource(self):
        """Test that non-blocking WSGI applications render properly."""
        def application(environ, start_response):
            status = '200 OK'
            response_headers = [('Content-type','text/html')]
            writer = start_response(status, response_headers)
            writer('<html>')
            return ['<h1>Some HTML</h1>',
                    '</html>']

        return self.assertResponse(
            (WSGI(application), 'http://host/'),
            (200, {"Content-Length": None}, '<html><h1>Some HTML</h1></html>'))
예제 #12
0
 def test_readiter(self):
     """Test that using wsgi.input as an iterator works."""
     def application(environ, start_response):
         input = environ['wsgi.input']
         out = 'X'.join(input)
         
         start_response("200 OK", {})
         return [out]
     
     return self.assertResponse(
         (WSGI(application), 'http://host/', {}, None, None, '',
          "Line blah blah\nOh Line\n"),
         (200, {"Content-Length": 24}, "Line blah blah\nXOh Line\n"))
예제 #13
0
 def test_errorfulIterator2(self):
     def iterator():
         yield "Foo"
         yield "Bar"
         raise TestError("This is also expected")
     
     def application(environ, start_response):
         start_response("200 OK", {})
         return iterator()
     
     return self.assertResponse(
         (WSGI(application), 'http://host/'),
         (200, {"Content-Length": None}, "FooBar"), failure=True
         ).addBoth(self.flushErrors, TestError)
예제 #14
0
 def test_readInputMixed(self):
     def application(environ, start_response):
         input = environ['wsgi.input']
         out = [input.read(5)]
         out.extend(["X", input.readline()])
         out.extend(["X", input.read(1)])
         out.extend(["X", input.readline()])
         
         start_response("200 OK", {})
         return out
     
     return self.assertResponse(
         (WSGI(application), 'http://host/', {}, None, None, '',
          "Line blah blah\nOh Line\n"),
         (200, {"Content-Length": 26}, "Line Xblah blah\nXOXh Line\n"))
예제 #15
0
    def test_getBlockingResource(self):
        """Test that blocking WSGI applications render properly."""
        def application(environ, start_response):
            """Simplest possible application object"""
            status = '200 OK'
            response_headers = [('Content-type','text/html')]
            writer = start_response(status, response_headers)
            writer('<h1>A little bit')
            time.sleep(1)
            writer(' of HTML</h1>')
            time.sleep(1)
            return ['<p>Hello!</p>']

        return self.assertResponse(
            (WSGI(application), 'http://host/'),
            (200, {"Content-Length": None}, '<h1>A little bit of HTML</h1><p>Hello!</p>'))
예제 #16
0
    def test_readInputLineSizeNegZero(self):
        """Test that calling wsgi.input.readline works with -1 and 0 and none."""
        def application(environ, start_response):
            input = environ['wsgi.input']

            out = [input.read(5)] # 'Line '
            out.extend(["X", input.readline(-1)]) # 'blah blah\n'
            out.extend(["X", input.readline(0)])  # ''
            out.extend(["X", input.readline(None)]) # 'Oh Line\n'
            out.extend(["X", input.readline()])   # ''

            start_response("200 OK", {})
            return out

        return self.assertResponse(
            (WSGI(application), 'http://host/', {}, None, None, '',
             "Line blah blah\nOh Line\n"),
            (200, {"Content-Length": 27},
             "Line Xblah blah\nXXOh Line\nX"))
예제 #17
0
    def test_readInputLineSize(self):
        """Test that readline() with a size works."""
        def application(environ, start_response):
            input = environ['wsgi.input']

            out = [input.read(5)]           # 'Line '
            out.extend(["X", input.readline(5)]) # 'blah '
            out.extend(["X", input.readline()])  # 'blah\n'
            out.extend(["X", input.readline(1)])     # 'O'
            out.extend(["X", input.readline()])  # 'h Line\n'

            start_response("200 OK", {})
            return out

        return self.assertResponse(
            (WSGI(application), 'http://host/', {}, None, None, '',
             "Line blah blah\nOh Line\n"),
            (200, {"Content-Length": 27},
             "Line Xblah Xblah\nXOXh Line\n"))