test_url.py
This file
Path
tests/models/test_url.py
331 lines · 10.7 KiB · Python
Last changed by
Fix some cases of merging with base_url (#1532)Tom Christiebefore adoption6cb1672
History of this fileTake it
The walk stopped after 200 commits: lines older than 696c1ef point at it.
Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 1–2why, one step back1import pytest2Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 3why, one step back3import httpxParameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 4–20why, one step back456@pytest.mark.parametrize(7 "given,idna,host,scheme,port",8 [9 (10 "http://中国.icom.museum:80/",11 "http://xn--fiqs8s.icom.museum:80/",12 "xn--fiqs8s.icom.museum",13 "http",14 80,15 ),16 (17 "http://Königsgäßchen.de",18 "http://xn--knigsgchen-b4a3dun.de",19 "xn--knigsgchen-b4a3dun.de",20 "http",URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
line 21why, one step back21 None,Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
line 22why, one step back22 ),URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
line 23why, one step back23 ("https://faß.de", "https://xn--fa-hia.de", "xn--fa-hia.de", "https", None),Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 24–55why, one step back24 (25 "https://βόλος.com:443",26 "https://xn--nxasmm1c.com:443",27 "xn--nxasmm1c.com",28 "https",29 443,30 ),31 (32 "http://ශ්රී.com:444",33 "http://xn--10cl1a0b660p.com:444",34 "xn--10cl1a0b660p.com",35 "http",36 444,37 ),38 (39 "https://نامهای.com:4433",40 "https://xn--mgba3gch31f060k.com:4433",41 "xn--mgba3gch31f060k.com",42 "https",43 4433,44 ),45 ],46 ids=[47 "http_with_port",48 "unicode_tr46_compat",49 "https_without_port",50 "https_with_port",51 "http_with_custom_port",52 "https_with_custom_port",53 ],54)55def test_idna_url(given, idna, host, scheme, port):Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
lines 56–57why, one step back56 url = httpx.URL(given)57 assert url == httpx.URL(idna)Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 58–63why, one step back58 assert url.host == host59 assert url.scheme == scheme60 assert url.port == port616263def test_url():Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 64why, one step back64 url = httpx.URL("https://example.org:123/path/to/somewhere?abc=123#anchor")Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 65–68why, one step back65 assert url.scheme == "https"66 assert url.host == "example.org"67 assert url.port == 12368 assert url.path == "/path/to/somewhere"Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1
lines 69–70why, one step back69 assert url.query == b"abc=123"70 assert url.raw_path == b"/path/to/somewhere?abc=123"Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 71–76why, one step back71 assert url.fragment == "anchor"72 assert (73 repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"74 )7576 new = url.copy_with(scheme="http", port=None)Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 77why, one step back77 assert new == httpx.URL("http://example.org/path/to/somewhere?abc=123#anchor")Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 78–81why, one step back78 assert new.scheme == "http"798081def test_url_eq_str():Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 82why, one step back82 url = httpx.URL("https://example.org:123/path/to/somewhere?abc=123#anchor")Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 83–87why, one step back83 assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"84 assert str(url) == url858687def test_url_params():Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 88why, one step back88 url = httpx.URL("https://example.org:123/path/to/somewhere", params={"a": "123"})Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 89–90why, one step back89 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"90Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
lines 91–93why, one step back91 url = httpx.URL(92 "https://example.org:123/path/to/somewhere?b=456", params={"a": "123"}93 )Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 94–100why, one step back94 assert str(url) == "https://example.org:123/path/to/somewhere?b=456&a=123"959697def test_url_join():98 """99 Some basic URL joining tests.100 """Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 101why, one step back101 url = httpx.URL("https://example.org:123/path/to/somewhere")Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 102–111why, 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"110111Fix some cases of merging with base_url (#1532)Tom Christiebefore adoption6cb1672
lines 112–119why, one step back112def 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"118119Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 120–126why, one step back120def test_url_join_rfc3986():121 """122 URL joining tests, as-per reference examples in RFC 3986.123124 https://tools.ietf.org/html/rfc3986#section-5.4125 """126Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 127why, one step back127 url = httpx.URL("http://example.com/b/c/d;p?q")Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 128–176why, one step back128129 assert url.join("g") == "http://example.com/b/c/g"130 assert url.join("./g") == "http://example.com/b/c/g"131 assert url.join("g/") == "http://example.com/b/c/g/"132 assert url.join("/g") == "http://example.com/g"133 assert url.join("//g") == "http://g"134 assert url.join("?y") == "http://example.com/b/c/d;p?y"135 assert url.join("g?y") == "http://example.com/b/c/g?y"136 assert url.join("#s") == "http://example.com/b/c/d;p?q#s"137 assert url.join("g#s") == "http://example.com/b/c/g#s"138 assert url.join("g?y#s") == "http://example.com/b/c/g?y#s"139 assert url.join(";x") == "http://example.com/b/c/;x"140 assert url.join("g;x") == "http://example.com/b/c/g;x"141 assert url.join("g;x?y#s") == "http://example.com/b/c/g;x?y#s"142 assert url.join("") == "http://example.com/b/c/d;p?q"143 assert url.join(".") == "http://example.com/b/c/"144 assert url.join("./") == "http://example.com/b/c/"145 assert url.join("..") == "http://example.com/b/"146 assert url.join("../") == "http://example.com/b/"147 assert url.join("../g") == "http://example.com/b/g"148 assert url.join("../..") == "http://example.com/"149 assert url.join("../../") == "http://example.com/"150 assert url.join("../../g") == "http://example.com/g"151152 assert url.join("../../../g") == "http://example.com/g"153 assert url.join("../../../../g") == "http://example.com/g"154155 assert url.join("/./g") == "http://example.com/g"156 assert url.join("/../g") == "http://example.com/g"157 assert url.join("g.") == "http://example.com/b/c/g."158 assert url.join(".g") == "http://example.com/b/c/.g"159 assert url.join("g..") == "http://example.com/b/c/g.."160 assert url.join("..g") == "http://example.com/b/c/..g"161162 assert url.join("./../g") == "http://example.com/b/g"163 assert url.join("./g/.") == "http://example.com/b/c/g/"164 assert url.join("g/./h") == "http://example.com/b/c/g/h"165 assert url.join("g/../h") == "http://example.com/b/c/h"166 assert url.join("g;x=1/./y") == "http://example.com/b/c/g;x=1/y"167 assert url.join("g;x=1/../y") == "http://example.com/b/c/y"168169 assert url.join("g?y/./x") == "http://example.com/b/c/g?y/./x"170 assert url.join("g?y/../x") == "http://example.com/b/c/g?y/../x"171 assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"172 assert url.join("g#s/../x") == "http://example.com/b/c/g#s/../x"173174175def test_url_set():176 urls = (Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
lines 177–178why, one step back177 httpx.URL("http://example.org:123/path/to/somewhere"),178 httpx.URL("http://example.org:123/path/to/somewhere/else"),Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 179–185why, one step back179 )180181 url_set = set(urls)182183 assert all(url in urls for url in url_set)184185Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1
line 186why, one step back186def test_url_copywith_authority_subcomponents():Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 187–192why, one step back187 copy_with_kwargs = {188 "username": "username",189 "password": "password",190 "port": 444,191 "host": "example.net",192 }Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 193why, one step back193 url = httpx.URL("https://example.org")Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 194–195why, one step back194 new = url.copy_with(**copy_with_kwargs)195 assert str(new) == "https://username:password@example.net:444"Map rfc3986 exceptions (#1163)Joebefore adoption45de714
lines 196–197why, one step back196197Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1
line 198why, one step back198def test_url_copywith_netloc():Handle URL quoting username and password components. (#1159)Tom Christiebefore adoption655773e
line 199why, one step back199 copy_with_kwargs = {Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1
lines 200–208why, one step back200 "netloc": "example.net:444",201 }202 url = httpx.URL("https://example.org")203 new = url.copy_with(**copy_with_kwargs)204 assert str(new) == "https://example.net:444"205206207def test_url_copywith_userinfo_subcomponents():208 copy_with_kwargs = {Handle URL quoting username and password components. (#1159)Tom Christiebefore adoption655773e
lines 209–211why, one step back209 "username": "tom@example.org",210 "password": "abc123@ %",211 }Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 212why, one step back212 url = httpx.URL("https://example.org")Handle URL quoting username and password components. (#1159)Tom Christiebefore adoption655773e
lines 213–216why, one step back213 new = url.copy_with(**copy_with_kwargs)214 assert str(new) == "https://tom%40example.org:abc123%40%20%25@example.org"215 assert new.username == "tom@example.org"216 assert new.password == "abc123@ %"Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1
line 217why, one step back217 assert new.userinfo == b"tom%40example.org:abc123%40%20%25"Handle URL quoting username and password components. (#1159)Tom Christiebefore adoption655773e
lines 218–219why, one step back218219Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1
lines 220–263why, one step back220def test_url_copywith_invalid_component():221 url = httpx.URL("https://example.org")222 with pytest.raises(TypeError):223 url.copy_with(pathh="/incorrect-spelling")224 with pytest.raises(TypeError):225 url.copy_with(userinfo="should be bytes")226227228def test_url_copywith_urlencoded_path():229 url = httpx.URL("https://example.org")230 url = url.copy_with(path="/path to somewhere")231 assert url.path == "/path to somewhere"232 assert url.query == b""233 assert url.raw_path == b"/path%20to%20somewhere"234235236def test_url_copywith_query():237 url = httpx.URL("https://example.org")238 url = url.copy_with(query=b"a=123")239 assert url.path == "/"240 assert url.query == b"a=123"241 assert url.raw_path == b"/?a=123"242243244def test_url_copywith_raw_path():245 url = httpx.URL("https://example.org")246 url = url.copy_with(raw_path=b"/some/path")247 assert url.path == "/some/path"248 assert url.query == b""249 assert url.raw_path == b"/some/path"250251 url = httpx.URL("https://example.org")252 url = url.copy_with(raw_path=b"/some/path?")253 assert url.path == "/some/path"254 assert url.query == b""255 assert url.raw_path == b"/some/path?"256257 url = httpx.URL("https://example.org")258 url = url.copy_with(raw_path=b"/some/path?a=123")259 assert url.path == "/some/path"260 assert url.query == b"a=123"261 assert url.raw_path == b"/some/path?a=123"262263Map rfc3986 exceptions (#1163)Joebefore adoption45de714
line 264why, one step back264def test_url_invalid():Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
lines 265–266why, one step back265 with pytest.raises(httpx.InvalidURL):266 httpx.URL("https://😇/")Raise a proper type error on invalid URL type (#1259)Tyler Wozniakbefore adoption42c6686
lines 267–274why, one step back267268269def test_url_invalid_type():270 class ExternalURLClass: # representing external URL class271 pass272273 with pytest.raises(TypeError):274 httpx.URL(ExternalURLClass()) # type: ignoreFinesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1
lines 275–297why, one step back275276277def test_url_with_empty_query():278 """279 URLs with and without a trailing `?` but an empty query component280 should preserve the information on the raw path.281 """282 url = httpx.URL("https://www.example.com/path")283 assert url.path == "/path"284 assert url.query == b""285 assert url.raw_path == b"/path"286287 url = httpx.URL("https://www.example.com/path?")288 assert url.path == "/path"289 assert url.query == b""290 assert url.raw_path == b"/path?"291292293def test_url_with_url_encoded_path():294 url = httpx.URL("https://www.example.com/path%20to%20somewhere")295 assert url.path == "/path to somewhere"296 assert url.query == b""297 assert url.raw_path == b"/path%20to%20somewhere"Correctly handle ipv6 addresses as a part of URL (#1349)cdelerbefore adoption7fda99f
lines 298–331why, one step back298299300def test_ipv6_url():301 url = httpx.URL("http://[::ffff:192.168.0.1]:5678/")302303 assert url.host == "::ffff:192.168.0.1"304 assert url.netloc == "[::ffff:192.168.0.1]:5678"305306307@pytest.mark.parametrize(308 "url_str",309 [310 "http://127.0.0.1:1234",311 "http://example.com:1234",312 "http://[::ffff:127.0.0.1]:1234",313 ],314)315@pytest.mark.parametrize("new_host", ["[::ffff:192.168.0.1]", "::ffff:192.168.0.1"])316def test_ipv6_url_copy_with_host(url_str, new_host):317 url = httpx.URL(url_str).copy_with(host=new_host)318319 assert url.host == "::ffff:192.168.0.1"320 assert url.netloc == "[::ffff:192.168.0.1]:1234"321 assert str(url) == "http://[::ffff:192.168.0.1]:1234"322323324@pytest.mark.parametrize("host", [b"[::ffff:192.168.0.1]", b"::ffff:192.168.0.1"])325def test_ipv6_url_from_raw_url(host):326 raw_url = (b"https", host, 443, b"/")327 url = httpx.URL(raw_url)328329 assert url.host == "::ffff:192.168.0.1"330 assert url.netloc == "[::ffff:192.168.0.1]:443"331 assert str(url) == "https://[::ffff:192.168.0.1]:443/"