From 5165320257b18ca1917727c1ae1d3cdde366d85d Mon Sep 17 00:00:00 2001 From: Naveed Massjouni Date: Mon, 9 Jan 2012 21:51:09 +0000 Subject: [PATCH] Adding the request id to response headers. bp:nova-request-response-id Change-Id: Ie2d49337010f6e7a540018f9c8d542b6c8ebbce2 --- nova/api/openstack/wsgi.py | 5 ++++- nova/tests/api/openstack/test_wsgi.py | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index bc19a0ae68a0..7c23d564cad8 100644 --- a/nova/api/openstack/wsgi.py +++ b/nova/api/openstack/wsgi.py @@ -438,6 +438,9 @@ class ResponseHeadersSerializer(ActionDispatcher): def serialize(self, response, data, action): self.dispatch(response, data, action=action) + context = response.request.environ.get('nova.context') + if context: + response.headers['X-Compute-Request-Id'] = context.request_id def default(self, response, data): response.status_int = 200 @@ -464,7 +467,7 @@ class ResponseSerializer(object): :param content_type: expected mimetype of serialized response body """ - response = webob.Response() + response = webob.Response(request=request) self.serialize_headers(response, response_data, action) self.serialize_body(request, response, response_data, content_type, action) diff --git a/nova/tests/api/openstack/test_wsgi.py b/nova/tests/api/openstack/test_wsgi.py index af9497a5aecd..4082a6681f72 100644 --- a/nova/tests/api/openstack/test_wsgi.py +++ b/nova/tests/api/openstack/test_wsgi.py @@ -5,7 +5,9 @@ import webob from nova import exception from nova import test +from nova import utils from nova.api.openstack import wsgi +import nova.context class RequestTest(test.TestCase): @@ -97,7 +99,9 @@ class ActionDispatcherTest(test.TestCase): class ResponseHeadersSerializerTest(test.TestCase): def test_default(self): serializer = wsgi.ResponseHeadersSerializer() - response = webob.Response() + context = nova.context.get_admin_context() + req = webob.Request.blank('/', environ={'nova.context': context}) + response = webob.Response(request=req) serializer.serialize(response, {'v': '123'}, 'asdf') self.assertEqual(response.status_int, 200) @@ -107,7 +111,9 @@ class ResponseHeadersSerializerTest(test.TestCase): response.status_int = 404 response.headers['X-Custom-Header'] = data['v'] serializer = Serializer() - response = webob.Response() + context = nova.context.get_admin_context() + req = webob.Request.blank('/', environ={'nova.context': context}) + response = webob.Response(request=req) serializer.serialize(response, {'v': '123'}, 'update') self.assertEqual(response.status_int, 404) self.assertEqual(response.headers['X-Custom-Header'], '123') @@ -215,6 +221,17 @@ class RequestHeadersDeserializerTest(test.TestCase): self.assertEqual(deserializer.deserialize(req, 'update'), {'a': 'b'}) +class ResponseHeadersSerializerTest(test.TestCase): + def test_request_id(self): + serializer = wsgi.ResponseHeadersSerializer() + context = nova.context.get_admin_context() + req = webob.Request.blank('/', environ={'nova.context': context}) + res = webob.Response(request=req) + serializer.serialize(res, {}, 'foo') + self.assertTrue( + utils.is_uuid_like(res.headers['X-Compute-Request-Id'])) + + class JSONSerializer(object): def serialize(self, data, action='default'): return 'pew_json'