test_url.py
This file
Path
tests/models/test_url.py
121 lines · 4.6 KiB · Python
Last changed by
Fix `URL.join(...)` (#144)Tom Christiebefore adoption00e4fdb
History of this fileTake it
Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 1why, one step back1from httpx import URLFix `URL.join(...)` (#144)Tom Christiebefore adoption00e4fdb
lines 2–3why, one step back2from httpx.exceptions import InvalidURL3import pytestAdd idna support. More tests.Tom Christiebefore adoptionb948798
lines 4–9why, one step back456def test_idna_url():7 url = URL("http://中国.icom.museum:80/")8 assert url == URL("http://xn--fiqs8s.icom.museum:80/")9 assert url.host == "xn--fiqs8s.icom.museum"Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 10–20why, one step back101112def test_url():13 url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")14 assert url.scheme == "https"15 assert url.host == "example.org"16 assert url.port == 12317 assert url.authority == "example.org:123"18 assert url.path == "/path/to/somewhere"19 assert url.query == "abc=123"20 assert url.fragment == "anchor"Truckin' onTom Christiebefore adoption0ca1c84
lines 21–23why, one step back21 assert (22 repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"23 )Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 24–27why, one step back2425 new = url.copy_with(scheme="http")26 assert new == URL("http://example.org:123/path/to/somewhere?abc=123#anchor")27 assert new.scheme == "http"Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
lines 28–29why, one step back2829Allow string comparison for URL().__eq__ (#139)Stephen Brown IIbefore adoption8db36ed
lines 30–35why, one step back30def test_url_eq_str():31 url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")32 assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"33 assert str(url) == url3435Fix `URL.join(...)` (#144)Tom Christiebefore adoption00e4fdb
line 36why, one step back36def test_url_params():query_params -> paramsTom Christiebefore adoptionbb70377
line 37why, one step back37 url = URL("https://example.org:123/path/to/somewhere", params={"a": "123"})Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
lines 38–39why, one step back38 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"39query_params -> paramsTom Christiebefore adoptionbb70377
line 40why, one step back40 url = URL("https://example.org:123/path/to/somewhere?b=456", params={"a": "123"})Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 41why, one step back41 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"Fix `URL.join(...)` (#144)Tom Christiebefore adoption00e4fdb
lines 42–110why, one step back424344def 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"535455def test_url_join_rfc3986():56 """57 URL joining tests, as-per reference examples in RFC 3986.5859 https://tools.ietf.org/html/rfc3986#section-5.460 """6162 url = URL("http://example.com/b/c/d;p?q")6364 with pytest.raises(InvalidURL):65 assert url.join("g:h") == "g:h"6667 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"8990 assert url.join("../../../g") == "http://example.com/g"91 assert url.join("../../../../g") == "http://example.com/g"9293 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"99100 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"106107 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"Include URL.__hash__ in test (#64)Yeray Diaz Diazbefore adoption2e9ab2e
lines 111–121why, one step back111112113def test_url_set():114 urls = (115 URL("http://example.org:123/path/to/somewhere"),116 URL("http://example.org:123/path/to/somewhere/else"),117 )118119 url_set = set(urls)120121 assert all(url in urls for url in url_set)