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

Support absolute url to override base url #10074

Merged
merged 16 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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: 2 additions & 0 deletions CHANGES/10074.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added support for overriding the base URL with an absolute one in client sessions
-- by :user:`vivodi`.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Arseny Timoniq
Artem Yushkovskiy
Arthur Darcet
Austin Scola
Bai Haoran
Ben Bader
Ben Greiner
Ben Kallus
Expand Down
7 changes: 2 additions & 5 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,8 @@ def request(

def _build_url(self, str_or_url: StrOrURL) -> URL:
url = URL(str_or_url)
if self._base_url is None:
return url
else:
assert not url.absolute
return self._base_url.join(url)
need_join = self._base_url and not url.absolute
return self._base_url.join(url) if need_join else url
vivodi marked this conversation as resolved.
Show resolved Hide resolved

async def _request(
self,
Expand Down
4 changes: 4 additions & 0 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ The client session supports the context manager protocol for self closing.

.. versionadded:: 3.8

.. versionchanged:: 3.12

Added support for overriding the base URL with an absolute one in client sessions.

:param aiohttp.BaseConnector connector: BaseConnector
sub-class instance to support connection pooling.

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