Exemplo n.º 1
0
    def test_if_unmodified_since_precondition_fail(self):
        r = Request.build("/resource", method='PUT')
        r.if_unmodified_since = datetime.datetime(year=2012, month=7, day=1)
        r.body = b'put this here'
        resp = r.get_response(self.app)

        self.assertEqual(resp.status_int, 412)
Exemplo n.º 2
0
    def test_if_unmodified_since_precondition_fail(self):
        r = Request.build("/resource", method='PUT')
        r.if_unmodified_since = datetime.datetime(year=2012, month=7, day=1)
        r.body = b'put this here'
        resp = r.get_response(self.app)

        self.assertEqual(resp.status_int, 412)
Exemplo n.º 3
0
    def test_redirect_back_success(self):
        r = Request.build("/redirect_back",
                          headers={'Referer': b'http://www.google.com'})
        resp = r.get_response(self.app)

        assert 300 <= resp.status_int <= 399
        self.assertEqual(resp.headers['Location'], b'http://www.google.com')
Exemplo n.º 4
0
    def test_redirect_with_non_get(self):
        req = Request.build("/upload", method='POST')
        req.http_version = "HTTP/1.1"
        resp = req.get_response(self.app)

        self.assertEqual(resp.status_int, 303)
        self.assertTrue(resp.headers['Location'].endswith(b'/uploaded'))
Exemplo n.º 5
0
    def test_redirect_with_non_get(self):
        req = Request.build("/upload", method='POST')
        req.http_version = "HTTP/1.1"
        resp = req.get_response(self.app)

        self.assertEqual(resp.status_int, 303)
        self.assertTrue(resp.headers['Location'].endswith(b'/uploaded'))
Exemplo n.º 6
0
 def assert_halts_with(self, code, body, path):
     """Helper function to set the halt value and assert"""
     self.halt_code = code
     self.halt_body = body
     req = Request.build(path)
     resp = req.get_response(self.app)
     self.assertEqual(resp.status_int, 200)
     self.assertEqual(resp.body, body)
Exemplo n.º 7
0
    def test_HEAD_fallback(self):
        r = Request.build('/')
        r.method = "HEAD"
        resp = r.get_response(self.app)

        self.assertEqual(resp.status_int, 200)
        self.assertEqual(len(resp.body), 0)
        self.assertEqual(resp.headers['X-Custom-Header'], b'foobar')
Exemplo n.º 8
0
 def assert_halts_with(self, code, body, path):
     """Helper function to set the halt value and assert"""
     self.halt_code = code
     self.halt_body = body
     req = Request.build(path)
     resp = req.get_response(self.app)
     self.assertEqual(resp.status_int, 200)
     self.assertEqual(resp.body, body)
Exemplo n.º 9
0
    def test_g(self):
        app = HobokenApplication('')

        @app.get("/one")
        def one():
            app.g.foo = 'bar'

        @app.get("/two")
        def two():
            self.assertNotIn('foo', app.g)

        r = Request.build("/one")
        resp = r.get_response(app)

        self.assertNotIn('foo', app.g.__dict__)

        r = Request.build("/two")
        resp = r.get_response(app)
Exemplo n.º 10
0
    def test_redirect_code(self):
        for code in [301, 302, 303]:
            self.redirect_code = code

            req = Request.build("/redirect")
            resp = req.get_response(self.app)

            self.assertEqual(resp.status_int, code)
            self.assertTrue(resp.headers['Location'].endswith(b'/foo'))
Exemplo n.º 11
0
    def test_g(self):
        app = HobokenApplication('')

        @app.get("/one")
        def one():
            app.g.foo = 'bar'

        @app.get("/two")
        def two():
            self.assertNotIn('foo', app.g)

        r = Request.build("/one")
        resp = r.get_response(app)

        self.assertNotIn('foo', app.g.__dict__)

        r = Request.build("/two")
        resp = r.get_response(app)
Exemplo n.º 12
0
    def test_with_request_lock(self):
        self.app.config['SERIALIZE_REQUESTS'] = True

        @self.app.get('/')
        def idx():
            return b'foo'

        @self.app.get('/errors')
        def err():
            raise RuntimeError('foobar')

        r = Request.build('/')
        e = r.get_response(self.app)
        self.assertEqual(e.status_int, 200)

        r = Request.build('/errors')
        e = r.get_response(self.app)
        self.assertEqual(e.status_int, 500)
Exemplo n.º 13
0
    def test_with_request_lock(self):
        self.app.config['SERIALIZE_REQUESTS'] = True

        @self.app.get('/')
        def idx():
            return b'foo'

        @self.app.get('/errors')
        def err():
            raise RuntimeError('foobar')

        r = Request.build('/')
        e = r.get_response(self.app)
        self.assertEqual(e.status_int, 200)

        r = Request.build('/errors')
        e = r.get_response(self.app)
        self.assertEqual(e.status_int, 500)
