Skip to content

aiohttp request host_url include scheme fix #673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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