Skip to content

Commit 37bfb36

Browse files
authored
Merge pull request #673 from python-openapi/fix/aiohttp-request-host-url-fix
aiohttp request host_url include scheme fix
2 parents e0af0df + 613319d commit 37bfb36

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

Diff for: openapi_core/contrib/aiohttp/requests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, request: web.Request, *, body: str | None):
3434

3535
@property
3636
def host_url(self) -> str:
37-
return self.request.url.host or ""
37+
return f"{self.request.url.scheme}://{self.request.url.host}"
3838

3939
@property
4040
def path(self) -> str:

Diff for: tests/integration/contrib/aiohttp/data/v3.0/aiohttp_factory.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ info:
33
title: Basic OpenAPI specification used with starlette integration tests
44
version: "0.1"
55
servers:
6-
- url: '/'
6+
- url: 'http://localhost'
77
description: 'testing'
88
paths:
99
'/browse/{id}/':

Diff for: tests/integration/contrib/aiohttp/test_aiohttp_validation.py

+44-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ async def test_aiohttp_integration_valid_input(client: TestClient):
1414
given_query_string = {
1515
"q": "string",
1616
}
17-
given_headers = {"content-type": "application/json"}
17+
given_headers = {
18+
"content-type": "application/json",
19+
"Host": "localhost",
20+
}
1821
given_data = {"param1": 1}
1922
expected_status_code = 200
2023
expected_response_data = {"data": "data"}
@@ -31,6 +34,42 @@ async def test_aiohttp_integration_valid_input(client: TestClient):
3134
assert response_data == expected_response_data
3235

3336

37+
async def test_aiohttp_integration_invalid_server(client: TestClient, request):
38+
if "no_validation" in request.node.name:
39+
pytest.skip("No validation for given handler.")
40+
# Given
41+
given_query_string = {
42+
"q": "string",
43+
}
44+
given_headers = {
45+
"content-type": "application/json",
46+
"Host": "petstore.swagger.io",
47+
}
48+
given_data = {"param1": 1}
49+
expected_status_code = 400
50+
expected_response_data = {
51+
"errors": [
52+
{
53+
"message": (
54+
"Server not found for "
55+
"http://petstore.swagger.io/browse/12/"
56+
),
57+
}
58+
]
59+
}
60+
# When
61+
response = await client.post(
62+
"/browse/12/",
63+
params=given_query_string,
64+
json=given_data,
65+
headers=given_headers,
66+
)
67+
response_data = await response.json()
68+
# Then
69+
assert response.status == expected_status_code
70+
assert response_data == expected_response_data
71+
72+
3473
async def test_aiohttp_integration_invalid_input(
3574
client: TestClient, response_getter, request
3675
):
@@ -40,7 +79,10 @@ async def test_aiohttp_integration_invalid_input(
4079
given_query_string = {
4180
"q": "string",
4281
}
43-
given_headers = {"content-type": "application/json"}
82+
given_headers = {
83+
"content-type": "application/json",
84+
"Host": "localhost",
85+
}
4486
given_data = {"param1": "string"}
4587
response_getter.return_value = {"data": 1}
4688
expected_status_code = 400

0 commit comments

Comments
 (0)