Process json based on content-type

OpenStack provides content-type which we can respond to. That we do not
currently provide it in our unittests is our fault. So let's provide it.

Change-Id: Ib20f4df950cbedf404c0fbe3ef4c39660eb1b70f
Depends-On: Iaef2a140e33fc48f8bfa8ff4769eded37ce152c6
This commit is contained in:
Monty Taylor 2017-01-26 15:09:44 -06:00
parent 2a38af599d
commit 16a058f16e
3 changed files with 308 additions and 546 deletions

View File

@ -90,43 +90,38 @@ class ShadeAdapter(adapter.Adapter):
def _munch_response(self, response, result_key=None): def _munch_response(self, response, result_key=None):
exc.raise_from_response(response) exc.raise_from_response(response)
# Glance image downloads just return the data in the body
if response.headers.get('Content-Type') in ( if not response.content:
'text/plain', # This doens't have any content
'application/octet-stream'):
return response return response
# Some REST calls do not return json content. Don't decode it.
if 'application/json' not in response.headers.get('Content-Type'):
return response
try:
result_json = response.json()
except Exception:
return response
request_id = response.headers.get('x-openstack-request-id')
if task_manager._is_listlike(result_json):
return meta.obj_list_to_dict(
result_json, request_id=request_id)
# Wrap the keys() call in list() because in python3 keys returns
# a "dict_keys" iterator-like object rather than a list
json_keys = list(result_json.keys())
if len(json_keys) > 1 and result_key:
result = result_json[result_key]
elif len(json_keys) == 1:
result = result_json[json_keys[0]]
else: else:
if not response.content: # Passthrough the whole body - sometimes (hi glance) things
# This doens't have any content # come through without a top-level container. Also, sometimes
return response # you need to deal with pagination
try: result = result_json
result_json = response.json()
except Exception:
raise exc.OpenStackCloudHTTPError(
"Problems decoding json from response."
" Reponse: {code} {reason}".format(
code=response.status_code,
reason=response.reason),
response=response)
request_id = response.headers.get('x-openstack-request-id')
if task_manager._is_listlike(result_json):
return meta.obj_list_to_dict(
result_json, request_id=request_id)
# Wrap the keys() call in list() because in python3 keys returns
# a "dict_keys" iterator-like object rather than a list
json_keys = list(result_json.keys())
if len(json_keys) > 1 and result_key:
result = result_json[result_key]
elif len(json_keys) == 1:
result = result_json[json_keys[0]]
else:
# Passthrough the whole body - sometimes (hi glance) things
# come through without a top-level container. Also, sometimes
# you need to deal with pagination
result = result_json
if task_manager._is_listlike(result): if task_manager._is_listlike(result):
return meta.obj_list_to_dict(result, request_id=request_id) return meta.obj_list_to_dict(result, request_id=request_id)

View File

@ -19,6 +19,7 @@ import fixtures
import mock import mock
import os import os
import os_client_config as occ import os_client_config as occ
from requests import structures
from requests_mock.contrib import fixture as rm_fixture from requests_mock.contrib import fixture as rm_fixture
import tempfile import tempfile
@ -167,6 +168,11 @@ class RequestsMockTestCase(BaseTestCase):
def register_uri(self, method, uri, **kwargs): def register_uri(self, method, uri, **kwargs):
validate = kwargs.pop('validate', {}) validate = kwargs.pop('validate', {})
key = '{method}:{uri}'.format(method=method, uri=uri) key = '{method}:{uri}'.format(method=method, uri=uri)
headers = structures.CaseInsensitiveDict(kwargs.pop('headers', {}))
if 'content-type' not in headers:
headers[u'content-type'] = 'application/json'
kwargs['headers'] = headers
if key in self._uri_registry: if key in self._uri_registry:
self._uri_registry[key].append(kwargs) self._uri_registry[key].append(kwargs)
self.adapter.register_uri(method, uri, self._uri_registry[key]) self.adapter.register_uri(method, uri, self._uri_registry[key])

File diff suppressed because it is too large Load Diff