Skip to content
Myr

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 file

Take it

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 1–6why, one step back
1import pytest
23import httpx456def test_urlparse():

Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36

line 7why, one step back
7 url = httpx.URL("https://www.example.com/")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 8–9why, one step back
8
9 assert url.scheme == "https"

Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36

lines 10–11why, one step back
10 assert url.userinfo == b""
11 assert url.netloc == b"www.example.com"

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 12–14why, one step back
12 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 back
15 assert url.query == b""
16 assert url.fragment == ""

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 17–21why, one step back
17
18 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 back
22 url = httpx.URL("://example.com")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 23–24why, one step back
23 assert url.scheme == ""
24 assert url.host == "example.com"

Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36

line 25why, one step back
25 assert url.path == "/"

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 26–28why, one step back
26
2728def test_urlparse_no_authority():

Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36

line 29why, one step back
29 url = httpx.URL("http://")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 30–31why, one step back
30 assert url.scheme == "http"
31 assert url.host == ""

Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36

line 32why, one step back
32 assert url.path == "/"

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 33–38why, one step back
33
3435# Tests for different host types363738def test_urlparse_valid_host():

Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36

line 39why, one step back
39 url = httpx.URL("https://example.com/")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 40–43why, one step back
40 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 back
44 url = httpx.URL("https://EXAMPLE.com/")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 45–47why, one step back
45 assert url.host == "example.com"
4647

Fix encode host (#2886)robinalybefore adoptione63b659

lines 48–53why, one step back
48def 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"5253

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

line 54why, one step back
54def test_urlparse_valid_ipv4():

Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36

line 55why, one step back
55 url = httpx.URL("https://1.2.3.4/")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 56–60why, one step back
56 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 back
61 httpx.URL("https://999.999.999.999/")

Additional context in InvalidURL exceptions (#2675)Tom Christiebefore adoption26dc392

line 62why, one step back
62 assert str(exc.value) == "Invalid IPv4 address: '999.999.999.999'"

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 63–65why, one step back
63
6465def test_urlparse_valid_ipv6():

Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36

line 66why, one step back
66 url = httpx.URL("https://[2001:db8::ff00:42:8329]/")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 67–71why, one step back
67 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 back
72 httpx.URL("https://[2001]/")

Additional context in InvalidURL exceptions (#2675)Tom Christiebefore adoption26dc392

line 73why, one step back
73 assert str(exc.value) == "Invalid IPv6 address: '[2001]'"

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 74–76why, one step back
74
7576def test_urlparse_unescaped_idna_host():

Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36

lines 77–78why, one step back
77 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 back
79
8081def test_urlparse_escaped_idna_host():

Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36

lines 82–83why, one step back
82 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 back
84
8586def 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 back
88 httpx.URL("https://☃.com/")

Additional context in InvalidURL exceptions (#2675)Tom Christiebefore adoption26dc392

line 89why, one step back
89 assert str(exc.value) == "Invalid IDNA hostname: '☃.com'"

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 90–95why, one step back
90
9192# Tests for different port types939495def test_urlparse_valid_port():

Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36

line 96why, one step back
96 url = httpx.URL("https://example.com:123/")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 97–101why, one step back
97 assert url.port == 123
9899100def 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 back
102 url = httpx.URL("https://example.com:443/")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 103–107why, one step back
103 assert url.port is None
104105106def 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 back
108 httpx.URL("https://example.com:abc/")

Additional context in InvalidURL exceptions (#2675)Tom Christiebefore adoption26dc392

line 109why, one step back
109 assert str(exc.value) == "Invalid port: 'abc'"

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 110–115why, one step back
110
111112# Tests for path handling113114115def test_urlparse_normalized_path():

Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36

line 116why, one step back
116 url = httpx.URL("https://example.com/abc/def/../ghi/./jkl")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 117–120why, one step back
117 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 back
121 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 back
123
124125def test_urlparse_leading_dot_prefix_on_absolute_url():

Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36

line 126why, one step back
126 url = httpx.URL("https://example.com/../abc")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 127–130why, one step back
127 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 back
131 url = httpx.URL("../abc")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 132–134why, one step back
132 assert url.path == "../abc"
133134

Fix optional percent encoding behaviour. (#2671)Tom Christiebefore adoption9ae170a

lines 135–149why, one step back
135# Tests for optional percent encoding
136137138def 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 back
150 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 back
151
152

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 153–157why, one step back
153# Tests for invalid URLs
154155156def 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 back
158 httpx.URL("https://www.example.com/" + "x" * 100_000)

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 159–163why, one step back
159 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 back
164 httpx.URL("https://www.example.com", path="/" + "x" * 100_000)

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 165–169why, one step back
165 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 back
170 httpx.URL("https://www.example.com/\n")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 171–175why, one step back
171 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 back
176 httpx.URL("https://www.example.com", path="/\n")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 177–186why, one step back
177 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 back
187 url = httpx.URL(scheme="https", host="www.example.com", path="/")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 188–189why, one step back
188
189 assert url.scheme == "https"

Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36

line 190why, one step back
190 assert url.userinfo == b""

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 191–193why, one step back
191 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 back
194 assert url.query == b""
195 assert url.fragment == ""

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 196–201why, one step back
196
197 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 back
202 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 back
204
205206def 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 back
208 httpx.URL(scheme="~", host="www.example.com", path="/")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 209–213why, one step back
209 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 back
214 httpx.URL(scheme="https", host="www.example.com", path="abc")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 215–217why, one step back
215 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 back
218 httpx.URL(path="//abc")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 219–224why, one step back
219 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 back
225 httpx.URL(path=":abc")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 226–233why, one step back
226 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 back
234 url = httpx.URL(path="abc")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 235–241why, one step back
235 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 back
242 url = httpx.URL("https://www.example.com/")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 243–250why, one step back
243 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/"250

Drop private imports from test_urlparse.py (#2572)Florimond Mancabefore adoptiona934c36

line 251why, one step back
251 url = url.copy_with(netloc=b"example.com")

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

lines 252–255why, one step back
252 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 back
256
257258# 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 back
273 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 back
274 assert url.path == "/"

Always encode forward slashes as `%2F` in query parameters (#2723)Zanie Adkinsbefore adoption920333e

line 275why, one step back
275 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 back
276 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 :/[]@?#"