diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 18c47aed..056b7de5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,7 @@ Changelog ========= - The content will be used to build the Changelog for the new bravado-core release +- Fix handling of API calls that return non-JSON content (specifically text content). (add before this line your modifications summary and PR reference) 4.7.3 (2017-05-05) diff --git a/bravado_core/response.py b/bravado_core/response.py index 1dfed482..a8393f46 100644 --- a/bravado_core/response.py +++ b/bravado_core/response.py @@ -101,15 +101,19 @@ def has_content(response_spec): if not has_content(response_spec): return None - # TODO: Non-json response contents - content_spec = deref(response_spec['schema']) - content_value = response.json() + content_type = response.headers.get('content-type', '').lower() + + if content_type.startswith(APP_JSON): + content_spec = deref(response_spec['schema']) + content_value = response.json() - if op.swagger_spec.config['validate_responses']: - validate_schema_object(op.swagger_spec, content_spec, content_value) + if op.swagger_spec.config['validate_responses']: + validate_schema_object(op.swagger_spec, content_spec, content_value) - return unmarshal_schema_object( - op.swagger_spec, content_spec, content_value) + return unmarshal_schema_object( + op.swagger_spec, content_spec, content_value) + # TODO: Non-json response contents + return response.text def get_response_spec(status_code, op): diff --git a/tests/response/unmarshal_response_test.py b/tests/response/unmarshal_response_test.py index f1ea3b45..4096f778 100644 --- a/tests/response/unmarshal_response_test.py +++ b/tests/response/unmarshal_response_test.py @@ -3,6 +3,7 @@ from mock import Mock from mock import patch +from bravado_core.content_type import APP_JSON from bravado_core.response import IncomingResponse from bravado_core.response import unmarshal_response @@ -34,6 +35,7 @@ def test_json_content(empty_swagger_spec, response_spec): response = Mock( spec=IncomingResponse, status_code=200, + headers={'content-type': APP_JSON}, json=Mock(return_value='Monday')) with patch('bravado_core.response.get_response_spec') as m: @@ -42,11 +44,25 @@ def test_json_content(empty_swagger_spec, response_spec): assert 'Monday' == unmarshal_response(response, op) +def test_text_content(empty_swagger_spec, response_spec): + response = Mock( + spec=IncomingResponse, + status_code=200, + headers={'content-type': 'text/plain'}, + text='Monday') + + with patch('bravado_core.response.get_response_spec') as m: + m.return_value = response_spec + op = Mock(swagger_spec=empty_swagger_spec) + assert 'Monday' == unmarshal_response(response, op) + + def test_skips_validation(empty_swagger_spec, response_spec): empty_swagger_spec.config['validate_responses'] = False response = Mock( spec=IncomingResponse, status_code=200, + headers={'content-type': APP_JSON}, json=Mock(return_value='Monday')) with patch('bravado_core.response.validate_schema_object') as val_schem: @@ -62,6 +78,7 @@ def test_performs_validation(empty_swagger_spec, response_spec): response = Mock( spec=IncomingResponse, status_code=200, + headers={'content-type': APP_JSON}, json=Mock(return_value='Monday')) with patch('bravado_core.response.validate_schema_object') as val_schem: