Merge pull request #46 from HoverHell/paramlocations

Request headers and cookies parameter parsing fix
This commit is contained in:
A 2018-08-13 20:21:46 +01:00 committed by GitHub
commit ac797bcf2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 232 additions and 18 deletions

View file

@ -6,7 +6,7 @@ from openapi_core.validation.models import BaseValidationResult
class RequestParameters(dict):
valid_locations = ['path', 'query', 'headers', 'cookies']
valid_locations = ['path', 'query', 'header', 'cookie']
def __getitem__(self, location):
self.validate_location(location)

View file

@ -31,8 +31,8 @@ class FlaskOpenAPIRequest(BaseOpenAPIRequest):
return {
'path': self.request.view_args,
'query': self.request.args,
'headers': self.request.headers,
'cookies': self.request.cookies,
'header': self.request.headers,
'cookie': self.request.cookies,
}
@property

View file

@ -74,6 +74,19 @@ paths:
operationId: createPets
tags:
- pets
parameters:
- name: api_key
in: header
schema:
type: integer
format: int32
required: true
- name: user
in: cookie
schema:
type: integer
format: int32
required: true
requestBody:
required: true
content:

View file

@ -397,15 +397,29 @@ class TestPetstore(object):
}
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest(
host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data,
headers=headers, cookies=cookies,
)
parameters = request.get_parameters(spec)
assert parameters == {}
assert parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
body = request.get_body(spec)
@ -443,15 +457,29 @@ class TestPetstore(object):
}
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest(
host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data,
headers=headers, cookies=cookies,
)
parameters = request.get_parameters(spec)
assert parameters == {}
assert parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
body = request.get_body(spec)
@ -489,15 +517,29 @@ class TestPetstore(object):
}
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest(
host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data,
headers=headers, cookies=cookies,
)
parameters = request.get_parameters(spec)
assert parameters == {}
assert parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
body = request.get_body(spec)
@ -523,15 +565,29 @@ class TestPetstore(object):
'alias': alias,
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest(
host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data,
headers=headers, cookies=cookies,
)
parameters = request.get_parameters(spec)
assert parameters == {}
assert parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
with pytest.raises(NoOneOfSchema):
request.get_body(spec)
@ -548,15 +604,29 @@ class TestPetstore(object):
}
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest(
host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data,
headers=headers, cookies=cookies,
)
parameters = request.get_parameters(spec)
assert parameters == {}
assert parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
body = request.get_body(spec)
@ -575,19 +645,101 @@ class TestPetstore(object):
'tag': 'cats',
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest(
host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data, mimetype='text/html',
headers=headers, cookies=cookies,
)
parameters = request.get_parameters(spec)
assert parameters == {}
assert parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
with pytest.raises(InvalidContentType):
request.get_body(spec)
def test_post_pets_missing_cookie(self, spec, spec_dict):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
pet_name = 'Cat'
pet_healthy = True
data_json = {
'name': pet_name,
'ears': {
'healthy': pet_healthy,
}
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
request = MockRequest(
host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data,
headers=headers,
)
with pytest.raises(MissingRequiredParameter):
request.get_parameters(spec)
body = request.get_body(spec)
schemas = spec_dict['components']['schemas']
pet_model = schemas['PetCreate']['x-model']
assert body.__class__.__name__ == pet_model
assert body.name == pet_name
assert not hasattr(body, 'tag')
assert not hasattr(body, 'address')
def test_post_pets_missing_header(self, spec, spec_dict):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
pet_name = 'Cat'
pet_healthy = True
data_json = {
'name': pet_name,
'ears': {
'healthy': pet_healthy,
}
}
data = json.dumps(data_json)
cookies = {
'user': '123',
}
request = MockRequest(
host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data,
cookies=cookies,
)
with pytest.raises(MissingRequiredParameter):
request.get_parameters(spec)
body = request.get_body(spec)
schemas = spec_dict['components']['schemas']
pet_model = schemas['PetCreate']['x-model']
assert body.__class__.__name__ == pet_model
assert body.name == pet_name
assert not hasattr(body, 'tag')
assert not hasattr(body, 'address')
def test_post_pets_raises_invalid_server_error(self, spec):
host_url = 'http://flowerstore.swagger.io/v1'
path_pattern = '/v1/pets'
@ -596,10 +748,17 @@ class TestPetstore(object):
'tag': 'cats',
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest(
host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data, mimetype='text/html',
headers=headers, cookies=cookies,
)
with pytest.raises(InvalidServer):

View file

@ -86,9 +86,16 @@ class TestRequestValidator(object):
}
def test_missing_body(self, validator):
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest(
self.host_url, 'post', '/v1/pets',
path_pattern='/v1/pets',
headers=headers, cookies=cookies,
)
result = validator.validate(request)
@ -96,12 +103,26 @@ class TestRequestValidator(object):
assert len(result.errors) == 1
assert type(result.errors[0]) == MissingRequestBody
assert result.body is None
assert result.parameters == {}
assert result.parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
def test_invalid_content_type(self, validator):
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest(
self.host_url, 'post', '/v1/pets',
path_pattern='/v1/pets', mimetype='text/csv',
headers=headers, cookies=cookies,
)
result = validator.validate(request)
@ -109,7 +130,14 @@ class TestRequestValidator(object):
assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidContentType
assert result.body is None
assert result.parameters == {}
assert result.parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
def test_post_pets(self, validator, spec_dict):
pet_name = 'Cat'
@ -129,15 +157,29 @@ class TestRequestValidator(object):
}
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
}
cookies = {
'user': '123',
}
request = MockRequest(
self.host_url, 'post', '/v1/pets',
path_pattern='/v1/pets', data=data,
headers=headers, cookies=cookies,
)
result = validator.validate(request)
assert result.errors == []
assert result.parameters == {}
assert result.parameters == {
'header': {
'api_key': 12345,
},
'cookie': {
'user': 123,
},
}
schemas = spec_dict['components']['schemas']
pet_model = schemas['PetCreate']['x-model']

View file

@ -62,8 +62,8 @@ class TestFlaskOpenAPIRequest(object):
assert openapi_request.parameters == {
'path': path,
'query': query,
'headers': headers,
'cookies': cookies,
'header': headers,
'cookie': cookies,
}
assert openapi_request.host_url == request.host_url
assert openapi_request.path == request.path
@ -87,8 +87,8 @@ class TestFlaskOpenAPIRequest(object):
assert openapi_request.parameters == {
'path': path,
'query': query,
'headers': headers,
'cookies': cookies,
'header': headers,
'cookie': cookies,
}
assert openapi_request.host_url == request.host_url
assert openapi_request.path == request.path
@ -109,8 +109,8 @@ class TestFlaskOpenAPIRequest(object):
assert openapi_request.parameters == {
'path': path,
'query': query,
'headers': headers,
'cookies': cookies,
'header': headers,
'cookie': cookies,
}
assert openapi_request.host_url == request.host_url
assert openapi_request.path == request.path