test_url.py
This file
Path
tests/models/test_url.py
186 lines · 6.2 KiB · Python
Last changed by
Update IDNA encoding to 2008 spec (#161)Stephen Brown IIbefore adoption40849bf
History of this fileTake it
Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b
line 1why, one step back1import pytestUpdate IDNA encoding to 2008 spec (#161)Stephen Brown IIbefore adoption40849bf
line 2why, one step back2import rfc3986Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b
line 3why, one step back3Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 4why, one step back4from httpx import URLFix `URL.join(...)` (#144)Tom Christiebefore adoption00e4fdb
line 5why, one step back5from httpx.exceptions import InvalidURLAdd idna support. More tests.Tom Christiebefore adoptionb948798
lines 6–7why, one step back67Update IDNA encoding to 2008 spec (#161)Stephen Brown IIbefore adoption40849bf
lines 8–62why, one step back8@pytest.mark.parametrize(9 "given,idna,host,scheme,port",10 [11 (12 "http://中国.icom.museum:80/",13 "http://xn--fiqs8s.icom.museum:80/",14 "xn--fiqs8s.icom.museum",15 "http",16 80,17 ),18 (19 "http://Königsgäßchen.de",20 "http://xn--knigsgchen-b4a3dun.de",21 "xn--knigsgchen-b4a3dun.de",22 "http",23 80,24 ),25 ("https://faß.de", "https://xn--fa-hia.de", "xn--fa-hia.de", "https", 443),26 (27 "https://βόλος.com:443",28 "https://xn--nxasmm1c.com:443",29 "xn--nxasmm1c.com",30 "https",31 443,32 ),33 (34 "http://ශ්රී.com:444",35 "http://xn--10cl1a0b660p.com:444",36 "xn--10cl1a0b660p.com",37 "http",38 444,39 ),40 (41 "https://نامهای.com:4433",42 "https://xn--mgba3gch31f060k.com:4433",43 "xn--mgba3gch31f060k.com",44 "https",45 4433,46 ),47 ],48 ids=[49 "http_with_port",50 "unicode_tr46_compat",51 "https_without_port",52 "https_with_port",53 "http_with_custom_port",54 "https_with_custom_port",55 ],56)57def test_idna_url(given, idna, host, scheme, port):58 url = URL(given)59 assert url == URL(idna)60 assert url.host == host61 assert url.scheme == scheme62 assert url.port == portCarefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 63–73why, one step back636465def test_url():66 url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")67 assert url.scheme == "https"68 assert url.host == "example.org"69 assert url.port == 12370 assert url.authority == "example.org:123"71 assert url.path == "/path/to/somewhere"72 assert url.query == "abc=123"73 assert url.fragment == "anchor"Truckin' onTom Christiebefore adoption0ca1c84
lines 74–76why, one step back74 assert (75 repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"76 )Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 77–80why, one step back7778 new = url.copy_with(scheme="http")79 assert new == URL("http://example.org:123/path/to/somewhere?abc=123#anchor")80 assert new.scheme == "http"Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
lines 81–82why, one step back8182Allow string comparison for URL().__eq__ (#139)Stephen Brown IIbefore adoption8db36ed
lines 83–88why, one step back83def test_url_eq_str():84 url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")85 assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"86 assert str(url) == url8788Fix `URL.join(...)` (#144)Tom Christiebefore adoption00e4fdb
line 89why, one step back89def test_url_params():query_params -> paramsTom Christiebefore adoptionbb70377
line 90why, one step back90 url = URL("https://example.org:123/path/to/somewhere", params={"a": "123"})Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
lines 91–92why, one step back91 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"92query_params -> paramsTom Christiebefore adoptionbb70377
line 93why, one step back93 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 94why, one step back94 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"Fix `URL.join(...)` (#144)Tom Christiebefore adoption00e4fdb
lines 95–101why, one step back959697def test_url_join():98 """99 Some basic URL joining tests.100 """101 url = URL("https://example.org:123/path/to/somewhere")Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b
lines 102–109why, one step back102 assert url.join("/somewhere-else") == "https://example.org:123/somewhere-else"103 assert (104 url.join("somewhere-else") == "https://example.org:123/path/to/somewhere-else"105 )106 assert (107 url.join("../somewhere-else") == "https://example.org:123/path/somewhere-else"108 )109 assert url.join("../../somewhere-else") == "https://example.org:123/somewhere-else"Fix `URL.join(...)` (#144)Tom Christiebefore adoption00e4fdb
lines 110–167why, one step back110111112def test_url_join_rfc3986():113 """114 URL joining tests, as-per reference examples in RFC 3986.115116 https://tools.ietf.org/html/rfc3986#section-5.4117 """118119 url = URL("http://example.com/b/c/d;p?q")120121 with pytest.raises(InvalidURL):122 assert url.join("g:h") == "g:h"123124 assert url.join("g") == "http://example.com/b/c/g"125 assert url.join("./g") == "http://example.com/b/c/g"126 assert url.join("g/") == "http://example.com/b/c/g/"127 assert url.join("/g") == "http://example.com/g"128 assert url.join("//g") == "http://g"129 assert url.join("?y") == "http://example.com/b/c/d;p?y"130 assert url.join("g?y") == "http://example.com/b/c/g?y"131 assert url.join("#s") == "http://example.com/b/c/d;p?q#s"132 assert url.join("g#s") == "http://example.com/b/c/g#s"133 assert url.join("g?y#s") == "http://example.com/b/c/g?y#s"134 assert url.join(";x") == "http://example.com/b/c/;x"135 assert url.join("g;x") == "http://example.com/b/c/g;x"136 assert url.join("g;x?y#s") == "http://example.com/b/c/g;x?y#s"137 assert url.join("") == "http://example.com/b/c/d;p?q"138 assert url.join(".") == "http://example.com/b/c/"139 assert url.join("./") == "http://example.com/b/c/"140 assert url.join("..") == "http://example.com/b/"141 assert url.join("../") == "http://example.com/b/"142 assert url.join("../g") == "http://example.com/b/g"143 assert url.join("../..") == "http://example.com/"144 assert url.join("../../") == "http://example.com/"145 assert url.join("../../g") == "http://example.com/g"146147 assert url.join("../../../g") == "http://example.com/g"148 assert url.join("../../../../g") == "http://example.com/g"149150 assert url.join("/./g") == "http://example.com/g"151 assert url.join("/../g") == "http://example.com/g"152 assert url.join("g.") == "http://example.com/b/c/g."153 assert url.join(".g") == "http://example.com/b/c/.g"154 assert url.join("g..") == "http://example.com/b/c/g.."155 assert url.join("..g") == "http://example.com/b/c/..g"156157 assert url.join("./../g") == "http://example.com/b/g"158 assert url.join("./g/.") == "http://example.com/b/c/g/"159 assert url.join("g/./h") == "http://example.com/b/c/g/h"160 assert url.join("g/../h") == "http://example.com/b/c/h"161 assert url.join("g;x=1/./y") == "http://example.com/b/c/g;x=1/y"162 assert url.join("g;x=1/../y") == "http://example.com/b/c/y"163164 assert url.join("g?y/./x") == "http://example.com/b/c/g?y/./x"165 assert url.join("g?y/../x") == "http://example.com/b/c/g?y/../x"166 assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"167 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 168–178why, one step back168169170def test_url_set():171 urls = (172 URL("http://example.org:123/path/to/somewhere"),173 URL("http://example.org:123/path/to/somewhere/else"),174 )175176 url_set = set(urls)177178 assert all(url in urls for url in url_set)Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c
lines 179–186why, one step back179180181def test_hsts_preload_converted_to_https():182 url = URL("http://www.paypal.com")183184 assert url.is_ssl185 assert url.scheme == "https"186 assert url == "https://www.paypal.com"