mirror of
https://github.com/correl/openapi-core.git
synced 2024-11-22 03:00:10 +00:00
Merge pull request #227 from schunka/fix/requests-response-data
Fix Requests.response factory
This commit is contained in:
commit
753637d2d0
6 changed files with 36 additions and 10 deletions
|
@ -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,
|
||||
)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -40,6 +40,7 @@ tests_require =
|
|||
pytest-cov
|
||||
falcon
|
||||
flask
|
||||
responses
|
||||
webob
|
||||
|
||||
[options.packages.find]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue