-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #336 from macisamuele/maci-response-handling-is-pe…
…rformed-in-bravado Response handling is performed in bravado
- Loading branch information
Showing
6 changed files
with
209 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# -*- coding: utf-8 -*- | ||
CONFIG_DEFAULTS = { | ||
# See the constructor of :class:`bravado.http_future.HttpFuture` for an | ||
# in depth explanation of what this means. | ||
'also_return_response': False, | ||
} | ||
|
||
REQUEST_OPTIONS_DEFAULTS = { | ||
# List of callbacks that are executed after the incoming response has been | ||
# validated and the swagger_result has been unmarshalled. | ||
# | ||
# The callback should expect two arguments: | ||
# param : incoming_response | ||
# type : subclass of class:`bravado_core.response.IncomingResponse` | ||
# param : operation | ||
# type : class:`bravado_core.operation.Operation` | ||
'response_callbacks': [], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# -*- coding: utf-8 -*- | ||
import mock | ||
import msgpack | ||
import pytest | ||
from bravado_core.content_type import APP_JSON | ||
from bravado_core.content_type import APP_MSGPACK | ||
from bravado_core.response import IncomingResponse | ||
from bravado_core.spec import Spec | ||
|
||
from bravado.http_future import unmarshal_response_inner | ||
|
||
|
||
@pytest.fixture | ||
def empty_swagger_spec(): | ||
return Spec(spec_dict={}) | ||
|
||
|
||
@pytest.fixture | ||
def response_spec(): | ||
return { | ||
'description': "Day of the week", | ||
'schema': { | ||
'type': 'string', | ||
} | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def mock_get_response_spec(): | ||
with mock.patch('bravado.http_future.get_response_spec') as m: | ||
yield m | ||
|
||
|
||
@pytest.fixture | ||
def mock_validate_schema_object(): | ||
with mock.patch('bravado.http_future.validate_schema_object') as m: | ||
yield m | ||
|
||
|
||
def test_no_content(mock_get_response_spec, empty_swagger_spec): | ||
response_spec = { | ||
'description': "I don't have a 'schema' key so I return nothing", | ||
} | ||
response = mock.Mock(spec=IncomingResponse, status_code=200) | ||
|
||
mock_get_response_spec.return_value = response_spec | ||
op = mock.Mock(swagger_spec=empty_swagger_spec) | ||
result = unmarshal_response_inner(response, op) | ||
assert result is None | ||
|
||
|
||
def test_json_content(mock_get_response_spec, empty_swagger_spec, response_spec): | ||
response = mock.Mock( | ||
spec=IncomingResponse, | ||
status_code=200, | ||
headers={'content-type': APP_JSON}, | ||
json=mock.Mock(return_value='Monday'), | ||
) | ||
|
||
mock_get_response_spec.return_value = response_spec | ||
op = mock.Mock(swagger_spec=empty_swagger_spec) | ||
assert 'Monday' == unmarshal_response_inner(response, op) | ||
|
||
|
||
def test_msgpack_content(mock_get_response_spec, empty_swagger_spec, response_spec): | ||
message = 'Monday' | ||
response = mock.Mock( | ||
spec=IncomingResponse, | ||
status_code=200, | ||
headers={'content-type': APP_MSGPACK}, | ||
raw_bytes=msgpack.dumps(message), | ||
) | ||
|
||
mock_get_response_spec.return_value = response_spec | ||
op = mock.Mock(swagger_spec=empty_swagger_spec) | ||
assert message == unmarshal_response_inner(response, op) | ||
|
||
|
||
def test_text_content(mock_get_response_spec, empty_swagger_spec, response_spec): | ||
response = mock.Mock( | ||
spec=IncomingResponse, | ||
status_code=200, | ||
headers={'content-type': 'text/plain'}, | ||
text='Monday', | ||
) | ||
|
||
mock_get_response_spec.return_value = response_spec | ||
op = mock.Mock(swagger_spec=empty_swagger_spec) | ||
assert 'Monday' == unmarshal_response_inner(response, op) | ||
|
||
|
||
def test_skips_validation(mock_validate_schema_object, mock_get_response_spec, empty_swagger_spec, response_spec): | ||
empty_swagger_spec.config['validate_responses'] = False | ||
response = mock.Mock( | ||
spec=IncomingResponse, | ||
status_code=200, | ||
headers={'content-type': APP_JSON}, | ||
json=mock.Mock(return_value='Monday'), | ||
) | ||
|
||
mock_get_response_spec.return_value = response_spec | ||
op = mock.Mock(swagger_spec=empty_swagger_spec) | ||
unmarshal_response_inner(response, op) | ||
assert mock_validate_schema_object.call_count == 0 | ||
|
||
|
||
def test_performs_validation(mock_validate_schema_object, mock_get_response_spec, empty_swagger_spec, response_spec): | ||
empty_swagger_spec.config['validate_responses'] = True | ||
response = mock.Mock( | ||
spec=IncomingResponse, | ||
status_code=200, | ||
headers={'content-type': APP_JSON}, | ||
json=mock.Mock(return_value='Monday'), | ||
) | ||
|
||
mock_get_response_spec.return_value = response_spec | ||
op = mock.Mock(swagger_spec=empty_swagger_spec) | ||
unmarshal_response_inner(response, op) | ||
assert mock_validate_schema_object.call_count == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters