Skip to content
Myr

Fix some cases of merging with base_url (#1532)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Fix some cases of merging with base_url * Fix for joining relative URLs * Improve code comment in _merge_url implementation

authored by Tom Christie · committed by GitHub · parent ea5ffce · Browse files · 4 files+35 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+35 added−5 removed

  • tests:+14 added−0 removed
  • other:+21 added−5 removed

4 files+35 added−5 removed

modified:httpx/_client.py+13 added-4 removed
323 unmodified lines
324 to create the URL used for the outgoing request.325 """326 merge_url = URL(url)327 if merge_url.is_relative_url:328 # We always ensure the base_url paths include the trailing '/',329 # and always strip any leading '/' from the merge URL.330 merge_url = merge_url.copy_with(raw_path=merge_url.raw_path.lstrip(b"/"))331 return self.base_url.join(merge_url)328 # To merge URLs we always append to the base URL. To get this329 # behaviour correct we always ensure the base URL ends in a '/'330 # seperator, and strip any leading '/' from the merge URL.331 #332 # So, eg...333 #334 # >>> client = Client(base_url="https://www.example.com/subpath")335 # >>> client.base_url336 # URL('https://www.example.com/subpath/')337 # >>> client.build_request("GET", "/path").url338 # URL('https://www.example.com/subpath/path')339 merge_raw_path = self.base_url.raw_path + merge_url.raw_path.lstrip(b"/")340 return self.base_url.copy_with(raw_path=merge_raw_path)341 return merge_url342343 def _merge_cookies(344 self, cookies: CookieTypes = None
1502 unmodified lines
modified:httpx/_models.py+8 added-1 removed
392 unmodified lines
393 url = url.join("/new/path")394 assert url == "https://www.example.com/test/new/path"395 """396 if self.is_relative_url:397 return URL(url)397 # Workaround to handle relative URLs, which otherwise raise398 # rfc3986.exceptions.ResolutionError when used as an argument399 # in `.resolve_with`.400 return (401 self.copy_with(scheme="http", host="example.com")402 .join(url)403 .copy_with(scheme=None, host=None)404 )405406 # We drop any fragment portion, because RFC 3986 strictly407 # treats URLs with a fragment portion as not being absolute URLs.408 base_uri = self._uri_reference.copy_with(fragment=None)
1137 unmodified lines
modified:tests/client/test_client.py+6 added
195 unmodified lines
196 request = client.build_request("GET", "../testing/123")197 assert request.url == "https://www.example.com/some/testing/123"198199200def test_merge_relative_url_with_path_including_colon():201 client = httpx.Client(base_url="https://www.example.com/some/path")202 request = client.build_request("GET", "/testing:123")203 assert request.url == "https://www.example.com/some/path/testing:123"204205206def test_merge_relative_url_with_encoded_slashes():207 client = httpx.Client(base_url="https://www.example.com/")208 request = client.build_request("GET", "/testing%2F123")209 assert request.url == "https://www.example.com/testing%2F123"
177 unmodified lines
modified:tests/models/test_url.py+8 added
107 unmodified lines
108 )109 assert url.join("../../somewhere-else") == "https://example.org:123/somewhere-else"110111112def test_relative_url_join():113 url = httpx.URL("/path/to/somewhere")114 assert url.join("/somewhere-else") == "/somewhere-else"115 assert url.join("somewhere-else") == "/path/to/somewhere-else"116 assert url.join("../somewhere-else") == "/path/somewhere-else"117 assert url.join("../../somewhere-else") == "/somewhere-else"118119120def test_url_join_rfc3986():121 """122 URL joining tests, as-per reference examples in RFC 3986.123
208 unmodified lines