From d79ad01e2fd2f0afb3f1d95d403a5a323aa4f497 Mon Sep 17 00:00:00 2001 From: Ales Jirasek <6615474+schunka@users.noreply.github.com> Date: Wed, 25 Mar 2020 12:29:53 +0100 Subject: [PATCH 1/2] Update responses.py --- openapi_core/contrib/requests/responses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi_core/contrib/requests/responses.py b/openapi_core/contrib/requests/responses.py index 0546051..502d6b9 100644 --- a/openapi_core/contrib/requests/responses.py +++ b/openapi_core/contrib/requests/responses.py @@ -8,7 +8,7 @@ class RequestsOpenAPIResponseFactory(object): def create(cls, response): mimetype = response.headers.get('Content-Type') return OpenAPIResponse( - data=response.raw, + data=response.content, status_code=response.status_code, mimetype=mimetype, ) From 926a17b9b2f2eaaaf0f875ed0807b0677f37b623 Mon Sep 17 00:00:00 2001 From: p1c2u Date: Fri, 27 Mar 2020 08:58:45 +0000 Subject: [PATCH 2/2] Update requests contrib tests --- requirements_dev.txt | 1 + setup.cfg | 1 + .../integration/contrib/requests/conftest.py | 6 ++++- .../requests/test_requests_responses.py | 14 ++++++++++++ .../requests/test_requests_validation.py | 22 ++++++++++++------- 5 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 tests/integration/contrib/requests/test_requests_responses.py diff --git a/requirements_dev.txt b/requirements_dev.txt index 1f282d6..38acad6 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -6,4 +6,5 @@ falcon==2.0.0 flask django==2.2.10; python_version>="3.0" requests==2.22.0 +responses==0.10.12 webob diff --git a/setup.cfg b/setup.cfg index ba0da4b..e58fefe 100644 --- a/setup.cfg +++ b/setup.cfg @@ -40,6 +40,7 @@ tests_require = pytest-cov falcon flask + responses webob [options.packages.find] diff --git a/tests/integration/contrib/requests/conftest.py b/tests/integration/contrib/requests/conftest.py index 00aac4f..fc62bfc 100644 --- a/tests/integration/contrib/requests/conftest.py +++ b/tests/integration/contrib/requests/conftest.py @@ -1,7 +1,9 @@ import pytest from requests.models import Request, Response from requests.structures import CaseInsensitiveDict +from six import BytesIO, b from six.moves.urllib.parse import urljoin, parse_qs +from urllib3.response import HTTPResponse @pytest.fixture @@ -24,11 +26,13 @@ def request_factory(): def response_factory(): def create_response( data, status_code=200, content_type='application/json'): + fp = BytesIO(b(data)) + raw = HTTPResponse(fp, preload_content=False) resp = Response() resp.headers = CaseInsensitiveDict({ 'Content-Type': content_type, }) resp.status_code = status_code - resp.raw = data + resp.raw = raw return resp return create_response diff --git a/tests/integration/contrib/requests/test_requests_responses.py b/tests/integration/contrib/requests/test_requests_responses.py new file mode 100644 index 0000000..9ad45fd --- /dev/null +++ b/tests/integration/contrib/requests/test_requests_responses.py @@ -0,0 +1,14 @@ +from openapi_core.contrib.requests import RequestsOpenAPIResponse + + +class TestRequestsOpenAPIResponse(object): + + def test_invalid_server(self, response_factory): + response = response_factory('Not Found', status_code=404) + + openapi_response = RequestsOpenAPIResponse(response) + + assert openapi_response.data == response.content + assert openapi_response.status_code == response.status_code + mimetype = response.headers.get('Content-Type') + assert openapi_response.mimetype == mimetype diff --git a/tests/integration/contrib/requests/test_requests_validation.py b/tests/integration/contrib/requests/test_requests_validation.py index 7dc0355..6812d93 100644 --- a/tests/integration/contrib/requests/test_requests_validation.py +++ b/tests/integration/contrib/requests/test_requests_validation.py @@ -1,4 +1,6 @@ import pytest +import requests +import responses from openapi_core.contrib.requests import ( RequestsOpenAPIRequest, RequestsOpenAPIResponse, @@ -15,21 +17,25 @@ class TestFlaskOpenAPIValidation(object): specfile = 'contrib/requests/data/v3.0/requests_factory.yaml' return create_spec(factory.spec_from_file(specfile)) - def test_response_validator_path_pattern(self, - spec, - request_factory, - response_factory): + @responses.activate + def test_response_validator_path_pattern(self, spec): + responses.add( + responses.GET, 'http://localhost/browse/12/', + json={"data": "data"}, status=200) validator = ResponseValidator(spec) - request = request_factory('GET', '/browse/12/', subdomain='kb') + request = requests.Request('GET', 'http://localhost/browse/12/') + request_prepared = request.prepare() + session = requests.Session() + response = session.send(request_prepared) openapi_request = RequestsOpenAPIRequest(request) - response = response_factory('{"data": "data"}', status_code=200) openapi_response = RequestsOpenAPIResponse(response) result = validator.validate(openapi_request, openapi_response) assert not result.errors - def test_request_validator_path_pattern(self, spec, request_factory): + @responses.activate + def test_request_validator_path_pattern(self, spec): validator = RequestValidator(spec) - request = request_factory('GET', '/browse/12/', subdomain='kb') + request = requests.Request('GET', 'http://localhost/browse/12/') openapi_request = RequestsOpenAPIRequest(request) result = validator.validate(openapi_request) assert not result.errors