Skip to content
Myr

test_url.py

This file

Path

tests/models/test_url.py

323 lines · 10.3 KiB · Python

Last changed by

Correctly handle ipv6 addresses as a part of URL (#1349)cdelerbefore adoption7fda99f

History of this file

Take it

The walk stopped after 200 commits: lines older than e107e0f point at it.

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 1–2why, one step back
1import pytest
2

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 3why, one step back
3import httpx

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 4–20why, one step back
4
56@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 back
21 None,

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

line 22why, one step back
22 ),

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

line 23why, one step back
23 ("https://faß.de", "https://xn--fa-hia.de", "xn--fa-hia.de", "https", None),

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 24–55why, one step back
24 (
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 back
56 url = httpx.URL(given)
57 assert url == httpx.URL(idna)

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 58–63why, one step back
58 assert url.host == host
59 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 back
64 url = httpx.URL("https://example.org:123/path/to/somewhere?abc=123#anchor")

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 65–68why, one step back
65 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 back
69 assert url.query == b"abc=123"
70 assert url.raw_path == b"/path/to/somewhere?abc=123"

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 71–76why, one step back
71 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 back
77 assert new == httpx.URL("http://example.org/path/to/somewhere?abc=123#anchor")

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 78–81why, one step back
78 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 back
82 url = httpx.URL("https://example.org:123/path/to/somewhere?abc=123#anchor")

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 83–87why, one step back
83 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 back
88 url = httpx.URL("https://example.org:123/path/to/somewhere", params={"a": "123"})

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 89–90why, one step back
89 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"
90

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 91–93why, one step back
91 url = httpx.URL(
92 "https://example.org:123/path/to/somewhere?b=456", params={"a": "123"}93 )

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 94–100why, one step back
94 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 back
101 url = httpx.URL("https://example.org:123/path/to/somewhere")

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 102–118why, one step back
102 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"110111112def test_url_join_rfc3986():113 """114 URL joining tests, as-per reference examples in RFC 3986.115116 https://tools.ietf.org/html/rfc3986#section-5.4117 """118

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 119why, one step back
119 url = httpx.URL("http://example.com/b/c/d;p?q")

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 120–168why, one step back
120
121 assert url.join("g") == "http://example.com/b/c/g"122 assert url.join("./g") == "http://example.com/b/c/g"123 assert url.join("g/") == "http://example.com/b/c/g/"124 assert url.join("/g") == "http://example.com/g"125 assert url.join("//g") == "http://g"126 assert url.join("?y") == "http://example.com/b/c/d;p?y"127 assert url.join("g?y") == "http://example.com/b/c/g?y"128 assert url.join("#s") == "http://example.com/b/c/d;p?q#s"129 assert url.join("g#s") == "http://example.com/b/c/g#s"130 assert url.join("g?y#s") == "http://example.com/b/c/g?y#s"131 assert url.join(";x") == "http://example.com/b/c/;x"132 assert url.join("g;x") == "http://example.com/b/c/g;x"133 assert url.join("g;x?y#s") == "http://example.com/b/c/g;x?y#s"134 assert url.join("") == "http://example.com/b/c/d;p?q"135 assert url.join(".") == "http://example.com/b/c/"136 assert url.join("./") == "http://example.com/b/c/"137 assert url.join("..") == "http://example.com/b/"138 assert url.join("../") == "http://example.com/b/"139 assert url.join("../g") == "http://example.com/b/g"140 assert url.join("../..") == "http://example.com/"141 assert url.join("../../") == "http://example.com/"142 assert url.join("../../g") == "http://example.com/g"143144 assert url.join("../../../g") == "http://example.com/g"145 assert url.join("../../../../g") == "http://example.com/g"146147 assert url.join("/./g") == "http://example.com/g"148 assert url.join("/../g") == "http://example.com/g"149 assert url.join("g.") == "http://example.com/b/c/g."150 assert url.join(".g") == "http://example.com/b/c/.g"151 assert url.join("g..") == "http://example.com/b/c/g.."152 assert url.join("..g") == "http://example.com/b/c/..g"153154 assert url.join("./../g") == "http://example.com/b/g"155 assert url.join("./g/.") == "http://example.com/b/c/g/"156 assert url.join("g/./h") == "http://example.com/b/c/g/h"157 assert url.join("g/../h") == "http://example.com/b/c/h"158 assert url.join("g;x=1/./y") == "http://example.com/b/c/g;x=1/y"159 assert url.join("g;x=1/../y") == "http://example.com/b/c/y"160161 assert url.join("g?y/./x") == "http://example.com/b/c/g?y/./x"162 assert url.join("g?y/../x") == "http://example.com/b/c/g?y/../x"163 assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"164 assert url.join("g#s/../x") == "http://example.com/b/c/g#s/../x"165166167def test_url_set():168 urls = (

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 169–170why, one step back
169 httpx.URL("http://example.org:123/path/to/somewhere"),
170 httpx.URL("http://example.org:123/path/to/somewhere/else"),

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 171–177why, one step back
171 )
172173 url_set = set(urls)174175 assert all(url in urls for url in url_set)176177

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

