From edcf4eca66c91db032bdb56572eba4437947948d Mon Sep 17 00:00:00 2001 From: Andrew Laski Date: Fri, 31 May 2013 11:55:30 -0400 Subject: Add x-compute-request-id header when no response body The x-compute-request-id header was only being added for requests which generated a response body, like an index or show. This adds it to requests which generate no response body, like create/update/delete, or actions on servers like resize. Bug 1183712 Change-Id: Ia0e9d75e4b84280caab060fae58036ed818e00c3 --- nova/api/openstack/wsgi.py | 4 +++- nova/tests/api/openstack/test_wsgi.py | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index eda82483f..bf5627b3a 100644 --- a/nova/api/openstack/wsgi.py +++ b/nova/api/openstack/wsgi.py @@ -972,7 +972,6 @@ class Resource(wsgi.Application): # Run post-processing extensions if resp_obj: - _set_request_id_header(request, resp_obj) # Do a preserialize to set up the response object serializers = getattr(meth, 'wsgi_serializers', {}) resp_obj._bind_method_serializers(serializers) @@ -988,6 +987,9 @@ class Resource(wsgi.Application): response = resp_obj.serialize(request, accept, self.default_serializers) + if context and hasattr(response, 'headers'): + response.headers.add('x-compute-request-id', context.request_id) + return response def get_method(self, request, action, content_type, body): diff --git a/nova/tests/api/openstack/test_wsgi.py b/nova/tests/api/openstack/test_wsgi.py index 4c10ec6b6..41a80a65c 100644 --- a/nova/tests/api/openstack/test_wsgi.py +++ b/nova/tests/api/openstack/test_wsgi.py @@ -449,6 +449,49 @@ class ResourceTest(test.TestCase): self.assertEqual(content_type, 'application/json') self.assertEqual(body, 'foo') + def test_get_request_id_with_dict_response_body(self): + class Controller(wsgi.Controller): + def index(self, req): + return {'foo': 'bar'} + + req = fakes.HTTPRequest.blank('/tests') + context = req.environ['nova.context'] + app = fakes.TestRouter(Controller()) + response = req.get_response(app) + self.assertEqual(response.headers['x-compute-request-id'], + context.request_id) + self.assertEqual(response.body, '{"foo": "bar"}') + self.assertEqual(response.status_int, 200) + + def test_no_request_id_with_str_response_body(self): + class Controller(wsgi.Controller): + def index(self, req): + return 'foo' + + req = fakes.HTTPRequest.blank('/tests') + app = fakes.TestRouter(Controller()) + response = req.get_response(app) + # NOTE(alaski): This test is really to ensure that a str response + # doesn't error. Not having a request_id header is a side effect of + # our wsgi setup, ideally it would be there. + self.assertFalse(hasattr(response.headers, 'x-compute-request-id')) + self.assertEqual(response.body, 'foo') + self.assertEqual(response.status_int, 200) + + def test_get_request_id_no_response_body(self): + class Controller(object): + def index(self, req): + pass + + req = fakes.HTTPRequest.blank('/tests') + context = req.environ['nova.context'] + app = fakes.TestRouter(Controller()) + response = req.get_response(app) + self.assertEqual(response.headers['x-compute-request-id'], + context.request_id) + self.assertEqual(response.body, '') + self.assertEqual(response.status_int, 200) + def test_deserialize_badtype(self): class Controller(object): def index(self, req, pants=None): -- cgit