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
This commit is contained in:
sridhargaddam 2014-02-24 20:51:31 +05:30
parent ee0788b9b3
commit 53b8073548
4 changed files with 27 additions and 12 deletions

View File

@ -168,7 +168,9 @@ class MetadataProxyHandler(object):
if resp.status == 200: if resp.status == 200:
LOG.debug(str(resp)) LOG.debug(str(resp))
return content req.response.content_type = resp['content-type']
req.response.body = content
return req.response
elif resp.status == 403: elif resp.status == 403:
msg = _( msg = _(
'The remote metadata server responded with Forbidden. This ' 'The remote metadata server responded with Forbidden. This '

View File

@ -19,7 +19,6 @@
import httplib import httplib
import socket import socket
import eventlet import eventlet
import httplib2 import httplib2
from oslo.config import cfg from oslo.config import cfg
@ -108,7 +107,11 @@ class NetworkMetadataProxyHandler(object):
if resp.status == 200: if resp.status == 200:
LOG.debug(resp) LOG.debug(resp)
LOG.debug(content) 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: elif resp.status == 404:
return webob.exc.HTTPNotFound() return webob.exc.HTTPNotFound()
elif resp.status == 409: elif resp.status == 409:

View File

@ -200,10 +200,12 @@ class TestMetadataProxyHandler(base.BaseTestCase):
req = mock.Mock(path_info='/the_path', query_string='', headers=hdrs, req = mock.Mock(path_info='/the_path', query_string='', headers=hdrs,
method=method, body=body) 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: with mock.patch.object(self.handler, '_sign_instance_id') as sign:
sign.return_value = 'signed' sign.return_value = 'signed'
with mock.patch('httplib2.Http') as mock_http: with mock.patch('httplib2.Http') as mock_http:
resp.__getitem__.return_value = "text/plain"
mock_http.return_value.request.return_value = (resp, 'content') mock_http.return_value.request.return_value = (resp, 'content')
retval = self.handler._proxy_request('the_id', 'tenant_id', retval = self.handler._proxy_request('the_id', 'tenant_id',
@ -225,11 +227,14 @@ class TestMetadataProxyHandler(base.BaseTestCase):
return retval return retval
def test_proxy_request_post(self): def test_proxy_request_post(self):
self.assertEqual('content', response = self._proxy_request_test_helper(method='POST')
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): 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): def test_proxy_request_403(self):
self.assertIsInstance(self._proxy_request_test_helper(403), self.assertIsInstance(self._proxy_request_test_helper(403),

View File

@ -95,8 +95,9 @@ class TestNetworkMetadataProxyHandler(base.BaseTestCase):
def test_proxy_request_router_200(self): def test_proxy_request_router_200(self):
self.handler.router_id = 'router_id' self.handler.router_id = 'router_id'
resp = mock.Mock(status=200) resp = mock.MagicMock(status=200)
with mock.patch('httplib2.Http') as mock_http: with mock.patch('httplib2.Http') as mock_http:
resp.__getitem__.return_value = "text/plain"
mock_http.return_value.request.return_value = (resp, 'content') mock_http.return_value.request.return_value = (resp, 'content')
retval = self.handler._proxy_request('192.168.1.1', 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): def test_proxy_request_network_200(self):
self.handler.network_id = 'network_id' self.handler.network_id = 'network_id'
resp = mock.Mock(status=200) resp = mock.MagicMock(status=200)
with mock.patch('httplib2.Http') as mock_http: 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', retval = self.handler._proxy_request('192.168.1.1',
'GET', '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): def test_proxy_request_network_404(self):
self.handler.network_id = 'network_id' self.handler.network_id = 'network_id'