Exemplo n.º 14
0
    def test_redirect_code(self):
        for code in [301, 302, 303]:
            self.redirect_code = code

            req = Request.build("/redirect")
            resp = req.get_response(self.app)

            self.assertEqual(resp.status_int, code)
            self.assertTrue(resp.headers['Location'].endswith(b'/foo'))
Exemplo n.º 15
0
    def test_invalid_method(self):
        @self.app.get("/")
        def index():
            return b'foo'

        r = Request.build("/")
        r.method = 'OTHER'
        resp = r.get_response(self.app)

        self.assertEqual(resp.status_int, 405)
Exemplo n.º 16
0
    def test_invalid_method(self):
        @self.app.get("/")
        def index():
            return b'foo'

        r = Request.build("/")
        r.method = 'OTHER'
        resp = r.get_response(self.app)

        self.assertEqual(resp.status_int, 405)
Exemplo n.º 17
0
    def test_will_handle_regex_named_captures_4(self):
        r = re.compile(b"/(?P<first>.*?)foo(.*?)bar")

        @self.app.get(r)
        def regex_get_params(arg, first=None):
            self.assertEqual(first, b'ONE')
            self.assertEqual(arg, b'TWO')
            return b'param'

        r = Request.build("/ONEfooTWObar")
        resp = r.get_response(self.app)
        self.assertEqual(resp.status_int, 200)
        self.assertEqual(resp.body, b'param')
Exemplo n.º 18
0
    def test_will_handle_regex_named_captures_4(self):
        r = re.compile(b"/(?P<first>.*?)foo(.*?)bar")

        @self.app.get(r)
        def regex_get_params(arg, first=None):
            self.assertEqual(first, b'ONE')
            self.assertEqual(arg, b'TWO')
            return b'param'

        r = Request.build("/ONEfooTWObar")
        resp = r.get_response(self.app)
        self.assertEqual(resp.status_int, 200)
        self.assertEqual(resp.body, b'param')
Exemplo n.º 19
0
    def test_will_handle_custom_matcher(self):
        m = mock.MagicMock()
        m.match.return_value = (True, ['arg'], {'val': 'kwarg'})

        @self.app.get(m)
        def custom_get(arg, val=None):
            self.assertEqual(arg, 'arg')
            self.assertEqual(val, 'kwarg')
            return b'body'

        r = Request.build("/")
        resp = r.get_response(self.app)
        self.assertEqual(resp.status_int, 200)
        self.assertEqual(m.match.call_count, 1)
Exemplo n.º 20
0
    def test_will_handle_custom_matcher(self):
        m = mock.MagicMock()
        m.match.return_value = (True, ['arg'], {'val': 'kwarg'})

        @self.app.get(m)
        def custom_get(arg, val=None):
            self.assertEqual(arg, 'arg')
            self.assertEqual(val, 'kwarg')
            return b'body'

        r = Request.build("/")
        resp = r.get_response(self.app)
        self.assertEqual(resp.status_int, 200)
        self.assertEqual(m.match.call_count, 1)
Exemplo n.º 21
0
    def call_app(self, path='/', method='GET', user_agent=None, host=None, accepts=None):
        """
        This function calls our application, and returns a tuple of (status, body)
        """
        req = Request.build(path)
        req.method = method
        if user_agent is not None:
            req.headers['User-Agent'] = user_agent

        if host is not None:
            req.headers['Host'] = host

        if accepts is not None:
            req.headers['Accept'] = accepts

        resp = req.get_response(self.app)
        return resp.status_int, resp.text
Exemplo n.º 22
0
 def test_bytes(self):
     req = Request.build('/bytes')
     resp = req.get_response(self.app)
     self.assertEqual(resp.body, b'byte string')
Exemplo n.º 23
0
 def test_bytes(self):
     req = Request.build('/string')
     resp = req.get_response(self.app)
     self.assertEqual(resp.text, b'f\xc3\xb8\xc3\xb8'.decode('utf-8'))
Exemplo n.º 24
0
    def test_redirect_back_failure(self):
        r = Request.build("/redirect_fail", headers={'Referer': b''})
        resp = r.get_response(self.app)

        self.assertEqual(resp.status_int, 200)
        self.assertEqual(resp.body, b'no redirect')
