diff --git a/openapi_core/validation/request/models.py b/openapi_core/validation/request/models.py index e7d57cf..55d4887 100644 --- a/openapi_core/validation/request/models.py +++ b/openapi_core/validation/request/models.py @@ -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) diff --git a/openapi_core/wrappers/flask.py b/openapi_core/wrappers/flask.py index 84bf093..ab91665 100644 --- a/openapi_core/wrappers/flask.py +++ b/openapi_core/wrappers/flask.py @@ -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 diff --git a/requirements_dev.txt b/requirements_dev.txt index cbc4f86..7d4afaa 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -1,5 +1,5 @@ -mock -pytest +mock==2.0.0 +pytest==3.5.0 pytest-flake8 -pytest-cov -flask +pytest-cov==2.5.1 +flask \ No newline at end of file diff --git a/tests/integration/data/v3.0/petstore.yaml b/tests/integration/data/v3.0/petstore.yaml index e573a4e..4a91d23 100644 --- a/tests/integration/data/v3.0/petstore.yaml +++ b/tests/integration/data/v3.0/petstore.yaml @@ -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: diff --git a/tests/integration/test_petstore.py b/tests/integration/test_petstore.py index 0f3bbb7..d6b8a5d 100644 --- a/tests/integration/test_petstore.py +++ b/tests/integration/test_petstore.py @@ -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): diff --git a/tests/integration/test_validators.py b/tests/integration/test_validators.py index d5e6aae..43c185a 100644 --- a/tests/integration/test_validators.py +++ b/tests/integration/test_validators.py @@ -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'] diff --git a/tests/integration/test_wrappers.py b/tests/integration/test_wrappers.py index 29a357d..ff0c157 100644 --- a/tests/integration/test_wrappers.py +++ b/tests/integration/test_wrappers.py @@ -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 diff --git a/tests/unit/schema/test_paramters.py b/tests/unit/schema/test_parameters.py similarity index 100% rename from tests/unit/schema/test_paramters.py rename to tests/unit/schema/test_parameters.py