From 613319d78d9e71b168b5f42f5f915b7087a2545e Mon Sep 17 00:00:00 2001 From: p1c2u Date: Wed, 20 Sep 2023 11:06:49 +0000 Subject: [PATCH] aiohttp request host_url include scheme fix --- openapi_core/contrib/aiohttp/requests.py | 2 +- .../aiohttp/data/v3.0/aiohttp_factory.yaml | 2 +- .../aiohttp/test_aiohttp_validation.py | 46 ++++++++++++++++++- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/openapi_core/contrib/aiohttp/requests.py b/openapi_core/contrib/aiohttp/requests.py index 49c107b4..232540f8 100644 --- a/openapi_core/contrib/aiohttp/requests.py +++ b/openapi_core/contrib/aiohttp/requests.py @@ -34,7 +34,7 @@ def __init__(self, request: web.Request, *, body: str | None): @property def host_url(self) -> str: - return self.request.url.host or "" + return f"{self.request.url.scheme}://{self.request.url.host}" @property def path(self) -> str: diff --git a/tests/integration/contrib/aiohttp/data/v3.0/aiohttp_factory.yaml b/tests/integration/contrib/aiohttp/data/v3.0/aiohttp_factory.yaml index 38db3548..4de7fac0 100644 --- a/tests/integration/contrib/aiohttp/data/v3.0/aiohttp_factory.yaml +++ b/tests/integration/contrib/aiohttp/data/v3.0/aiohttp_factory.yaml @@ -3,7 +3,7 @@ info: title: Basic OpenAPI specification used with starlette integration tests version: "0.1" servers: - - url: '/' + - url: 'http://localhost' description: 'testing' paths: '/browse/{id}/': diff --git a/tests/integration/contrib/aiohttp/test_aiohttp_validation.py b/tests/integration/contrib/aiohttp/test_aiohttp_validation.py index 99231bb4..134e530d 100644 --- a/tests/integration/contrib/aiohttp/test_aiohttp_validation.py +++ b/tests/integration/contrib/aiohttp/test_aiohttp_validation.py @@ -14,7 +14,10 @@ async def test_aiohttp_integration_valid_input(client: TestClient): given_query_string = { "q": "string", } - given_headers = {"content-type": "application/json"} + given_headers = { + "content-type": "application/json", + "Host": "localhost", + } given_data = {"param1": 1} expected_status_code = 200 expected_response_data = {"data": "data"} @@ -31,6 +34,42 @@ async def test_aiohttp_integration_valid_input(client: TestClient): assert response_data == expected_response_data +async def test_aiohttp_integration_invalid_server(client: TestClient, request): + if "no_validation" in request.node.name: + pytest.skip("No validation for given handler.") + # Given + given_query_string = { + "q": "string", + } + given_headers = { + "content-type": "application/json", + "Host": "petstore.swagger.io", + } + given_data = {"param1": 1} + expected_status_code = 400 + expected_response_data = { + "errors": [ + { + "message": ( + "Server not found for " + "http://petstore.swagger.io/browse/12/" + ), + } + ] + } + # When + response = await client.post( + "/browse/12/", + params=given_query_string, + json=given_data, + headers=given_headers, + ) + response_data = await response.json() + # Then + assert response.status == expected_status_code + assert response_data == expected_response_data + + async def test_aiohttp_integration_invalid_input( client: TestClient, response_getter, request ): @@ -40,7 +79,10 @@ async def test_aiohttp_integration_invalid_input( given_query_string = { "q": "string", } - given_headers = {"content-type": "application/json"} + given_headers = { + "content-type": "application/json", + "Host": "localhost", + } given_data = {"param1": "string"} response_getter.return_value = {"data": 1} expected_status_code = 400