From f733258ed9e5e71b7b97511f5654efd6799cac46 Mon Sep 17 00:00:00 2001 From: vivodi <103735539+vivodi@users.noreply.github.com> Date: Mon, 2 Dec 2024 22:32:24 +0800 Subject: [PATCH] Support absolute url to override base url (#10074) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) --- CHANGES/10074.feature.rst | 2 ++ CONTRIBUTORS.txt | 1 + aiohttp/client.py | 6 ++---- docs/client_reference.rst | 4 ++++ tests/test_client_session.py | 18 ++++++++++++++++++ 5 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 CHANGES/10074.feature.rst diff --git a/CHANGES/10074.feature.rst b/CHANGES/10074.feature.rst new file mode 100644 index 00000000000..d956c38af57 --- /dev/null +++ b/CHANGES/10074.feature.rst @@ -0,0 +1,2 @@ +Added support for overriding the base URL with an absolute one in client sessions +-- by :user:`vivodi`. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 5d5f922e6fb..66c706cde4f 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -54,6 +54,7 @@ Arseny Timoniq Artem Yushkovskiy Arthur Darcet Austin Scola +Bai Haoran Ben Bader Ben Greiner Ben Kallus diff --git a/aiohttp/client.py b/aiohttp/client.py index 16c8c749ea0..bc6c0aaaa5d 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -414,11 +414,9 @@ 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 + if self._base_url and not url.absolute: return self._base_url.join(url) + return url async def _request( self, diff --git a/docs/client_reference.rst b/docs/client_reference.rst index 937e5e8afbb..9a76bfbd14b 100644 --- a/docs/client_reference.rst +++ b/docs/client_reference.rst @@ -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. diff --git a/tests/test_client_session.py b/tests/test_client_session.py index 8ea0ed6c345..1c40505e9e2 100644 --- a/tests/test_client_session.py +++ b/tests/test_client_session.py @@ -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(