Fix `URL.join(...)` (#144)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by Seth Michael Larson · parent 77cf336 · Browse files · 2 files·+75 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+75 added−3 removed
- tests:+72 added−1 removed
- other:+3 added−2 removed
Provenance
- Commit
00e4fdb- Tree
7ad64e8- Author
Tom Christie <tom@tomchristie.com>- Committer
Seth Michael Larson <sethmichaellarson@gmail.com>- Parent
77cf336
2 files changed+75 added−3 removed
modified:httpx/models.py+3 added-2 removed
96 unmodified lines97 if idna_authority != self.components.authority:98 self.components = self.components.copy_with(authority=idna_authority)99 100 # Normalize scheme and domain name.101+ if self.is_absolute_url:101- self.components = self.components.normalize()102+ self.components = self.components.normalize()103 104 # Add any query parameters.105 if params:106 query_string = str(QueryParams(params))63 unmodified lines170 """171 Return `True` for absolute URLs such as 'http://example.com/path',172 and `False` for relative URLs such as '/path'.173 """173- # We don't use rfc3986's `is_absolute` because it treats174+ # We don't use `.is_absolute` from `rfc3986` because it treats175 # URLs with a fragment portion as not absolute.176 # What we actually care about is if the URL provides177 # a scheme and hostname to which connections should be made.178 return self.components.scheme and self.components.host991 unmodified linesmodified:tests/models/test_url.py+72 added-1 removed
1 from httpx import URL2+from httpx.exceptions import InvalidURL3+import pytest4 5 6 def test_idna_url():7 url = URL("http://中国.icom.museum:80/")24 unmodified lines32 assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"33 assert str(url) == url34 35 34-def test_url__params():36+def test_url_params():37 url = URL("https://example.org:123/path/to/somewhere", params={"a": "123"})38 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"39 40 url = URL("https://example.org:123/path/to/somewhere?b=456", params={"a": "123"})41 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"42 43 44+def test_url_join():45+ """46+ Some basic URL joining tests.47+ """48+ url = URL("https://example.org:123/path/to/somewhere")49+ assert url.join('/somewhere-else') == "https://example.org:123/somewhere-else"50+ assert url.join('somewhere-else') == "https://example.org:123/path/to/somewhere-else"51+ assert url.join('../somewhere-else') == "https://example.org:123/path/somewhere-else"52+ assert url.join('../../somewhere-else') == "https://example.org:123/somewhere-else"53+54+55+def test_url_join_rfc3986():56+ """57+ URL joining tests, as-per reference examples in RFC 3986.58+59+ https://tools.ietf.org/html/rfc3986#section-5.460+ """61+62+ url = URL("http://example.com/b/c/d;p?q")63+64+ with pytest.raises(InvalidURL):65+ assert url.join("g:h") == "g:h"66+67+ assert url.join("g") == "http://example.com/b/c/g"68+ assert url.join("./g") == "http://example.com/b/c/g"69+ assert url.join("g/") == "http://example.com/b/c/g/"70+ assert url.join("/g") == "http://example.com/g"71+ assert url.join("//g") == "http://g"72+ assert url.join("?y") == "http://example.com/b/c/d;p?y"73+ assert url.join("g?y") == "http://example.com/b/c/g?y"74+ assert url.join("#s") == "http://example.com/b/c/d;p?q#s"75+ assert url.join("g#s") == "http://example.com/b/c/g#s"76+ assert url.join("g?y#s") == "http://example.com/b/c/g?y#s"77+ assert url.join(";x") == "http://example.com/b/c/;x"78+ assert url.join("g;x") == "http://example.com/b/c/g;x"79+ assert url.join("g;x?y#s") == "http://example.com/b/c/g;x?y#s"80+ assert url.join("") == "http://example.com/b/c/d;p?q"81+ assert url.join(".") == "http://example.com/b/c/"82+ assert url.join("./") == "http://example.com/b/c/"83+ assert url.join("..") == "http://example.com/b/"84+ assert url.join("../") == "http://example.com/b/"85+ assert url.join("../g") == "http://example.com/b/g"86+ assert url.join("../..") == "http://example.com/"87+ assert url.join("../../") == "http://example.com/"88+ assert url.join("../../g") == "http://example.com/g"89+90+ assert url.join("../../../g") == "http://example.com/g"91+ assert url.join("../../../../g") == "http://example.com/g"92+93+ assert url.join("/./g") == "http://example.com/g"94+ assert url.join("/../g") == "http://example.com/g"95+ assert url.join("g.") == "http://example.com/b/c/g."96+ assert url.join(".g") == "http://example.com/b/c/.g"97+ assert url.join("g..") == "http://example.com/b/c/g.."98+ assert url.join("..g") == "http://example.com/b/c/..g"99+100+ assert url.join("./../g") == "http://example.com/b/g"101+ assert url.join("./g/.") == "http://example.com/b/c/g/"102+ assert url.join("g/./h") == "http://example.com/b/c/g/h"103+ assert url.join("g/../h") == "http://example.com/b/c/h"104+ assert url.join("g;x=1/./y") == "http://example.com/b/c/g;x=1/y"105+ assert url.join("g;x=1/../y") == "http://example.com/b/c/y"106+107+ assert url.join("g?y/./x") == "http://example.com/b/c/g?y/./x"108+ assert url.join("g?y/../x") == "http://example.com/b/c/g?y/../x"109+ assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"110+ assert url.join("g#s/../x") == "http://example.com/b/c/g#s/../x"111+112+113 def test_url_set():114 urls = (115 URL("http://example.org:123/path/to/somewhere"),116 URL("http://example.org:123/path/to/somewhere/else"),5 unmodified lines