line 178why, one step back
178def test_url_copywith_authority_subcomponents():

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 179–184why, one step back
179 copy_with_kwargs = {
180 "username": "username",181 "password": "password",182 "port": 444,183 "host": "example.net",184 }

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Tighten public client methods (#997)Tom Christiebefore adoptione107e0f

lines 186–187why, one step back
186 new = url.copy_with(**copy_with_kwargs)
187 assert str(new) == "https://username:password@example.net:444"

Map rfc3986 exceptions (#1163)Joebefore adoption45de714

lines 188–189why, one step back
188
189

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

line 190why, one step back
190def test_url_copywith_netloc():

Handle URL quoting username and password components. (#1159)Tom Christiebefore adoption655773e

line 191why, one step back
191 copy_with_kwargs = {

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

lines 192–200why, one step back
192 "netloc": "example.net:444",
193 }194 url = httpx.URL("https://example.org")195 new = url.copy_with(**copy_with_kwargs)196 assert str(new) == "https://example.net:444"197198199def test_url_copywith_userinfo_subcomponents():200 copy_with_kwargs = {

Handle URL quoting username and password components. (#1159)Tom Christiebefore adoption655773e

lines 201–203why, one step back
201 "username": "tom@example.org",
202 "password": "abc123@ %",203 }

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

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

Handle URL quoting username and password components. (#1159)Tom Christiebefore adoption655773e

lines 205–208why, one step back
205 new = url.copy_with(**copy_with_kwargs)
206 assert str(new) == "https://tom%40example.org:abc123%40%20%25@example.org"207 assert new.username == "tom@example.org"208 assert new.password == "abc123@ %"

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

line 209why, one step back
209 assert new.userinfo == b"tom%40example.org:abc123%40%20%25"

Handle URL quoting username and password components. (#1159)Tom Christiebefore adoption655773e

lines 210–211why, one step back
210
211

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

lines 212–255why, one step back
212def test_url_copywith_invalid_component():
213 url = httpx.URL("https://example.org")214 with pytest.raises(TypeError):215 url.copy_with(pathh="/incorrect-spelling")216 with pytest.raises(TypeError):217 url.copy_with(userinfo="should be bytes")218219220def test_url_copywith_urlencoded_path():221 url = httpx.URL("https://example.org")222 url = url.copy_with(path="/path to somewhere")223 assert url.path == "/path to somewhere"224 assert url.query == b""225 assert url.raw_path == b"/path%20to%20somewhere"226227228def test_url_copywith_query():229 url = httpx.URL("https://example.org")230 url = url.copy_with(query=b"a=123")231 assert url.path == "/"232 assert url.query == b"a=123"233 assert url.raw_path == b"/?a=123"234235236def test_url_copywith_raw_path():237 url = httpx.URL("https://example.org")238 url = url.copy_with(raw_path=b"/some/path")239 assert url.path == "/some/path"240 assert url.query == b""241 assert url.raw_path == b"/some/path"242243 url = httpx.URL("https://example.org")244 url = url.copy_with(raw_path=b"/some/path?")245 assert url.path == "/some/path"246 assert url.query == b""247 assert url.raw_path == b"/some/path?"248249 url = httpx.URL("https://example.org")250 url = url.copy_with(raw_path=b"/some/path?a=123")251 assert url.path == "/some/path"252 assert url.query == b"a=123"253 assert url.raw_path == b"/some/path?a=123"254255

Map rfc3986 exceptions (#1163)Joebefore adoption45de714

line 256why, one step back
256def test_url_invalid():

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 257–258why, one step back
257 with pytest.raises(httpx.InvalidURL):
258 httpx.URL("https://😇/")

Raise a proper type error on invalid URL type (#1259)Tyler Wozniakbefore adoption42c6686

lines 259–266why, one step back
259
260261def test_url_invalid_type():262 class ExternalURLClass: # representing external URL class263 pass264265 with pytest.raises(TypeError):266 httpx.URL(ExternalURLClass()) # type: ignore

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

lines 267–289why, one step back
267
268269def test_url_with_empty_query():270 """271 URLs with and without a trailing `?` but an empty query component272 should preserve the information on the raw path.273 """274 url = httpx.URL("https://www.example.com/path")275 assert url.path == "/path"276 assert url.query == b""277 assert url.raw_path == b"/path"278279 url = httpx.URL("https://www.example.com/path?")280 assert url.path == "/path"281 assert url.query == b""282 assert url.raw_path == b"/path?"283284285def test_url_with_url_encoded_path():286 url = httpx.URL("https://www.example.com/path%20to%20somewhere")287 assert url.path == "/path to somewhere"288 assert url.query == b""289 assert url.raw_path == b"/path%20to%20somewhere"

Correctly handle ipv6 addresses as a part of URL (#1349)cdelerbefore adoption7fda99f

lines 290–323why, one step back
290
291292def test_ipv6_url():293 url = httpx.URL("http://[::ffff:192.168.0.1]:5678/")294295 assert url.host == "::ffff:192.168.0.1"296 assert url.netloc == "[::ffff:192.168.0.1]:5678"297298299@pytest.mark.parametrize(300 "url_str",301 [302 "http://127.0.0.1:1234",303 "http://example.com:1234",304 "http://[::ffff:127.0.0.1]:1234",305 ],306)307@pytest.mark.parametrize("new_host", ["[::ffff:192.168.0.1]", "::ffff:192.168.0.1"])308def test_ipv6_url_copy_with_host(url_str, new_host):309 url = httpx.URL(url_str).copy_with(host=new_host)310311 assert url.host == "::ffff:192.168.0.1"312 assert url.netloc == "[::ffff:192.168.0.1]:1234"313 assert str(url) == "http://[::ffff:192.168.0.1]:1234"314315316@pytest.mark.parametrize("host", [b"[::ffff:192.168.0.1]", b"::ffff:192.168.0.1"])317def test_ipv6_url_from_raw_url(host):318 raw_url = (b"https", host, 443, b"/")319 url = httpx.URL(raw_url)320321 assert url.host == "::ffff:192.168.0.1"322 assert url.netloc == "[::ffff:192.168.0.1]:443"323 assert str(url) == "https://[::ffff:192.168.0.1]:443/"