~ubuntu-branches/ubuntu/trusty/swift/trusty-updates

« back to all changes in this revision

Viewing changes to test/unit/common/middleware/test_except.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-01-28 09:40:30 UTC
  • mfrom: (1.2.16)
  • Revision ID: package-import@ubuntu.com-20130128094030-aetz57x2qz9ye2d4
Tags: 1.7.6-0ubuntu1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
from swift.common.utils import get_logger
21
21
 
22
22
class FakeApp(object):
23
 
    def __init__(self, error=False):
 
23
    def __init__(self, error=False, body_iter=None):
24
24
        self.error = error
 
25
        self.body_iter = body_iter
25
26
 
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')
29
30
        if self.error:
30
31
            raise Exception('An error occurred')
31
 
        return ["FAKE APP"]
 
32
        if self.body_iter is None:
 
33
            return ["FAKE APP"]
 
34
        else:
 
35
            return self.body_iter
32
36
 
33
37
def start_response(*args):
34
38
    pass
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'])
47
51
 
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'])
53
57
 
54
58
    def test_trans_id_header_pass(self):
55
59
        self.assertEquals(self.logger.txn_id, None)
56
 
        def start_response(status, headers):
 
60
 
 
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')
62
67
 
63
68
    def test_trans_id_header_fail(self):
64
69
        self.assertEquals(self.logger.txn_id, None)
65
 
        def start_response(status, headers):
 
70
 
 
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)
71
77
 
 
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'])
 
84
 
72
85
if __name__ == '__main__':
73
86
    unittest.main()