mirror of
https://github.com/correl/openapi-core.git
synced 2024-11-24 19:19:56 +00:00
use prepared request tests
This commit is contained in:
parent
5a91425c68
commit
f316b81ba3
4 changed files with 58 additions and 13 deletions
|
@ -26,11 +26,10 @@ class RequestsOpenAPIRequestFactory(object):
|
||||||
method = request.method.lower()
|
method = request.method.lower()
|
||||||
|
|
||||||
# Cookies
|
# Cookies
|
||||||
|
cookie = {}
|
||||||
if request._cookies is not None:
|
if request._cookies is not None:
|
||||||
# cookies are stored in a cookiejar object
|
# cookies are stored in a cookiejar object
|
||||||
cookie = request._cookies.get_dict()
|
cookie = request._cookies.get_dict()
|
||||||
else:
|
|
||||||
cookie = {}
|
|
||||||
|
|
||||||
# Preparing a request formats the URL with params, strip them out again
|
# Preparing a request formats the URL with params, strip them out again
|
||||||
o = urlparse(request.url)
|
o = urlparse(request.url)
|
||||||
|
|
|
@ -13,7 +13,25 @@ paths:
|
||||||
description: the ID of the resource to retrieve
|
description: the ID of the resource to retrieve
|
||||||
schema:
|
schema:
|
||||||
type: integer
|
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:
|
responses:
|
||||||
200:
|
200:
|
||||||
description: Return the resource.
|
description: Return the resource.
|
||||||
|
|
|
@ -15,6 +15,7 @@ class TestRequestsOpenAPIRequest(object):
|
||||||
query = ImmutableMultiDict([])
|
query = ImmutableMultiDict([])
|
||||||
headers = request.headers
|
headers = request.headers
|
||||||
cookies = {}
|
cookies = {}
|
||||||
|
prepared = request.prepare()
|
||||||
assert openapi_request.parameters == RequestParameters(
|
assert openapi_request.parameters == RequestParameters(
|
||||||
path=path,
|
path=path,
|
||||||
query=query,
|
query=query,
|
||||||
|
@ -23,7 +24,7 @@ class TestRequestsOpenAPIRequest(object):
|
||||||
)
|
)
|
||||||
assert openapi_request.method == request.method.lower()
|
assert openapi_request.method == request.method.lower()
|
||||||
assert openapi_request.full_url_pattern == 'http://localhost/'
|
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'
|
assert openapi_request.mimetype == 'application/json'
|
||||||
|
|
||||||
def test_multiple_values(self, request_factory, request):
|
def test_multiple_values(self, request_factory, request):
|
||||||
|
@ -44,9 +45,10 @@ class TestRequestsOpenAPIRequest(object):
|
||||||
header=headers,
|
header=headers,
|
||||||
cookie=cookies,
|
cookie=cookies,
|
||||||
)
|
)
|
||||||
|
prepared = request.prepare()
|
||||||
assert openapi_request.method == request.method.lower()
|
assert openapi_request.method == request.method.lower()
|
||||||
assert openapi_request.full_url_pattern == 'http://localhost/'
|
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'
|
assert openapi_request.mimetype == 'application/json'
|
||||||
|
|
||||||
def test_url_rule(self, request_factory, request):
|
def test_url_rule(self, request_factory, request):
|
||||||
|
@ -57,7 +59,9 @@ class TestRequestsOpenAPIRequest(object):
|
||||||
# empty when not bound to spec
|
# empty when not bound to spec
|
||||||
path = {}
|
path = {}
|
||||||
query = ImmutableMultiDict([])
|
query = ImmutableMultiDict([])
|
||||||
headers = request.headers
|
headers = (
|
||||||
|
('Content-Type', 'application/json'),
|
||||||
|
)
|
||||||
cookies = {}
|
cookies = {}
|
||||||
assert openapi_request.parameters == RequestParameters(
|
assert openapi_request.parameters == RequestParameters(
|
||||||
path=path,
|
path=path,
|
||||||
|
@ -65,8 +69,9 @@ class TestRequestsOpenAPIRequest(object):
|
||||||
header=headers,
|
header=headers,
|
||||||
cookie=cookies,
|
cookie=cookies,
|
||||||
)
|
)
|
||||||
|
prepared = request.prepare()
|
||||||
assert openapi_request.method == request.method.lower()
|
assert openapi_request.method == request.method.lower()
|
||||||
assert openapi_request.full_url_pattern == \
|
assert openapi_request.full_url_pattern == \
|
||||||
'http://localhost/browse/12/'
|
'http://localhost/browse/12/'
|
||||||
assert openapi_request.body == request.data
|
assert openapi_request.body == prepared.body
|
||||||
assert openapi_request.mimetype == 'application/json'
|
assert openapi_request.mimetype == 'application/json'
|
||||||
|
|
|
@ -10,7 +10,7 @@ from openapi_core.validation.request.validators import RequestValidator
|
||||||
from openapi_core.validation.response.validators import ResponseValidator
|
from openapi_core.validation.response.validators import ResponseValidator
|
||||||
|
|
||||||
|
|
||||||
class TestFlaskOpenAPIValidation(object):
|
class TestRequestsOpenAPIValidation(object):
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def spec(self, factory):
|
def spec(self, factory):
|
||||||
|
@ -20,10 +20,16 @@ class TestFlaskOpenAPIValidation(object):
|
||||||
@responses.activate
|
@responses.activate
|
||||||
def test_response_validator_path_pattern(self, spec):
|
def test_response_validator_path_pattern(self, spec):
|
||||||
responses.add(
|
responses.add(
|
||||||
responses.GET, 'http://localhost/browse/12/',
|
responses.POST, 'http://localhost/browse/12/?q=string',
|
||||||
json={"data": "data"}, status=200)
|
json={"data": "data"}, status=200, match_querystring=True,
|
||||||
|
)
|
||||||
validator = ResponseValidator(spec)
|
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()
|
request_prepared = request.prepare()
|
||||||
session = requests.Session()
|
session = requests.Session()
|
||||||
response = session.send(request_prepared)
|
response = session.send(request_prepared)
|
||||||
|
@ -32,10 +38,27 @@ class TestFlaskOpenAPIValidation(object):
|
||||||
result = validator.validate(openapi_request, openapi_response)
|
result = validator.validate(openapi_request, openapi_response)
|
||||||
assert not result.errors
|
assert not result.errors
|
||||||
|
|
||||||
@responses.activate
|
|
||||||
def test_request_validator_path_pattern(self, spec):
|
def test_request_validator_path_pattern(self, spec):
|
||||||
validator = RequestValidator(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)
|
openapi_request = RequestsOpenAPIRequest(request)
|
||||||
result = validator.validate(openapi_request)
|
result = validator.validate(openapi_request)
|
||||||
assert not result.errors
|
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
|
||||||
|
|
Loading…
Reference in a new issue