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):
|
def json_request(self, method, url, **kwargs):
|
||||||
kwargs.setdefault('headers', {})
|
kwargs.setdefault('headers', {})
|
||||||
kwargs['headers'].setdefault('Content-Type', 'application/json')
|
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:
|
if 'body' in kwargs:
|
||||||
kwargs['body'] = json.dumps(kwargs['body'])
|
kwargs['body'] = json.dumps(kwargs['body'])
|
||||||
@ -173,7 +176,8 @@ class HTTPClient(object):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
LOG.error('Could not decode response body as JSON')
|
LOG.error('Could not decode response body as JSON')
|
||||||
else:
|
else:
|
||||||
body = None
|
msg = "Only json/application content-type is supported."
|
||||||
|
raise exc.InvalidContentType(msg)
|
||||||
|
|
||||||
return resp, body
|
return resp, body
|
||||||
|
|
||||||
|
@ -54,26 +54,37 @@ class TestGlanceHTTPClient(base.TestCase):
|
|||||||
return_value=resp))
|
return_value=resp))
|
||||||
return 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')
|
self._set_response_fixture({}, 200, 'fake_response_body')
|
||||||
resp, body = self.client.json_request('GET', '/images')
|
self.assertRaises(exceptions.InvalidContentType,
|
||||||
self.assertEqual(200, resp.status)
|
self.client.json_request, 'GET', '/images')
|
||||||
self.assertIsNone(body)
|
|
||||||
|
|
||||||
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'},
|
self._set_response_fixture({'content-type': 'application/xml'},
|
||||||
200, 'fake_response_body')
|
200, 'fake_response_body')
|
||||||
resp, body = self.client.json_request('GET', '/images')
|
self.assertRaises(exceptions.InvalidContentType,
|
||||||
self.assertEqual(200, resp.status)
|
self.client.json_request, 'GET', '/images')
|
||||||
self.assertIsNone(body)
|
|
||||||
|
|
||||||
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'},
|
self._set_response_fixture({'content-type': 'application/json'},
|
||||||
200, 'fake_response_body')
|
200, 'fake_response_body')
|
||||||
resp, body = self.client.json_request('GET', '/images')
|
resp, body = self.client.json_request('GET', '/images')
|
||||||
self.assertEqual(200, resp.status)
|
self.assertEqual(200, resp.status)
|
||||||
self.assertEqual('fake_response_body', body)
|
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):
|
def test_json_request_fails_to_json_loads(self):
|
||||||
self._set_response_fixture({'content-type': 'application/json'},
|
self._set_response_fixture({'content-type': 'application/json'},
|
||||||
200, 'fake_response_body')
|
200, 'fake_response_body')
|
||||||
|
Loading…
Reference in New Issue
Block a user