]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Include proper Content-Type in the HTTP response headers
authorsridhargaddam <sridhar.gaddam@enovance.com>
Mon, 24 Feb 2014 15:21:31 +0000 (20:51 +0530)
committerThomas Goirand <thomas@goirand.fr>
Thu, 13 Mar 2014 07:20:32 +0000 (15:20 +0800)
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
neutron/agent/metadata/namespace_proxy.py
neutron/tests/unit/test_metadata_agent.py
neutron/tests/unit/test_metadata_namespace_proxy.py

index 06e88be73a65a1d592c4454beb8573b8f865f70d..f7a1c982c97394675e6a617318dd24feb38b67ae 100644 (file)
@@ -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 '
index c6addb8fee005c693084137a3e404c49c76f6736..ed9be8f0e9acc88c0621cf3840e11a1861e2d853 100644 (file)
@@ -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:
index 8a56b62a933fbe8f525daad960a2d9c9bbf1eb94..68cb2b9a28490b61d40bd6c8fb8f69ad53bfea74 100644 (file)
@@ -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),
index ad992d4dc0834420293b7fd34bd255a9b7e94d60..26d7ba9df8413eaef59c52394663ca4d2a89203d 100644 (file)
@@ -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'