Skip to content
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

Allow URLs with paths that end with / as base_url in ClientSession #9530

Merged
merged 17 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 5 additions & 5 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,9 @@ def __init__(
else:
self._base_url = URL(base_url)
self._base_url_origin = self._base_url.origin()
assert (
self._base_url_origin == self._base_url
), "Only absolute URLs without path part are supported"
assert self._base_url.absolute, "Only absolute URLs are supported"
if self._base_url is not None:
assert self._base_url.path.endswith("/")
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved

loop = asyncio.get_running_loop()

Expand Down Expand Up @@ -415,8 +415,8 @@ def _build_url(self, str_or_url: StrOrURL) -> URL:
if self._base_url is None:
return url
else:
assert not url.absolute and url.path.startswith("/")
return self._base_url.join(url)
assert not url.absolute
return self._base_url.join(URL(url.path_qs.lstrip("/")))
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved

async def _request(
self,
Expand Down
12 changes: 12 additions & 0 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,18 @@ async def test_requote_redirect_url_default_disable() -> None:
URL("http://example.com/test"),
id="base_url=URL('http://example.com') url='/test'",
),
pytest.param(
URL("http://example.com/test1/"),
"test2",
URL("http://example.com/test1/test2"),
id="base_url=URL('http://example.com/test1/') url='test2'",
),
pytest.param(
URL("http://example.com/test1/"),
"/test2",
URL("http://example.com/test1/test2"),
id="base_url=URL('http://example.com/test1/') url='/test2'",
),
],
)
async def test_build_url_returns_expected_url(
Expand Down
Loading