2015-03-28 16:51:48 +00:00
|
|
|
import uuid
|
|
|
|
import unittest
|
|
|
|
|
2018-11-26 20:41:24 +00:00
|
|
|
from tornado import testing, web
|
2015-03-28 16:51:48 +00:00
|
|
|
|
|
|
|
from sprockets.mixins import correlation
|
|
|
|
|
|
|
|
|
|
|
|
class CorrelatedRequestHandler(correlation.HandlerMixin, web.RequestHandler):
|
|
|
|
|
|
|
|
def get(self, status_code):
|
|
|
|
status_code = int(status_code)
|
|
|
|
if status_code >= 300:
|
|
|
|
raise web.HTTPError(status_code)
|
|
|
|
self.write('status {0}'.format(status_code))
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
2018-11-26 20:22:51 +00:00
|
|
|
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)
|