Exemplo n.º 1
0
    def test_trans_id_header_fail(self):
        self.assertIsNone(self.logger.txn_id)

        app = catch_errors.CatchErrorMiddleware(FakeApp(True), {})
        req = Request.blank('/v1/a/c/o')
        app(req.environ, self.start_response)
        self.assertEqual(len(self.logger.txn_id), 34)
Exemplo n.º 2
0
    def test_HEAD_with_content_length(self):
        def cannot_count_app(env, sr):
            sr("200 OK", [("Content-Length", "10")])
            return [b""]

        app = catch_errors.CatchErrorMiddleware(cannot_count_app, {})
        list(app({'REQUEST_METHOD': 'HEAD'}, self.start_response))
Exemplo n.º 3
0
    def test_multiple_content_lengths(self):
        def poly_cl_app(env, sr):
            sr("200 OK", [("Content-Length", "30"), ("Content-Length", "40")])
            return [b"The head of our personal trainers is Jim Shortz"]

        app = catch_errors.CatchErrorMiddleware(poly_cl_app, {})
        list(app({'REQUEST_METHOD': 'GET'}, self.start_response))
Exemplo n.º 4
0
    def test_bogus_content_length(self):
        def bogus_cl_app(env, sr):
            sr("200 OK", [("Content-Length", "25 cm")])
            return [b"our British cutlery specialist is Sir Irving Spoon"]

        app = catch_errors.CatchErrorMiddleware(bogus_cl_app, {})
        list(app({'REQUEST_METHOD': 'GET'}, self.start_response))
Exemplo n.º 5
0
    def test_no_content_length(self):
        def no_cl_app(env, sr):
            sr("200 OK", [("Content-Type", "application/names")])
            return [b"our staff statistician is Marge Inovera"]

        app = catch_errors.CatchErrorMiddleware(no_cl_app, {})
        list(app({'REQUEST_METHOD': 'GET'}, self.start_response))
Exemplo n.º 6
0
    def test_trans_id_header_suffix(self):
        self.assertIsNone(self.logger.txn_id)

        app = catch_errors.CatchErrorMiddleware(
            FakeApp(), {'trans_id_suffix': '-stuff'})
        req = Request.blank('/v1/a/c/o')
        app(req.environ, self.start_response)
        self.assertTrue(self.logger.txn_id.endswith('-stuff'))
Exemplo n.º 7
0
 def test_trans_id_header_fail(self):
     self.assertEquals(self.logger.txn_id, None)
     def start_response(status, headers):
         self.assert_('x-trans-id' in (x[0] for x in headers))
     app = catch_errors.CatchErrorMiddleware(FakeApp(True), {})
     req = Request.blank('/v1/a/c/o')
     app(req.environ, start_response)
     self.assertEquals(len(self.logger.txn_id), 34)
Exemplo n.º 8
0
    def test_trans_id_header_extra_quoted(self):
        self.assertIsNone(self.logger.txn_id)

        app = catch_errors.CatchErrorMiddleware(FakeApp(), {})
        req = Request.blank('/v1/a/c/o',
                            headers={'X-Trans-Id-Extra': 'xan than"gum'})
        app(req.environ, self.start_response)
        self.assertTrue(self.logger.txn_id.endswith('-xan%20than%22gum'))
Exemplo n.º 9
0
    def test_long_response_body(self):
        def cannot_count_app(env, sr):
            sr("200 OK", [("Content-Length", "10")])
            return [b"our optometric firm is C.F. Eye Care"]

        app = catch_errors.CatchErrorMiddleware(cannot_count_app, {})

        with self.assertRaises(catch_errors.BadResponseLength):
            list(app({'REQUEST_METHOD': 'GET'}, self.start_response))
