|
15 | 15 |
|
16 | 16 | import unittest
|
17 | 17 |
|
18 |
| -from webob import Request |
| 18 | +from webob import Request, Response |
19 | 19 |
|
20 | 20 | from swift.common.middleware import catch_errors
|
| 21 | +from swift.common.utils import get_logger |
21 | 22 |
|
22 | 23 | class FakeApp(object):
|
23 | 24 | def __init__(self, error=False):
|
24 | 25 | self.error = error
|
25 | 26 |
|
26 | 27 | def __call__(self, env, start_response):
|
| 28 | + if 'swift.trans_id' not in env: |
| 29 | + raise Exception('Trans id should always be in env') |
27 | 30 | if self.error:
|
28 |
| - raise Exception('augh!') |
29 |
| - return "FAKE APP" |
| 31 | + raise Exception('An error occurred') |
| 32 | + return ["FAKE APP"] |
30 | 33 |
|
31 | 34 | def start_response(*args):
|
32 | 35 | pass
|
33 | 36 |
|
34 | 37 | class TestCatchErrors(unittest.TestCase):
|
35 | 38 |
|
| 39 | + def setUp(self): |
| 40 | + self.logger = get_logger({}) |
| 41 | + self.logger.txn_id = None |
| 42 | + |
36 | 43 | def test_catcherrors_passthrough(self):
|
37 | 44 | app = catch_errors.CatchErrorMiddleware(FakeApp(), {})
|
38 | 45 | req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
|
39 | 46 | resp = app(req.environ, start_response)
|
40 |
| - self.assertEquals(resp, 'FAKE APP') |
| 47 | + self.assertEquals(resp, ['FAKE APP']) |
41 | 48 |
|
42 | 49 | def test_catcherrors(self):
|
43 | 50 | app = catch_errors.CatchErrorMiddleware(FakeApp(True), {})
|
44 | 51 | req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
|
45 | 52 | resp = app(req.environ, start_response)
|
46 | 53 | self.assertEquals(resp, ['An error occurred'])
|
47 | 54 |
|
48 |
| - def test_trans_id_header(self): |
49 |
| - |
| 55 | + def test_trans_id_header_pass(self): |
| 56 | + self.assertEquals(self.logger.txn_id, None) |
50 | 57 | def start_response(status, headers):
|
51 | 58 | self.assert_('x-trans-id' in (x[0] for x in headers))
|
52 | 59 | app = catch_errors.CatchErrorMiddleware(FakeApp(), {})
|
53 |
| - req = Request.blank('/v1/a') |
54 |
| - app(req.environ, start_response) |
55 |
| - app = catch_errors.CatchErrorMiddleware(FakeApp(), {}) |
56 |
| - req = Request.blank('/v1/a/c') |
57 |
| - app(req.environ, start_response) |
58 |
| - app = catch_errors.CatchErrorMiddleware(FakeApp(), {}) |
59 | 60 | req = Request.blank('/v1/a/c/o')
|
60 | 61 | app(req.environ, start_response)
|
61 |
| - app = catch_errors.CatchErrorMiddleware(FakeApp(True), {}) |
62 |
| - req = Request.blank('/v1/a') |
63 |
| - app(req.environ, start_response) |
64 |
| - app = catch_errors.CatchErrorMiddleware(FakeApp(True), {}) |
65 |
| - req = Request.blank('/v1/a/c') |
66 |
| - app(req.environ, start_response) |
| 62 | + self.assertEquals(len(self.logger.txn_id), 34) # 32 hex + 'tx' |
| 63 | + |
| 64 | + def test_trans_id_header_fail(self): |
| 65 | + self.assertEquals(self.logger.txn_id, None) |
| 66 | + def start_response(status, headers): |
| 67 | + self.assert_('x-trans-id' in (x[0] for x in headers)) |
67 | 68 | app = catch_errors.CatchErrorMiddleware(FakeApp(True), {})
|
68 | 69 | req = Request.blank('/v1/a/c/o')
|
69 | 70 | app(req.environ, start_response)
|
| 71 | + self.assertEquals(len(self.logger.txn_id), 34) |
70 | 72 |
|
71 | 73 | if __name__ == '__main__':
|
72 | 74 | unittest.main()
|
0 commit comments