Skip to content
Myr

test_urlparse.py

This file

Path

tests/test_urlparse.py

231 lines · 6.2 KiB · Python

Last changed by

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

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–48why, one step back
45 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 back
49 url = httpx.URL("https://1.2.3.4/")

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

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

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

lines 56–59why, one step back
56 assert str(exc.value) == "Invalid IPv4 address"
575859def test_urlparse_valid_ipv6():

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

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

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

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

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

lines 67–70why, one step back
67 assert str(exc.value) == "Invalid IPv6 address"
686970def test_urlparse_unescaped_idna_host():

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

lines 71–72why, one step back
71 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 back
73
7475def test_urlparse_escaped_idna_host():

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

lines 76–77why, one step back
76 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 back
78
7980def 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 back
82 httpx.URL("https://☃.com/")

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

lines 83–89why, one step back
83 assert str(exc.value) == "Invalid IDNA hostname"
848586# Tests for different port types878889def test_urlparse_valid_port():

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

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

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

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

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

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

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

lines 103–109why, one step back
103 assert str(exc.value) == "Invalid port"
104105106# Tests for path handling107108109def test_urlparse_normalized_path():

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

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

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

lines 111–114why, one step back
111 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 back
115 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 back
117
118119def test_urlparse_leading_dot_prefix_on_absolute_url():

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

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

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

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

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

lines 126–133why, one step back
126 assert url.path == "../abc"
127128129# Tests for invalid URLs130131132def test_urlparse_excessively_long_url():133 with pytest.raises(httpx.InvalidURL) as exc:

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

line 134why, one step back
134 httpx.URL("https://www.example.com/" + "x" * 100_000)

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

lines 135–139why, one step back
135 assert str(exc.value) == "URL too long"
136137138def test_urlparse_excessively_long_component():139 with pytest.raises(httpx.InvalidURL) as exc:

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

line 140why, one step back
140 httpx.URL("https://www.example.com", path="/" + "x" * 100_000)

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

lines 141–145why, one step back
141 assert str(exc.value) == "URL component 'path' too long"
142143144def test_urlparse_non_printing_character_in_url():145 with pytest.raises(httpx.InvalidURL) as exc:

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

line 146why, one step back
146 httpx.URL("https://www.example.com/\n")

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

lines 147–151why, one step back
147 assert str(exc.value) == "Invalid non-printable ASCII character in URL"
148149150def test_urlparse_non_printing_character_in_component():151 with pytest.raises(httpx.InvalidURL) as exc:

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

line 152why, one step back
152 httpx.URL("https://www.example.com", path="/\n")

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

lines 153–162why, one step back
153 assert (
154 str(exc.value)155 == "Invalid non-printable ASCII character in URL component 'path'"156 )157158159# Test for urlparse components160161162def test_urlparse_with_components():

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

line 163why, one step back
163 url = httpx.URL(scheme="https", host="www.example.com", path="/")

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

lines 164–165why, one step back
164
165 assert url.scheme == "https"

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

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

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

lines 167–169why, one step back
167 assert url.host == "www.example.com"
168 assert url.port is None169 assert url.path == "/"

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

lines 170–171why, one step back
170 assert url.query == b""
171 assert url.fragment == ""

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

lines 172–177why, one step back
172
173 assert str(url) == "https://www.example.com/"174175176def test_urlparse_with_invalid_component():177 with pytest.raises(TypeError) as exc:

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

lines 178–179why, one step back
178 httpx.URL(scheme="https", host="www.example.com", incorrect="/")
179 assert str(exc.value) == "'incorrect' is an invalid keyword argument for URL()"

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

lines 180–183why, one step back
180
181182def test_urlparse_with_invalid_scheme():183 with pytest.raises(httpx.InvalidURL) as exc:

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

line 184why, one step back
184 httpx.URL(scheme="~", host="www.example.com", path="/")

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

lines 185–189why, one step back
185 assert str(exc.value) == "Invalid URL component 'scheme'"
186187188def test_urlparse_with_invalid_path():189 with pytest.raises(httpx.InvalidURL) as exc:

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

line 190why, one step back
190 httpx.URL(scheme="https", host="www.example.com", path="abc")

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

lines 191–193why, one step back
191 assert str(exc.value) == "For absolute URLs, path must be empty or begin with '/'"
192193 with pytest.raises(httpx.InvalidURL) as exc:

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

line 194why, one step back
194 httpx.URL(path="//abc")

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

lines 195–200why, one step back
195 assert (
196 str(exc.value)197 == "URLs with no authority component cannot have a path starting with '//'"198 )199200 with pytest.raises(httpx.InvalidURL) as exc:

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

line 201why, one step back
201 httpx.URL(path=":abc")

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

lines 202–209why, one step back
202 assert (
203 str(exc.value)204 == "URLs with no scheme component cannot have a path starting with ':'"205 )206207208def test_urlparse_with_relative_path():209 # 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 210why, one step back
210 url = httpx.URL(path="abc")

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

lines 211–217why, one step back
211 assert url.path == "abc"
212213214# Tests for accessing and modifying `urlparse` results.215216217def test_copy_with():

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

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

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

lines 219–226why, one step back
219 assert str(url) == "https://www.example.com/"
220221 url = url.copy_with()222 assert str(url) == "https://www.example.com/"223224 url = url.copy_with(scheme="http")225 assert str(url) == "http://www.example.com/"226

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

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

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

lines 228–231why, one step back
228 assert str(url) == "http://example.com/"
229230 url = url.copy_with(path="/abc")231 assert str(url) == "http://example.com/abc"