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)
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))
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))
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))
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))
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'))
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)
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'))
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))
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'
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))
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'))
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'))
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'))
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'))
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'])
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'])
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'])
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'])