Merge "Check non json type on glance client json_request method"
This commit is contained in:
commit
a8096b0bc9
|
@ -160,6 +160,9 @@ class HTTPClient(object):
|
|||
def json_request(self, method, url, **kwargs):
|
||||
kwargs.setdefault('headers', {})
|
||||
kwargs['headers'].setdefault('Content-Type', 'application/json')
|
||||
if kwargs['headers']['Content-Type'] != 'application/json':
|
||||
msg = "Only application/json content-type is supported."
|
||||
raise exc.InvalidContentType(msg)
|
||||
|
||||
if 'body' in kwargs:
|
||||
kwargs['body'] = json.dumps(kwargs['body'])
|
||||
|
@ -173,7 +176,8 @@ class HTTPClient(object):
|
|||
except ValueError:
|
||||
LOG.error('Could not decode response body as JSON')
|
||||
else:
|
||||
body = None
|
||||
msg = "Only json/application content-type is supported."
|
||||
raise exc.InvalidContentType(msg)
|
||||
|
||||
return resp, body
|
||||
|
||||
|
|
|
@ -54,26 +54,37 @@ class TestGlanceHTTPClient(base.TestCase):
|
|||
return_value=resp))
|
||||
return resp
|
||||
|
||||
def test_json_request_without_content_type_header(self):
|
||||
def test_json_request_without_content_type_header_in_response(self):
|
||||
self._set_response_fixture({}, 200, 'fake_response_body')
|
||||
resp, body = self.client.json_request('GET', '/images')
|
||||
self.assertEqual(200, resp.status)
|
||||
self.assertIsNone(body)
|
||||
self.assertRaises(exceptions.InvalidContentType,
|
||||
self.client.json_request, 'GET', '/images')
|
||||
|
||||
def test_json_request_with_xml_content_type_header(self):
|
||||
def test_json_request_with_xml_content_type_header_in_request(self):
|
||||
self.assertRaises(exceptions.InvalidContentType,
|
||||
self.client.json_request, 'GET', '/images',
|
||||
headers={'Content-Type': 'application/xml'})
|
||||
|
||||
def test_json_request_with_xml_content_type_header_in_response(self):
|
||||
self._set_response_fixture({'content-type': 'application/xml'},
|
||||
200, 'fake_response_body')
|
||||
resp, body = self.client.json_request('GET', '/images')
|
||||
self.assertEqual(200, resp.status)
|
||||
self.assertIsNone(body)
|
||||
self.assertRaises(exceptions.InvalidContentType,
|
||||
self.client.json_request, 'GET', '/images')
|
||||
|
||||
def test_json_request_with_content_type_header(self):
|
||||
def test_json_request_with_json_content_type_header_only_in_resp(self):
|
||||
self._set_response_fixture({'content-type': 'application/json'},
|
||||
200, 'fake_response_body')
|
||||
resp, body = self.client.json_request('GET', '/images')
|
||||
self.assertEqual(200, resp.status)
|
||||
self.assertEqual('fake_response_body', body)
|
||||
|
||||
def test_json_request_with_json_content_type_header_in_req_and_resp(self):
|
||||
self._set_response_fixture({'content-type': 'application/json'},
|
||||
200, 'fake_response_body')
|
||||
resp, body = self.client.json_request('GET', '/images', headers={
|
||||
'Content-Type': 'application/json'})
|
||||
self.assertEqual(200, resp.status)
|
||||
self.assertEqual('fake_response_body', body)
|
||||
|
||||
def test_json_request_fails_to_json_loads(self):
|
||||
self._set_response_fixture({'content-type': 'application/json'},
|
||||
200, 'fake_response_body')
|
||||
|
|
Loading…
Reference in New Issue