test_urlparse.py
This file
Path
tests/test_urlparse.py
285 lines · 8.2 KiB · Python
Last changed by
Fix encode host (#2886)robinalybefore adoptione63b659
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–47why, one step back45 assert url.host == "example.com"4647Fix encode host (#2886)robinalybefore adoptione63b659
lines 48–53why, one step back48def test_urlparse_ipv4_like_host():49 """rare host names used to quality as IPv4"""50 url = httpx.URL("https://023b76x43144/")51 assert url.host == "023b76x43144"5253Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
line 54why, one step back54def test_urlparse_valid_ipv4():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 55why, one step back55 url = httpx.URL("https://1.2.3.4/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 56–60why, one step back56 assert url.host == "1.2.3.4"575859def test_urlparse_invalid_ipv4():60 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 61why, one step back61 httpx.URL("https://999.999.999.999/")Additional context in InvalidURL exceptions (#2675)Tom Christiebefore adoption26dc392
line 62why, one step back62 assert str(exc.value) == "Invalid IPv4 address: '999.999.999.999'"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 63–65why, one step back636465def test_urlparse_valid_ipv6():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 66why, one step back66 url = httpx.URL("https://[2001:db8::ff00:42:8329]/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 67–71why, one step back67 assert url.host == "2001:db8::ff00:42:8329"686970def test_urlparse_invalid_ipv6():71 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 72why, one step back72 httpx.URL("https://[2001]/")Additional context in InvalidURL exceptions (#2675)Tom Christiebefore adoption26dc392
line 73why, one step back73 assert str(exc.value) == "Invalid IPv6 address: '[2001]'"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 74–76why, one step back747576def test_urlparse_unescaped_idna_host():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
lines 77–78why, one step back77 url = httpx.URL("https://中国.icom.museum/")78 assert url.raw_host == b"xn--fiqs8s.icom.museum"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 79–81why, one step back798081def test_urlparse_escaped_idna_host():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
lines 82–83why, one step back82 url = httpx.URL("https://xn--fiqs8s.icom.museum/")83 assert url.raw_host == b"xn--fiqs8s.icom.museum"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 84–87why, one step back848586def test_urlparse_invalid_idna_host():87 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 88why, one step back88 httpx.URL("https://☃.com/")Additional context in InvalidURL exceptions (#2675)Tom Christiebefore adoption26dc392
line 89why, one step back89 assert str(exc.value) == "Invalid IDNA hostname: '☃.com'"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 90–95why, one step back909192# Tests for different port types939495def test_urlparse_valid_port():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 96why, one step back96 url = httpx.URL("https://example.com:123/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 97–101why, one step back97 assert url.port == 1239899100def test_urlparse_normalized_port():101 # If the port matches the scheme default it is normalized to None.Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 102why, one step back102 url = httpx.URL("https://example.com:443/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 103–107why, one step back103 assert url.port is None104105106def test_urlparse_invalid_port():107 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 108why, one step back108 httpx.URL("https://example.com:abc/")Additional context in InvalidURL exceptions (#2675)Tom Christiebefore adoption26dc392
line 109why, one step back109 assert str(exc.value) == "Invalid port: 'abc'"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 110–115why, one step back110111112# Tests for path handling113114115def test_urlparse_normalized_path():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 116why, one step back116 url = httpx.URL("https://example.com/abc/def/../ghi/./jkl")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 117–120why, one step back117 assert url.path == "/abc/ghi/jkl"118119120def test_urlparse_escaped_path():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
lines 121–122why, one step back121 url = httpx.URL("https://example.com/ /🌟/")122 assert url.raw_path == b"/%20/%F0%9F%8C%9F/"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 123–125why, one step back123124125def test_urlparse_leading_dot_prefix_on_absolute_url():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 126why, one step back126 url = httpx.URL("https://example.com/../abc")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 127–130why, one step back127 assert url.path == "/abc"128129130def test_urlparse_leading_dot_prefix_on_relative_url():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 131why, one step back131 url = httpx.URL("../abc")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 132–134why, one step back132 assert url.path == "../abc"133134Fix optional percent encoding behaviour. (#2671)Tom Christiebefore adoption9ae170a
lines 135–149why, one step back135# Tests for optional percent encoding136137138def test_param_requires_encoding():139 url = httpx.URL("http://webservice", params={"u": "with spaces"})140 assert str(url) == "http://webservice?u=with%20spaces"141142143def test_param_does_not_require_encoding():144 url = httpx.URL("http://webservice", params={"u": "with%20spaces"})145 assert str(url) == "http://webservice?u=with%20spaces"146147148def test_param_with_existing_escape_requires_encoding():149 url = httpx.URL("http://webservice", params={"u": "http://example.com?q=foo%2Fa"})Always encode forward slashes as `%2F` in query parameters (#2723)Zanie Adkinsbefore adoption920333e
line 150why, one step back150 assert str(url) == "http://webservice?u=http%3A%2F%2Fexample.com%3Fq%3Dfoo%252Fa"Fix optional percent encoding behaviour. (#2671)Tom Christiebefore adoption9ae170a
lines 151–152why, one step back151152Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 153–157why, one step back153# Tests for invalid URLs154155156def test_urlparse_excessively_long_url():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/" + "x" * 100_000)Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 159–163why, one step back159 assert str(exc.value) == "URL too long"160161162def test_urlparse_excessively_long_component():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", path="/" + "x" * 100_000)Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 165–169why, one step back165 assert str(exc.value) == "URL component 'path' too long"166167168def test_urlparse_non_printing_character_in_url():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/\n")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 171–175why, one step back171 assert str(exc.value) == "Invalid non-printable ASCII character in URL"172173174def test_urlparse_non_printing_character_in_component():175 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 176why, one step back176 httpx.URL("https://www.example.com", path="/\n")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 177–186why, one step back177 assert (178 str(exc.value)179 == "Invalid non-printable ASCII character in URL component 'path'"180 )181182183# Test for urlparse components184185186def test_urlparse_with_components():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 187why, one step back187 url = httpx.URL(scheme="https", host="www.example.com", path="/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 188–189why, one step back188189 assert url.scheme == "https"Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 190why, one step back190 assert url.userinfo == b""Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 191–193why, one step back191 assert url.host == "www.example.com"192 assert url.port is None193 assert url.path == "/"Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
lines 194–195why, one step back194 assert url.query == b""195 assert url.fragment == ""Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 196–201why, one step back196197 assert str(url) == "https://www.example.com/"198199200def test_urlparse_with_invalid_component():201 with pytest.raises(TypeError) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
lines 202–203why, one step back202 httpx.URL(scheme="https", host="www.example.com", incorrect="/")203 assert str(exc.value) == "'incorrect' is an invalid keyword argument for URL()"Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 204–207why, one step back204205206def test_urlparse_with_invalid_scheme():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="~", host="www.example.com", path="/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 209–213why, one step back209 assert str(exc.value) == "Invalid URL component 'scheme'"210211212def test_urlparse_with_invalid_path():213 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 214why, one step back214 httpx.URL(scheme="https", host="www.example.com", path="abc")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 215–217why, one step back215 assert str(exc.value) == "For absolute URLs, path must be empty or begin with '/'"216217 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 218why, one step back218 httpx.URL(path="//abc")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 219–224why, one step back219 assert (220 str(exc.value)221 == "URLs with no authority component cannot have a path starting with '//'"222 )223224 with pytest.raises(httpx.InvalidURL) as exc:Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 225why, one step back225 httpx.URL(path=":abc")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 226–233why, one step back226 assert (227 str(exc.value)228 == "URLs with no scheme component cannot have a path starting with ':'"229 )230231232def test_urlparse_with_relative_path():233 # 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 234why, one step back234 url = httpx.URL(path="abc")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 235–241why, one step back235 assert url.path == "abc"236237238# Tests for accessing and modifying `urlparse` results.239240241def test_copy_with():Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 242why, one step back242 url = httpx.URL("https://www.example.com/")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 243–250why, one step back243 assert str(url) == "https://www.example.com/"244245 url = url.copy_with()246 assert str(url) == "https://www.example.com/"247248 url = url.copy_with(scheme="http")249 assert str(url) == "http://www.example.com/"250Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36
line 251why, one step back251 url = url.copy_with(netloc=b"example.com")Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf
lines 252–255why, one step back252 assert str(url) == "http://example.com/"253254 url = url.copy_with(path="/abc")255 assert str(url) == "http://example.com/abc"Fix for gen-delims escaping behaviour in path/query/fragment (#2701)Tom Christiebefore adoptionee432c0
lines 256–272why, one step back256257258# Tests for percent encoding across path, query, and fragement...259260261def test_path_percent_encoding():262 # Test percent encoding for SUB_DELIMS ALPHA NUM and allowable GEN_DELIMS263 url = httpx.URL("https://example.com/!$&'()*+,;= abc ABC 123 :/[]@")264 assert url.raw_path == b"/!$&'()*+,;=%20abc%20ABC%20123%20:/[]@"265 assert url.path == "/!$&'()*+,;= abc ABC 123 :/[]@"266 assert url.query == b""267 assert url.fragment == ""268269270def test_query_percent_encoding():271 # Test percent encoding for SUB_DELIMS ALPHA NUM and allowable GEN_DELIMS272 url = httpx.URL("https://example.com/?!$&'()*+,;= abc ABC 123 :/[]@" + "?")Always encode forward slashes as `%2F` in query parameters (#2723)Zanie Adkinsbefore adoption920333e
line 273why, one step back273 assert url.raw_path == b"/?!$&'()*+,;=%20abc%20ABC%20123%20:%2F[]@?"Fix for gen-delims escaping behaviour in path/query/fragment (#2701)Tom Christiebefore adoptionee432c0
line 274why, one step back274 assert url.path == "/"Always encode forward slashes as `%2F` in query parameters (#2723)Zanie Adkinsbefore adoption920333e
line 275why, one step back275 assert url.query == b"!$&'()*+,;=%20abc%20ABC%20123%20:%2F[]@?"Fix for gen-delims escaping behaviour in path/query/fragment (#2701)Tom Christiebefore adoptionee432c0
lines 276–285why, one step back276 assert url.fragment == ""277278279def test_fragment_percent_encoding():280 # Test percent encoding for SUB_DELIMS ALPHA NUM and allowable GEN_DELIMS281 url = httpx.URL("https://example.com/#!$&'()*+,;= abc ABC 123 :/[]@" + "?#")282 assert url.raw_path == b"/"283 assert url.path == "/"284 assert url.query == b""285 assert url.fragment == "!$&'()*+,;= abc ABC 123 :/[]@?#"