From 9005bd5df6019580c696788b769ba0faeb9053e5 Mon Sep 17 00:00:00 2001 From: Florimond Manca Date: Wed, 2 Dec 2020 14:01:11 +0100 Subject: [PATCH] Properly encoded slashes when using `base_url` (#1407) --- httpx/_client.py | 6 +++--- tests/client/test_client.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/httpx/_client.py b/httpx/_client.py index 18e45137bd..0ae5f2b1fa 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -120,9 +120,9 @@ def trust_env(self) -> bool: return self._trust_env def _enforce_trailing_slash(self, url: URL) -> URL: - if url.path.endswith("/"): + if url.raw_path.endswith(b"/"): return url - return url.copy_with(path=url.path + "/") + return url.copy_with(raw_path=url.raw_path + b"/") def _get_proxy_map( self, proxies: typing.Optional[ProxiesTypes], allow_env_proxies: bool @@ -327,7 +327,7 @@ def _merge_url(self, url: URLTypes) -> URL: if merge_url.is_relative_url: # We always ensure the base_url paths include the trailing '/', # and always strip any leading '/' from the merge URL. - merge_url = merge_url.copy_with(path=merge_url.path.lstrip("/")) + merge_url = merge_url.copy_with(raw_path=merge_url.raw_path.lstrip(b"/")) return self.base_url.join(merge_url) return merge_url diff --git a/tests/client/test_client.py b/tests/client/test_client.py index 3675730b30..e4bf46b9a4 100644 --- a/tests/client/test_client.py +++ b/tests/client/test_client.py @@ -198,6 +198,16 @@ def test_merge_relative_url_with_dotted_path(): assert request.url == "https://www.example.com/some/testing/123" +def test_merge_relative_url_with_encoded_slashes(): + client = httpx.Client(base_url="https://www.example.com/") + request = client.build_request("GET", "/testing%2F123") + assert request.url == "https://www.example.com/testing%2F123" + + client = httpx.Client(base_url="https://www.example.com/base%2Fpath") + request = client.build_request("GET", "/testing") + assert request.url == "https://www.example.com/base%2Fpath/testing" + + def test_pool_limits_deprecated(): limits = httpx.Limits()