From f316b81ba395d09ff0b9b3fd61a7dae1925e22a9 Mon Sep 17 00:00:00 2001 From: p1c2u Date: Wed, 24 Mar 2021 12:56:55 +0000 Subject: [PATCH] use prepared request tests --- openapi_core/contrib/requests/requests.py | 3 +- .../requests/data/v3.0/requests_factory.yaml | 20 ++++++++++- .../requests/test_requests_requests.py | 13 ++++--- .../requests/test_requests_validation.py | 35 +++++++++++++++---- 4 files changed, 58 insertions(+), 13 deletions(-) diff --git a/openapi_core/contrib/requests/requests.py b/openapi_core/contrib/requests/requests.py index c25ad67..b2eeb64 100644 --- a/openapi_core/contrib/requests/requests.py +++ b/openapi_core/contrib/requests/requests.py @@ -26,11 +26,10 @@ class RequestsOpenAPIRequestFactory(object): method = request.method.lower() # Cookies + cookie = {} if request._cookies is not None: # cookies are stored in a cookiejar object cookie = request._cookies.get_dict() - else: - cookie = {} # Preparing a request formats the URL with params, strip them out again o = urlparse(request.url) diff --git a/tests/integration/contrib/requests/data/v3.0/requests_factory.yaml b/tests/integration/contrib/requests/data/v3.0/requests_factory.yaml index abef7eb..c3f73cd 100644 --- a/tests/integration/contrib/requests/data/v3.0/requests_factory.yaml +++ b/tests/integration/contrib/requests/data/v3.0/requests_factory.yaml @@ -13,7 +13,25 @@ paths: description: the ID of the resource to retrieve schema: type: integer - get: + - name: q + in: query + required: true + description: query key + schema: + type: string + post: + requestBody: + description: request data + required: True + content: + application/json: + schema: + type: object + required: + - param1 + properties: + param1: + type: integer responses: 200: description: Return the resource. diff --git a/tests/integration/contrib/requests/test_requests_requests.py b/tests/integration/contrib/requests/test_requests_requests.py index 45e0258..bd0556b 100644 --- a/tests/integration/contrib/requests/test_requests_requests.py +++ b/tests/integration/contrib/requests/test_requests_requests.py @@ -15,6 +15,7 @@ class TestRequestsOpenAPIRequest(object): query = ImmutableMultiDict([]) headers = request.headers cookies = {} + prepared = request.prepare() assert openapi_request.parameters == RequestParameters( path=path, query=query, @@ -23,7 +24,7 @@ class TestRequestsOpenAPIRequest(object): ) assert openapi_request.method == request.method.lower() assert openapi_request.full_url_pattern == 'http://localhost/' - assert openapi_request.body == request.data + assert openapi_request.body == prepared.body assert openapi_request.mimetype == 'application/json' def test_multiple_values(self, request_factory, request): @@ -44,9 +45,10 @@ class TestRequestsOpenAPIRequest(object): header=headers, cookie=cookies, ) + prepared = request.prepare() assert openapi_request.method == request.method.lower() assert openapi_request.full_url_pattern == 'http://localhost/' - assert openapi_request.body == request.data + assert openapi_request.body == prepared.body assert openapi_request.mimetype == 'application/json' def test_url_rule(self, request_factory, request): @@ -57,7 +59,9 @@ class TestRequestsOpenAPIRequest(object): # empty when not bound to spec path = {} query = ImmutableMultiDict([]) - headers = request.headers + headers = ( + ('Content-Type', 'application/json'), + ) cookies = {} assert openapi_request.parameters == RequestParameters( path=path, @@ -65,8 +69,9 @@ class TestRequestsOpenAPIRequest(object): header=headers, cookie=cookies, ) + prepared = request.prepare() assert openapi_request.method == request.method.lower() assert openapi_request.full_url_pattern == \ 'http://localhost/browse/12/' - assert openapi_request.body == request.data + assert openapi_request.body == prepared.body assert openapi_request.mimetype == 'application/json' diff --git a/tests/integration/contrib/requests/test_requests_validation.py b/tests/integration/contrib/requests/test_requests_validation.py index 6812d93..997a1a4 100644 --- a/tests/integration/contrib/requests/test_requests_validation.py +++ b/tests/integration/contrib/requests/test_requests_validation.py @@ -10,7 +10,7 @@ from openapi_core.validation.request.validators import RequestValidator from openapi_core.validation.response.validators import ResponseValidator -class TestFlaskOpenAPIValidation(object): +class TestRequestsOpenAPIValidation(object): @pytest.fixture def spec(self, factory): @@ -20,10 +20,16 @@ class TestFlaskOpenAPIValidation(object): @responses.activate def test_response_validator_path_pattern(self, spec): responses.add( - responses.GET, 'http://localhost/browse/12/', - json={"data": "data"}, status=200) + responses.POST, 'http://localhost/browse/12/?q=string', + json={"data": "data"}, status=200, match_querystring=True, + ) validator = ResponseValidator(spec) - request = requests.Request('GET', 'http://localhost/browse/12/') + request = requests.Request( + 'POST', 'http://localhost/browse/12/', + params={'q': 'string'}, + headers={'content-type': 'application/json'}, + json={'param1': 1}, + ) request_prepared = request.prepare() session = requests.Session() response = session.send(request_prepared) @@ -32,10 +38,27 @@ class TestFlaskOpenAPIValidation(object): result = validator.validate(openapi_request, openapi_response) assert not result.errors - @responses.activate def test_request_validator_path_pattern(self, spec): validator = RequestValidator(spec) - request = requests.Request('GET', 'http://localhost/browse/12/') + request = requests.Request( + 'POST', 'http://localhost/browse/12/', + params={'q': 'string'}, + headers={'content-type': 'application/json'}, + json={'param1': 1}, + ) openapi_request = RequestsOpenAPIRequest(request) result = validator.validate(openapi_request) assert not result.errors + + def test_request_validator_prepared_request(self, spec): + validator = RequestValidator(spec) + request = requests.Request( + 'POST', 'http://localhost/browse/12/', + params={'q': 'string'}, + headers={'content-type': 'application/json'}, + json={'param1': 1}, + ) + request_prepared = request.prepare() + openapi_request = RequestsOpenAPIRequest(request_prepared) + result = validator.validate(openapi_request) + assert not result.errors