Merge "Fix Request ID has a double 'req-' at the start"
This commit is contained in:
@@ -53,7 +53,14 @@ class BaseContextMiddleware(wsgi.Middleware):
|
||||
except AttributeError:
|
||||
LOG.warn(_('Unable to retrieve request id from context'))
|
||||
else:
|
||||
resp.headers['x-openstack-request-id'] = 'req-%s' % request_id
|
||||
# For python 3 compatibility need to use bytes type
|
||||
prefix = b'req-' if isinstance(request_id, bytes) else 'req-'
|
||||
|
||||
if not request_id.startswith(prefix):
|
||||
request_id = prefix + request_id
|
||||
|
||||
resp.headers['x-openstack-request-id'] = request_id
|
||||
|
||||
return resp
|
||||
|
||||
|
||||
|
||||
@@ -118,6 +118,20 @@ class TestContextMiddleware(base.IsolatedUnitTest):
|
||||
self.assertRaises(webob.exc.HTTPInternalServerError,
|
||||
middleware.process_request, req)
|
||||
|
||||
def test_response(self):
|
||||
req = self._build_request()
|
||||
req.context = glance.context.RequestContext()
|
||||
request_id = req.context.request_id
|
||||
|
||||
resp = webob.Response()
|
||||
resp.request = req
|
||||
self._build_middleware().process_response(resp)
|
||||
self.assertEqual(request_id, resp.headers['x-openstack-request-id'])
|
||||
resp_req_id = resp.headers['x-openstack-request-id']
|
||||
# Validate that request-id do not starts with 'req-req-'
|
||||
self.assertFalse(resp_req_id.startswith(b'req-req-'))
|
||||
self.assertTrue(resp_req_id.startswith(b'req-'))
|
||||
|
||||
|
||||
class TestUnauthenticatedContextMiddleware(base.IsolatedUnitTest):
|
||||
def test_request(self):
|
||||
@@ -139,5 +153,8 @@ class TestUnauthenticatedContextMiddleware(base.IsolatedUnitTest):
|
||||
resp = webob.Response()
|
||||
resp.request = req
|
||||
middleware.process_response(resp)
|
||||
self.assertEqual(resp.headers['x-openstack-request-id'],
|
||||
'req-%s' % request_id)
|
||||
self.assertEqual(request_id, resp.headers['x-openstack-request-id'])
|
||||
resp_req_id = resp.headers['x-openstack-request-id']
|
||||
# Validate that request-id do not starts with 'req-req-'
|
||||
self.assertFalse(resp_req_id.startswith(b'req-req-'))
|
||||
self.assertTrue(resp_req_id.startswith(b'req-'))
|
||||
|
||||
Reference in New Issue
Block a user