From 2b201c4500c5029305c7db4d6188f04b00a64974 Mon Sep 17 00:00:00 2001 From: sridhargaddam Date: Mon, 24 Feb 2014 20:51:31 +0530 Subject: [PATCH] Include proper Content-Type in the HTTP response headers Neutron namespace proxy handler and metadata agent were not setting the Content-Type in its response. Both of them were returning only the response data which is obtained from the nova-metadata-service. Since they were returning only the response data, the Content-Type returned to the clients has the default one which is - "text/html". Ideally this should be set to the data type which is present in the HTTP Response. The fix now includes the Content-Type which is returned by nova-metadata-service Closes-Bug: #1243878 Change-Id: If68f0b508fbea4ecd1eb0e58d602b5ba6ccbe263 --- neutron/agent/metadata/agent.py | 4 +++- neutron/agent/metadata/namespace_proxy.py | 7 +++++-- neutron/tests/unit/test_metadata_agent.py | 13 +++++++++---- .../tests/unit/test_metadata_namespace_proxy.py | 15 ++++++++++----- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/neutron/agent/metadata/agent.py b/neutron/agent/metadata/agent.py index 06e88be73..f7a1c982c 100644 --- a/neutron/agent/metadata/agent.py +++ b/neutron/agent/metadata/agent.py @@ -168,7 +168,9 @@ class MetadataProxyHandler(object): if resp.status == 200: LOG.debug(str(resp)) - return content + req.response.content_type = resp['content-type'] + req.response.body = content + return req.response elif resp.status == 403: msg = _( 'The remote metadata server responded with Forbidden. This ' diff --git a/neutron/agent/metadata/namespace_proxy.py b/neutron/agent/metadata/namespace_proxy.py index c6addb8fe..ed9be8f0e 100644 --- a/neutron/agent/metadata/namespace_proxy.py +++ b/neutron/agent/metadata/namespace_proxy.py @@ -19,7 +19,6 @@ import httplib import socket - import eventlet import httplib2 from oslo.config import cfg @@ -108,7 +107,11 @@ class NetworkMetadataProxyHandler(object): if resp.status == 200: LOG.debug(resp) LOG.debug(content) - return content + response = webob.Response() + response.status = resp.status + response.headers['Content-Type'] = resp['content-type'] + response.body = content + return response elif resp.status == 404: return webob.exc.HTTPNotFound() elif resp.status == 409: diff --git a/neutron/tests/unit/test_metadata_agent.py b/neutron/tests/unit/test_metadata_agent.py index 8a56b62a9..68cb2b9a2 100644 --- a/neutron/tests/unit/test_metadata_agent.py +++ b/neutron/tests/unit/test_metadata_agent.py @@ -200,10 +200,12 @@ class TestMetadataProxyHandler(base.BaseTestCase): req = mock.Mock(path_info='/the_path', query_string='', headers=hdrs, method=method, body=body) - resp = mock.Mock(status=response_code) + resp = mock.MagicMock(status=response_code) + req.response = resp with mock.patch.object(self.handler, '_sign_instance_id') as sign: sign.return_value = 'signed' with mock.patch('httplib2.Http') as mock_http: + resp.__getitem__.return_value = "text/plain" mock_http.return_value.request.return_value = (resp, 'content') retval = self.handler._proxy_request('the_id', 'tenant_id', @@ -225,11 +227,14 @@ class TestMetadataProxyHandler(base.BaseTestCase): return retval def test_proxy_request_post(self): - self.assertEqual('content', - self._proxy_request_test_helper(method='POST')) + response = self._proxy_request_test_helper(method='POST') + self.assertEqual(response.content_type, "text/plain") + self.assertEqual(response.body, 'content') def test_proxy_request_200(self): - self.assertEqual('content', self._proxy_request_test_helper(200)) + response = self._proxy_request_test_helper(200) + self.assertEqual(response.content_type, "text/plain") + self.assertEqual(response.body, 'content') def test_proxy_request_403(self): self.assertIsInstance(self._proxy_request_test_helper(403), diff --git a/neutron/tests/unit/test_metadata_namespace_proxy.py b/neutron/tests/unit/test_metadata_namespace_proxy.py index ad992d4dc..26d7ba9df 100644 --- a/neutron/tests/unit/test_metadata_namespace_proxy.py +++ b/neutron/tests/unit/test_metadata_namespace_proxy.py @@ -95,8 +95,9 @@ class TestNetworkMetadataProxyHandler(base.BaseTestCase): def test_proxy_request_router_200(self): self.handler.router_id = 'router_id' - resp = mock.Mock(status=200) + resp = mock.MagicMock(status=200) with mock.patch('httplib2.Http') as mock_http: + resp.__getitem__.return_value = "text/plain" mock_http.return_value.request.return_value = (resp, 'content') retval = self.handler._proxy_request('192.168.1.1', @@ -118,14 +119,16 @@ class TestNetworkMetadataProxyHandler(base.BaseTestCase): )] ) - self.assertEqual(retval, 'content') + self.assertEqual(retval.headers['Content-Type'], 'text/plain') + self.assertEqual(retval.body, 'content') def test_proxy_request_network_200(self): self.handler.network_id = 'network_id' - resp = mock.Mock(status=200) + resp = mock.MagicMock(status=200) with mock.patch('httplib2.Http') as mock_http: - mock_http.return_value.request.return_value = (resp, 'content') + resp.__getitem__.return_value = "application/json" + mock_http.return_value.request.return_value = (resp, '{}') retval = self.handler._proxy_request('192.168.1.1', 'GET', @@ -146,7 +149,9 @@ class TestNetworkMetadataProxyHandler(base.BaseTestCase): )] ) - self.assertEqual(retval, 'content') + self.assertEqual(retval.headers['Content-Type'], + 'application/json') + self.assertEqual(retval.body, '{}') def test_proxy_request_network_404(self): self.handler.network_id = 'network_id' -- 2.45.2