Exemplo n.º 25
0
 def requestor_func():
     for i in range(0, num_requests // num_threads):
         resp = Request.build(path).get_response(app)
         responses.append(resp.body)
Exemplo n.º 26
0
 def test_no_last_modified_if_etag(self):
     r = Request.build("/resource")
     r.headers['If-None-Match'] = 'foobar'
     resp = r.get_response(self.app)
     self.assertIsNotNone(resp.last_modified)
Exemplo n.º 27
0
 def test_has_last_modified(self):
     r = Request.build("/resource")
     resp = r.get_response(self.app)
     self.assertIsNotNone(resp.last_modified)
Exemplo n.º 28
0
 def test_no_last_modified(self):
     r = Request.build("/bad")
     resp = r.get_response(self.app)
     self.assertIsNone(resp.last_modified)
Exemplo n.º 29
0
 def test_will_return_200_for_newer(self):
     r = Request.build("/resource")
     r.if_modified_since = datetime.datetime(year=2012, month=7, day=1)
     resp = r.get_response(self.app)
     self.assertEqual(resp.status_int, 200)
     self.assertEqual(resp.body, b'resource value')
Exemplo n.º 30
0
    def test_redirect_to(self):
        r = Request.build("/redirect_to")
        resp = r.get_response(self.app)

        assert 300 <= resp.status_int <= 399
        self.assertTrue(resp.headers['Location'].endswith(b'/to_me'))
Exemplo n.º 31
0
 def test_has_last_modified(self):
     r = Request.build("/resource")
     resp = r.get_response(self.app)
     self.assertIsNotNone(resp.last_modified)
Exemplo n.º 32
0
    def test_redirect_back_failure(self):
        r = Request.build("/redirect_fail", headers={'Referer': b''})
        resp = r.get_response(self.app)

        self.assertEqual(resp.status_int, 200)
        self.assertEqual(resp.body, b'no redirect')
Exemplo n.º 33
0
    def test_redirect_back_success(self):
        r = Request.build("/redirect_back", headers={'Referer': b'http://www.google.com'})
        resp = r.get_response(self.app)

        assert 300 <= resp.status_int <= 399
        self.assertEqual(resp.headers['Location'], b'http://www.google.com')
Exemplo n.º 34
0
 def test_bytes(self):
     req = Request.build('/string')
     resp = req.get_response(self.app)
     self.assertEqual(resp.text, b'f\xc3\xb8\xc3\xb8'.decode('utf-8'))
Exemplo n.º 35
0
 def call_app(self, path, *args, **kwargs):
     req = Request.build(path, *args, **kwargs)
     resp = req.get_response(self.app)
     return resp
Exemplo n.º 36
0
 def test_no_last_modified_if_etag(self):
     r = Request.build("/resource")
     r.headers['If-None-Match'] = 'foobar'
     resp = r.get_response(self.app)
     self.assertIsNotNone(resp.last_modified)
Exemplo n.º 37
0
    def test_redirect(self):
        req = Request.build("/upload", method='POST')
        resp = req.get_response(self.app)

        self.assertEqual(resp.status_int, 302)
        self.assertTrue(resp.headers['Location'].endswith(b'/uploaded'))
Exemplo n.º 38
0
 def test_will_return_304_for_get(self):
     r = Request.build("/resource")
     r.if_modified_since = datetime.datetime(year=2012, month=7, day=20)
     resp = r.get_response(self.app)
     self.assertEqual(resp.status_int, 304)
     self.assertEqual(resp.body, b'')
Exemplo n.º 39
0
 def test_will_return_304_for_get(self):
     r = Request.build("/resource")
     r.if_modified_since = datetime.datetime(year=2012, month=7, day=20)
     resp = r.get_response(self.app)
     self.assertEqual(resp.status_int, 304)
     self.assertEqual(resp.body, b'')
Exemplo n.º 40
0
 def test_will_return_200_for_newer(self):
     r = Request.build("/resource")
     r.if_modified_since = datetime.datetime(year=2012, month=7, day=1)
     resp = r.get_response(self.app)
     self.assertEqual(resp.status_int, 200)
     self.assertEqual(resp.body, b'resource value')
Exemplo n.º 41
0
 def test_no_last_modified(self):
     r = Request.build("/bad")
     resp = r.get_response(self.app)
     self.assertIsNone(resp.last_modified)
Exemplo n.º 42
0
 def test_bytes(self):
     req = Request.build('/bytes')
     resp = req.get_response(self.app)
     self.assertEqual(resp.body, b'byte string')
Exemplo n.º 43
0
    def test_redirect_to(self):
        r = Request.build("/redirect_to")
        resp = r.get_response(self.app)

        assert 300 <= resp.status_int <= 399
        self.assertTrue(resp.headers['Location'].endswith(b'/to_me'))
Exemplo n.º 44
0
 def call_app(self, path, *args, **kwargs):
     req = Request.build(path, *args, **kwargs)
     resp = req.get_response(self.app)
     return resp
Exemplo n.º 45
0
 def requestor_func():
     for i in range(0, num_requests // num_threads):
         resp = Request.build(path).get_response(app)
         responses.append(resp.body)
Exemplo n.º 46
0
    def test_redirect(self):
        req = Request.build("/upload", method='POST')
        resp = req.get_response(self.app)

        self.assertEqual(resp.status_int, 302)
        self.assertTrue(resp.headers['Location'].endswith(b'/uploaded'))