예제 #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)
예제 #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))
예제 #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))
예제 #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))
예제 #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))
예제 #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'))
예제 #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)
예제 #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'))
예제 #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))
예제 #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'
예제 #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))
예제 #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'))
예제 #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'))
예제 #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'))
예제 #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'))
예제 #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'])
예제 #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'])
예제 #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'])
예제 #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'])