test_urlparse.py
This file
Path
tests/test_urlparse.py
279 lines · 8.0 KiB · Python
Last changed by
Fix for gen-delims escaping behaviour in path/query/fragment (#2701)Tom Christiebefore adoptionee432c0
History of this fileTake it
Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 1–6why, one step back1import pytest23import httpx456def test_urlparse():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 7why, one step back7 url = httpx.URL("https://www.example.com/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 8–9why, one step back89 assert url.scheme == "https"Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
lines 10–11why, one step back10 assert url.userinfo == b""11 assert url.netloc == b"www.example.com"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 12–14why, one step back12 assert url.host == "www.example.com"13 assert url.port is None14 assert url.path == "/"Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
lines 15–16why, one step back15 assert url.query == b""16 assert url.fragment == ""Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 17–21why, one step back1718 assert str(url) == "https://www.example.com/"192021def test_urlparse_no_scheme():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 22why, one step back22 url = httpx.URL("://example.com")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 23–24why, one step back23 assert url.scheme == ""24 assert url.host == "example.com"Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 25why, one step back25 assert url.path == "/"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 26–28why, one step back262728def test_urlparse_no_authority():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 29why, one step back29 url = httpx.URL("http://")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 30–31why, one step back30 assert url.scheme == "http"31 assert url.host == ""Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 32why, one step back32 assert url.path == "/"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 33–38why, one step back333435# Tests for different host types363738def test_urlparse_valid_host():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 39why, one step back39 url = httpx.URL("https://example.com/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 40–43why, one step back40 assert url.host == "example.com"414243def test_urlparse_normalized_host():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 44why, one step back44 url = httpx.URL("https://EXAMPLE.com/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 45–48why, one step back45 assert url.host == "example.com"464748def test_urlparse_valid_ipv4():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 49why, one step back49 url = httpx.URL("https://1.2.3.4/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 50–54why, one step back50 assert url.host == "1.2.3.4"515253def test_urlparse_invalid_ipv4():54 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 55why, one step back55 httpx.URL("https://999.999.999.999/")Additional context in InvalidURL exceptions (#2675)Tom Christiebefore adoption26dc392
line 56why, one step back56 assert str(exc.value) == "Invalid IPv4 address: '999.999.999.999'"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 57–59why, one step back575859def test_urlparse_valid_ipv6():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 60why, one step back60 url = httpx.URL("https://[2001:db8::ff00:42:8329]/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 61–65why, one step back61 assert url.host == "2001:db8::ff00:42:8329"626364def test_urlparse_invalid_ipv6():65 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 66why, one step back66 httpx.URL("https://[2001]/")Additional context in InvalidURL exceptions (#2675)Tom Christiebefore adoption26dc392
line 67why, one step back67 assert str(exc.value) == "Invalid IPv6 address: '[2001]'"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 68–70why, one step back686970def test_urlparse_unescaped_idna_host():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
lines 71–72why, one step back71 url = httpx.URL("https://中国.icom.museum/")72 assert url.raw_host == b"xn--fiqs8s.icom.museum"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 73–75why, one step back737475def test_urlparse_escaped_idna_host():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
lines 76–77why, one step back76 url = httpx.URL("https://xn--fiqs8s.icom.museum/")77 assert url.raw_host == b"xn--fiqs8s.icom.museum"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 78–81why, one step back787980def test_urlparse_invalid_idna_host():81 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 82why, one step back82 httpx.URL("https://☃.com/")Additional context in InvalidURL exceptions (#2675)Tom Christiebefore adoption26dc392
line 83why, one step back83 assert str(exc.value) == "Invalid IDNA hostname: '☃.com'"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 84–89why, one step back848586# Tests for different port types878889def test_urlparse_valid_port():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 90why, one step back90 url = httpx.URL("https://example.com:123/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 91–95why, one step back91 assert url.port == 123929394def test_urlparse_normalized_port():95 # If the port matches the scheme default it is normalized to None.Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 96why, one step back96 url = httpx.URL("https://example.com:443/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 97–101why, one step back97 assert url.port is None9899100def test_urlparse_invalid_port():101 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 102why, one step back102 httpx.URL("https://example.com:abc/")Additional context in InvalidURL exceptions (#2675)Tom Christiebefore adoption26dc392
line 103why, one step back103 assert str(exc.value) == "Invalid port: 'abc'"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 104–109why, one step back104105106# Tests for path handling107108109def test_urlparse_normalized_path():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 110why, one step back110 url = httpx.URL("https://example.com/abc/def/../ghi/./jkl")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 111–114why, one step back111 assert url.path == "/abc/ghi/jkl"112113114def test_urlparse_escaped_path():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
lines 115–116why, one step back115 url = httpx.URL("https://example.com/ /🌟/")116 assert url.raw_path == b"/%20/%F0%9F%8C%9F/"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 117–119why, one step back117118119def test_urlparse_leading_dot_prefix_on_absolute_url():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 120why, one step back120 url = httpx.URL("https://example.com/../abc")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 121–124why, one step back121 assert url.path == "/abc"122123124def test_urlparse_leading_dot_prefix_on_relative_url():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 125why, one step back125 url = httpx.URL("../abc")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 126–128why, one step back126 assert url.path == "../abc"127128Fix optional percent encoding behaviour. (#2671)Tom Christiebefore adoption9ae170a
lines 129–146why, one step back129# Tests for optional percent encoding130131132def test_param_requires_encoding():133 url = httpx.URL("http://webservice", params={"u": "with spaces"})134 assert str(url) == "http://webservice?u=with%20spaces"135136137def test_param_does_not_require_encoding():138 url = httpx.URL("http://webservice", params={"u": "with%20spaces"})139 assert str(url) == "http://webservice?u=with%20spaces"140141142def test_param_with_existing_escape_requires_encoding():143 url = httpx.URL("http://webservice", params={"u": "http://example.com?q=foo%2Fa"})144 assert str(url) == "http://webservice?u=http%3A//example.com%3Fq%3Dfoo%252Fa"145146Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 147–151why, one step back147# Tests for invalid URLs148149150def test_urlparse_excessively_long_url():151 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 152why, one step back152 httpx.URL("https://www.example.com/" + "x" * 100_000)Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 153–157why, one step back153 assert str(exc.value) == "URL too long"154155156def test_urlparse_excessively_long_component():157 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 158why, one step back158 httpx.URL("https://www.example.com", path="/" + "x" * 100_000)Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 159–163why, one step back159 assert str(exc.value) == "URL component 'path' too long"160161162def test_urlparse_non_printing_character_in_url():163 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 164why, one step back164 httpx.URL("https://www.example.com/\n")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 165–169why, one step back165 assert str(exc.value) == "Invalid non-printable ASCII character in URL"166167168def test_urlparse_non_printing_character_in_component():169 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 170why, one step back170 httpx.URL("https://www.example.com", path="/\n")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 171–180why, one step back171 assert (172 str(exc.value)173 == "Invalid non-printable ASCII character in URL component 'path'"174 )175176177# Test for urlparse components178179180def test_urlparse_with_components():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 181why, one step back181 url = httpx.URL(scheme="https", host="www.example.com", path="/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 182–183why, one step back182183 assert url.scheme == "https"Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 184why, one step back184 assert url.userinfo == b""Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 185–187why, one step back185 assert url.host == "www.example.com"186 assert url.port is None187 assert url.path == "/"Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
lines 188–189why, one step back188 assert url.query == b""189 assert url.fragment == ""Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 190–195why, one step back190191 assert str(url) == "https://www.example.com/"192193194def test_urlparse_with_invalid_component():195 with pytest.raises(TypeError) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
lines 196–197why, one step back196 httpx.URL(scheme="https", host="www.example.com", incorrect="/")197 assert str(exc.value) == "'incorrect' is an invalid keyword argument for URL()"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 198–201why, one step back198199200def test_urlparse_with_invalid_scheme():201 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 202why, one step back202 httpx.URL(scheme="~", host="www.example.com", path="/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 203–207why, one step back203 assert str(exc.value) == "Invalid URL component 'scheme'"204205206def test_urlparse_with_invalid_path():207 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 208why, one step back208 httpx.URL(scheme="https", host="www.example.com", path="abc")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 209–211why, one step back209 assert str(exc.value) == "For absolute URLs, path must be empty or begin with '/'"210211 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 212why, one step back212 httpx.URL(path="//abc")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 213–218why, one step back213 assert (214 str(exc.value)215 == "URLs with no authority component cannot have a path starting with '//'"216 )217218 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 219why, one step back219 httpx.URL(path=":abc")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 220–227why, one step back220 assert (221 str(exc.value)222 == "URLs with no scheme component cannot have a path starting with ':'"223 )224225226def test_urlparse_with_relative_path():227 # This path would be invalid for an absolute URL, but is valid as a relative URL.Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 228why, one step back228 url = httpx.URL(path="abc")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 229–235why, one step back229 assert url.path == "abc"230231232# Tests for accessing and modifying `urlparse` results.233234235def test_copy_with():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 236why, one step back236 url = httpx.URL("https://www.example.com/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 237–244why, one step back237 assert str(url) == "https://www.example.com/"238239 url = url.copy_with()240 assert str(url) == "https://www.example.com/"241242 url = url.copy_with(scheme="http")243 assert str(url) == "http://www.example.com/"244Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 245why, one step back245 url = url.copy_with(netloc=b"example.com")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 246–249why, one step back246 assert str(url) == "http://example.com/"247248 url = url.copy_with(path="/abc")249 assert str(url) == "http://example.com/abc"Fix for gen-delims escaping behaviour in path/query/fragment (#2701)Tom Christiebefore adoptionee432c0
lines 250–279why, one step back250251252# Tests for percent encoding across path, query, and fragement...253254255def test_path_percent_encoding():256 # Test percent encoding for SUB_DELIMS ALPHA NUM and allowable GEN_DELIMS257 url = httpx.URL("https://example.com/!$&'()*+,;= abc ABC 123 :/[]@")258 assert url.raw_path == b"/!$&'()*+,;=%20abc%20ABC%20123%20:/[]@"259 assert url.path == "/!$&'()*+,;= abc ABC 123 :/[]@"260 assert url.query == b""261 assert url.fragment == ""262263264def test_query_percent_encoding():265 # Test percent encoding for SUB_DELIMS ALPHA NUM and allowable GEN_DELIMS266 url = httpx.URL("https://example.com/?!$&'()*+,;= abc ABC 123 :/[]@" + "?")267 assert url.raw_path == b"/?!$&'()*+,;=%20abc%20ABC%20123%20:/[]@?"268 assert url.path == "/"269 assert url.query == b"!$&'()*+,;=%20abc%20ABC%20123%20:/[]@?"270 assert url.fragment == ""271272273def test_fragment_percent_encoding():274 # Test percent encoding for SUB_DELIMS ALPHA NUM and allowable GEN_DELIMS275 url = httpx.URL("https://example.com/#!$&'()*+,;= abc ABC 123 :/[]@" + "?#")276 assert url.raw_path == b"/"277 assert url.path == "/"278 assert url.query == b""279 assert url.fragment == "!$&'()*+,;= abc ABC 123 :/[]@?#"