]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Properly encoded slashes when using `base_url` (#1407)
authorFlorimond Manca <florimond.manca@gmail.com>
Wed, 2 Dec 2020 13:01:11 +0000 (14:01 +0100)
committerGitHub <noreply@github.com>
Wed, 2 Dec 2020 13:01:11 +0000 (13:01 +0000)
httpx/_client.py
tests/client/test_client.py

index 18e45137bd017fb4381ee8e173c3f07e75c358a9..0ae5f2b1facfcda55b9124d53a4c6d7d09395a5a 100644 (file)
@@ -120,9 +120,9 @@ class BaseClient:
         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 @@ class BaseClient:
         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
 
index 3675730b309ddbe87d9521e705d6775d29a1059b..e4bf46b9a47aadfdb9780794f45a8892f0463e7b 100644 (file)
@@ -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()