Merge pull request #227 from schunka/fix/requests-response-data

Fix Requests.response factory
This commit is contained in:
A 2020-03-27 09:12:20 +00:00 committed by GitHub
commit 753637d2d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 10 deletions

View file

@ -8,7 +8,7 @@ class RequestsOpenAPIResponseFactory(object):
def create(cls, response): def create(cls, response):
mimetype = response.headers.get('Content-Type') mimetype = response.headers.get('Content-Type')
return OpenAPIResponse( return OpenAPIResponse(
data=response.raw, data=response.content,
status_code=response.status_code, status_code=response.status_code,
mimetype=mimetype, mimetype=mimetype,
) )

View file

@ -6,4 +6,5 @@ falcon==2.0.0
flask flask
django==2.2.10; python_version>="3.0" django==2.2.10; python_version>="3.0"
requests==2.22.0 requests==2.22.0
responses==0.10.12
webob webob

View file

@ -40,6 +40,7 @@ tests_require =
pytest-cov pytest-cov
falcon falcon
flask flask
responses
webob webob
[options.packages.find] [options.packages.find]

View file

@ -1,7 +1,9 @@
import pytest import pytest
from requests.models import Request, Response from requests.models import Request, Response
from requests.structures import CaseInsensitiveDict from requests.structures import CaseInsensitiveDict
from six import BytesIO, b
from six.moves.urllib.parse import urljoin, parse_qs from six.moves.urllib.parse import urljoin, parse_qs
from urllib3.response import HTTPResponse
@pytest.fixture @pytest.fixture
@ -24,11 +26,13 @@ def request_factory():
def response_factory(): def response_factory():
def create_response( def create_response(
data, status_code=200, content_type='application/json'): data, status_code=200, content_type='application/json'):
fp = BytesIO(b(data))
raw = HTTPResponse(fp, preload_content=False)
resp = Response() resp = Response()
resp.headers = CaseInsensitiveDict({ resp.headers = CaseInsensitiveDict({
'Content-Type': content_type, 'Content-Type': content_type,
}) })
resp.status_code = status_code resp.status_code = status_code
resp.raw = data resp.raw = raw
return resp return resp
return create_response return create_response

View file

@ -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

View file

@ -1,4 +1,6 @@
import pytest import pytest
import requests
import responses
from openapi_core.contrib.requests import ( from openapi_core.contrib.requests import (
RequestsOpenAPIRequest, RequestsOpenAPIResponse, RequestsOpenAPIRequest, RequestsOpenAPIResponse,
@ -15,21 +17,25 @@ class TestFlaskOpenAPIValidation(object):
specfile = 'contrib/requests/data/v3.0/requests_factory.yaml' specfile = 'contrib/requests/data/v3.0/requests_factory.yaml'
return create_spec(factory.spec_from_file(specfile)) return create_spec(factory.spec_from_file(specfile))
def test_response_validator_path_pattern(self, @responses.activate
spec, def test_response_validator_path_pattern(self, spec):
request_factory, responses.add(
response_factory): responses.GET, 'http://localhost/browse/12/',
json={"data": "data"}, status=200)
validator = ResponseValidator(spec) 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) openapi_request = RequestsOpenAPIRequest(request)
response = response_factory('{"data": "data"}', status_code=200)
openapi_response = RequestsOpenAPIResponse(response) openapi_response = RequestsOpenAPIResponse(response)
result = validator.validate(openapi_request, openapi_response) result = validator.validate(openapi_request, openapi_response)
assert not result.errors 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) validator = RequestValidator(spec)
request = request_factory('GET', '/browse/12/', subdomain='kb') request = requests.Request('GET', 'http://localhost/browse/12/')
openapi_request = RequestsOpenAPIRequest(request) openapi_request = RequestsOpenAPIRequest(request)
result = validator.validate(openapi_request) result = validator.validate(openapi_request)
assert not result.errors assert not result.errors