Merge "Append 'Openstack-Request-Id' header to the response"

This commit is contained in:
Jenkins 2016-03-22 19:57:34 +00:00 committed by Gerrit Code Review
commit 250d26b05b
3 changed files with 33 additions and 0 deletions

View File

@ -89,6 +89,15 @@ class ContextHook(hooks.PecanHook):
show_password=show_password,
**creds)
def after(self, state):
if state.request.context == {}:
# An incorrect url path will not create RequestContext
return
# NOTE(lintan): RequestContext will generate a request_id if no one
# passing outside, so it always contain a request_id.
request_id = state.request.context.request_id
state.response.headers['Openstack-Request-Id'] = request_id
class RPCHook(hooks.PecanHook):
"""Attach the rpcapi object to the request so controllers can get to it."""

View File

@ -285,6 +285,26 @@ class TestContextHook(base.BaseApiTest):
is_admin=False,
roles=headers['X-Roles'].split(','))
@mock.patch.object(context, 'RequestContext')
def test_context_hook_after_add_request_id(self, mock_ctx):
headers = fake_headers(admin=True)
reqstate = FakeRequestState(headers=headers)
reqstate.set_context()
reqstate.request.context.request_id = 'fake-id'
context_hook = hooks.ContextHook(None)
context_hook.after(reqstate)
self.assertIn('Openstack-Request-Id',
reqstate.response.headers)
self.assertEqual(
'fake-id',
reqstate.response.headers['Openstack-Request-Id'])
def test_context_hook_after_miss_context(self):
response = self.get_json('/bad/path',
expect_errors=True)
self.assertNotIn('Openstack-Request-Id',
response.headers)
class TestTrustedCallHook(base.BaseApiTest):
def test_trusted_call_hook_not_admin(self):

View File

@ -0,0 +1,4 @@
---
features:
- Append request_id as ``Openstack-Request-Id`` header to the response.