-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtests.py
125 lines (95 loc) · 3.87 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import uuid
import unittest
import unittest.mock
from tornado import testing, web
from sprockets.mixins import correlation
from sprockets.mixins.correlation.mixins import correlation_id_logger
class RequestHandler(web.RequestHandler):
def get(self, status_code):
status_code = int(status_code)
self.set_status(status_code)
if status_code >= 300:
raise web.HTTPError(status_code)
self.write('status {0}'.format(status_code))
class CorrelatedRequestHandler(correlation.HandlerMixin, RequestHandler):
pass
class CorrelationMixinTests(testing.AsyncHTTPTestCase):
def get_app(self):
return web.Application([
(r'/status/(?P<status_code>\d+)', CorrelatedRequestHandler),
])
def test_that_correlation_id_is_returned_when_successful(self):
response = self.fetch('/status/200')
self.assertIsNotNone(response.headers.get('Correlation-ID'))
def test_that_correlation_id_is_returned_in_error(self):
response = self.fetch('/status/500')
self.assertIsNotNone(response.headers.get('Correlation-ID'))
def test_that_correlation_id_is_copied_from_request(self):
correlation_id = uuid.uuid4().hex
response = self.fetch('/status/500',
headers={'Correlation-Id': correlation_id})
self.assertEqual(response.headers['correlation-id'], correlation_id)
class CorrelationIDLoggerTests(testing.AsyncHTTPTestCase):
def get_app(self):
return web.Application([
(r'/status/(?P<status_code>\d+)', CorrelatedRequestHandler),
(r'/status/no-correlation/(?P<status_code>\d+)', RequestHandler),
], log_function=correlation_id_logger)
def setUp(self):
self.patcher = unittest.mock.patch(
'sprockets.mixins.correlation.mixins.log.access_log')
self.access_logger = self.patcher.start()
super().setUp()
def tearDown(self):
self.patcher.stop()
super().tearDown()
def test_lt_400_logs_info(self):
for status in (200, 202):
response = self.fetch('/status/{}'.format(status))
self.access_logger.info.assert_any_call(
"%d %s %.2fms {CID %s}",
status,
unittest.mock.ANY,
unittest.mock.ANY,
response.headers['correlation-id']
)
def test_gte_400_lt_500_logs_warning(self):
for status in (400, 429):
response = self.fetch('/status/{}'.format(status))
self.access_logger.warning.assert_any_call(
"%d %s %.2fms {CID %s}",
status,
unittest.mock.ANY,
unittest.mock.ANY,
response.headers['correlation-id']
)
def test_gte_500_logs_error(self):
for status in (500, 504):
response = self.fetch('/status/{}'.format(status))
self.access_logger.error.assert_any_call(
"%d %s %.2fms {CID %s}",
status,
unittest.mock.ANY,
unittest.mock.ANY,
response.headers['correlation-id']
)
def test_uses_correlation_id_from_header_if_missing_from_handler(self):
correlation_id = uuid.uuid4().hex
self.fetch('/status/no-correlation/200',
headers={'Correlation-Id': correlation_id})
self.access_logger.info.assert_any_call(
"%d %s %.2fms {CID %s}",
200,
unittest.mock.ANY,
unittest.mock.ANY,
correlation_id
)
def test_correlation_id_is_none_if_missing_from_handler_and_header(self):
self.fetch('/status/no-correlation/200')
self.access_logger.info.assert_any_call(
"%d %s %.2fms {CID %s}",
200,
unittest.mock.ANY,
unittest.mock.ANY,
None
)