Skip to content

Commit

Permalink
Merge pull request #673 from python-openapi/fix/aiohttp-request-host-…
Browse files Browse the repository at this point in the history
…url-fix

aiohttp request host_url include scheme fix
  • Loading branch information
p1c2u authored Sep 20, 2023
2 parents e0af0df + 613319d commit 37bfb36
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion openapi_core/contrib/aiohttp/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}/':
Expand Down
46 changes: 44 additions & 2 deletions tests/integration/contrib/aiohttp/test_aiohttp_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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
):
Expand All @@ -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
Expand Down

0 comments on commit 37bfb36

Please sign in to comment.