20
20
from swift.common.utils import get_logger
22
22
class FakeApp(object):
23
def __init__(self, error=False):
23
def __init__(self, error=False, body_iter=None):
25
self.body_iter = body_iter
26
27
def __call__(self, env, start_response):
27
28
if 'swift.trans_id' not in env:
28
29
raise Exception('Trans id should always be in env')
30
31
raise Exception('An error occurred')
32
if self.body_iter is None:
33
37
def start_response(*args):
43
47
app = catch_errors.CatchErrorMiddleware(FakeApp(), {})
44
48
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
45
49
resp = app(req.environ, start_response)
46
self.assertEquals(resp, ['FAKE APP'])
50
self.assertEquals(list(resp), ['FAKE APP'])
48
52
def test_catcherrors(self):
49
53
app = catch_errors.CatchErrorMiddleware(FakeApp(True), {})
50
54
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
51
55
resp = app(req.environ, start_response)
52
self.assertEquals(resp, ['An error occurred'])
56
self.assertEquals(list(resp), ['An error occurred'])
54
58
def test_trans_id_header_pass(self):
55
59
self.assertEquals(self.logger.txn_id, None)
56
def start_response(status, headers):
61
def start_response(status, headers, exc_info=None):
57
62
self.assert_('x-trans-id' in (x[0] for x in headers))
58
63
app = catch_errors.CatchErrorMiddleware(FakeApp(), {})
59
64
req = Request.blank('/v1/a/c/o')
63
68
def test_trans_id_header_fail(self):
64
69
self.assertEquals(self.logger.txn_id, None)
65
def start_response(status, headers):
71
def start_response(status, headers, exc_info=None):
66
72
self.assert_('x-trans-id' in (x[0] for x in headers))
67
73
app = catch_errors.CatchErrorMiddleware(FakeApp(True), {})
68
74
req = Request.blank('/v1/a/c/o')
69
75
app(req.environ, start_response)
70
76
self.assertEquals(len(self.logger.txn_id), 34)
78
def test_error_in_iterator(self):
79
app = catch_errors.CatchErrorMiddleware(
80
FakeApp(body_iter=(int(x) for x in 'abcd')), {})
81
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
82
resp = app(req.environ, start_response)
83
self.assertEquals(list(resp), ['An error occurred'])
72
85
if __name__ == '__main__':