Properly encoded slashes when using base_url (#1407)

This commit is contained in:
Florimond Manca 2020-12-02 14:01:11 +01:00 committed by GitHub
parent d0835da230
commit 9005bd5df6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

View 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

View 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()