Exemplo n.º 10
0
    def test_trans_id_header_pass(self):
        self.assertEquals(self.logger.txn_id, None)

        def start_response(status, headers, exc_info=None):
            self.assertTrue('X-Trans-Id' in (x[0] for x in headers))
        app = catch_errors.CatchErrorMiddleware(FakeApp(), {})
        req = Request.blank('/v1/a/c/o')
        app(req.environ, start_response)
        self.assertEquals(len(self.logger.txn_id), 34)  # 32 hex + 'tx'
Exemplo n.º 11
0
    def test_short_response_body(self):
        def cannot_count_app(env, sr):
            sr("200 OK", [("Content-Length", "2000")])
            return [b"our staff tailor is Euripedes Imenedes"]

        app = catch_errors.CatchErrorMiddleware(cannot_count_app, {})

        with self.assertRaises(catch_errors.BadResponseLength):
            list(app({'REQUEST_METHOD': 'GET'}, self.start_response))
Exemplo n.º 12
0
    def test_trans_id_header_extra(self):
        self.assertIsNone(self.logger.txn_id)

        app = catch_errors.CatchErrorMiddleware(
            FakeApp(), {'trans_id_suffix': '-fromconf'})
        req = Request.blank('/v1/a/c/o',
                            headers={'X-Trans-Id-Extra': 'fromuser'})
        app(req.environ, self.start_response)
        self.assertTrue(self.logger.txn_id.endswith('-fromconf-fromuser'))
Exemplo n.º 13
0
    def test_trans_id_header_suffix(self):
        self.assertEquals(self.logger.txn_id, None)

        def start_response(status, headers, exc_info=None):
            self.assertTrue('X-Trans-Id' in (x[0] for x in headers))
        app = catch_errors.CatchErrorMiddleware(
            FakeApp(), {'trans_id_suffix': '-stuff'})
        req = Request.blank('/v1/a/c/o')
        app(req.environ, start_response)
        self.assertTrue(self.logger.txn_id.endswith('-stuff'))
Exemplo n.º 14
0
    def test_trans_id_header_extra_quoted(self):
        self.assertEquals(self.logger.txn_id, None)

        def start_response(status, headers, exc_info=None):
            self.assertTrue('X-Trans-Id' in (x[0] for x in headers))
        app = catch_errors.CatchErrorMiddleware(FakeApp(), {})
        req = Request.blank('/v1/a/c/o',
                            headers={'X-Trans-Id-Extra': 'xan than"gum'})
        app(req.environ, start_response)
        self.assertTrue(self.logger.txn_id.endswith('-xan%20than%22gum'))
Exemplo n.º 15
0
    def test_trans_id_header_extra_length_limit(self):
        self.assertEqual(self.logger.txn_id, None)

        app = catch_errors.CatchErrorMiddleware(
            FakeApp(), {'trans_id_suffix': '-fromconf'})
        req = Request.blank('/v1/a/c/o',
                            headers={'X-Trans-Id-Extra': 'a' * 1000})
        app(req.environ, self.start_response)
        self.assertTrue(
            self.logger.txn_id.endswith('-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'))
Exemplo n.º 16
0
 def test_catcherrors(self):
     app = catch_errors.CatchErrorMiddleware(FakeApp(True), {})
     req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
     resp = app(req.environ, start_response)
     self.assertEquals(resp, ['An error occurred'])
Exemplo n.º 17
0
 def test_catcherrors_passthrough(self):
     app = catch_errors.CatchErrorMiddleware(FakeApp(), {})
     req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
     resp = app(req.environ, start_response)
     self.assertEquals(resp, ['FAKE APP'])
Exemplo n.º 18
0
 def test_error_in_iterator(self):
     app = catch_errors.CatchErrorMiddleware(
         FakeApp(body_iter=(int(x) for x in 'abcd')), {})
     req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
     resp = app(req.environ, start_response)
     self.assertEqual(list(resp), ['An error occurred'])
Exemplo n.º 19
0
 def test_catcherrors_with_unexpected_error(self):
     app = catch_errors.CatchErrorMiddleware(FakeApp(error='strange'), {})
     req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
     resp = app(req.environ, start_response)
     self.assertEqual(list(resp), ['An error occurred'])