Skip to content
Myr

test_url.py

This file

Path

tests/models/test_url.py

378 lines · 12.0 KiB · Python

Last changed by

Perform port normalization for http, https, ws, wss, and ftp schemes (#1603)Tom Christiebefore adoption0a8b44e

History of this file

Take it

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

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 1–6why, one step back
1import pytest
23import httpx456@pytest.mark.parametrize(

Differentiate between 'url.host' and 'url.raw_host' (#1590)Tom Christiebefore adoption39d8ee6

line 7why, one step back
7 "given,idna,host,raw_host,scheme,port",

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 8–11why, one step back
8 [
9 (10 "http://中国.icom.museum:80/",11 "http://xn--fiqs8s.icom.museum:80/",

Differentiate between 'url.host' and 'url.raw_host' (#1590)Tom Christiebefore adoption39d8ee6

lines 12–13why, one step back
12 "中国.icom.museum",
13 b"xn--fiqs8s.icom.museum",

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 14why, one step back
14 "http",

Perform port normalization for http, https, ws, wss, and ftp schemes (#1603)Tom Christiebefore adoption0a8b44e

line 15why, one step back
15 None,

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 16–19why, one step back
16 ),
17 (18 "http://Königsgäßchen.de",19 "http://xn--knigsgchen-b4a3dun.de",

Differentiate between 'url.host' and 'url.raw_host' (#1590)Tom Christiebefore adoption39d8ee6

lines 20–21why, one step back
20 "königsgäßchen.de",
21 b"xn--knigsgchen-b4a3dun.de",

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 22–25why, one step back
22 "http",
23 None,24 ),25 (

Differentiate between 'url.host' and 'url.raw_host' (#1590)Tom Christiebefore adoption39d8ee6

lines 26–33why, one step back
26 "https://faß.de",
27 "https://xn--fa-hia.de",28 "faß.de",29 b"xn--fa-hia.de",30 "https",31 None,32 ),33 (

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 34–35why, one step back
34 "https://βόλος.com:443",
35 "https://xn--nxasmm1c.com:443",

Differentiate between 'url.host' and 'url.raw_host' (#1590)Tom Christiebefore adoption39d8ee6

lines 36–37why, one step back
36 "βόλος.com",
37 b"xn--nxasmm1c.com",

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 38why, one step back
38 "https",

Perform port normalization for http, https, ws, wss, and ftp schemes (#1603)Tom Christiebefore adoption0a8b44e

line 39why, one step back
39 None,

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 40–43why, one step back
40 ),
41 (42 "http://ශ්‍රී.com:444",43 "http://xn--10cl1a0b660p.com:444",

Differentiate between 'url.host' and 'url.raw_host' (#1590)Tom Christiebefore adoption39d8ee6

lines 44–45why, one step back
44 "ශ්‍රී.com",
45 b"xn--10cl1a0b660p.com",

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 46–51why, one step back
46 "http",
47 444,48 ),49 (50 "https://نامه‌ای.com:4433",51 "https://xn--mgba3gch31f060k.com:4433",

Differentiate between 'url.host' and 'url.raw_host' (#1590)Tom Christiebefore adoption39d8ee6

lines 52–53why, one step back
52 "نامه‌ای.com",
53 b"xn--mgba3gch31f060k.com",

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 54–66why, one step back
54 "https",
55 4433,56 ),57 ],58 ids=[59 "http_with_port",60 "unicode_tr46_compat",61 "https_without_port",62 "https_with_port",63 "http_with_custom_port",64 "https_with_custom_port",65 ],66)

Differentiate between 'url.host' and 'url.raw_host' (#1590)Tom Christiebefore adoption39d8ee6

line 67why, one step back
67def test_idna_url(given, idna, host, raw_host, scheme, port):

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 68–70why, one step back
68 url = httpx.URL(given)
69 assert url == httpx.URL(idna)70 assert url.host == host

Differentiate between 'url.host' and 'url.raw_host' (#1590)Tom Christiebefore adoption39d8ee6

line 71why, one step back
71 assert url.raw_host == raw_host

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 72–81why, one step back
72 assert url.scheme == scheme
73 assert url.port == port747576def test_url():77 url = httpx.URL("https://example.org:123/path/to/somewhere?abc=123#anchor")78 assert url.scheme == "https"79 assert url.host == "example.org"80 assert url.port == 12381 assert url.path == "/path/to/somewhere"

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

lines 82–83why, one step back
82 assert url.query == b"abc=123"
83 assert url.raw_path == b"/path/to/somewhere?abc=123"

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 84–102why, one step back
84 assert url.fragment == "anchor"
85 assert (86 repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"87 )8889 new = url.copy_with(scheme="http", port=None)90 assert new == httpx.URL("http://example.org/path/to/somewhere?abc=123#anchor")91 assert new.scheme == "http"929394def test_url_eq_str():95 url = httpx.URL("https://example.org:123/path/to/somewhere?abc=123#anchor")96 assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"97 assert str(url) == url9899100def test_url_params():101 url = httpx.URL("https://example.org:123/path/to/somewhere", params={"a": "123"})102 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"

Expand URL interface (#1601)Tom Christiebefore adoptione67b0dd

line 103why, one step back
103 assert url.params == httpx.QueryParams({"a": "123"})

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

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

Expand URL interface (#1601)Tom Christiebefore adoptione67b0dd

lines 108–109why, one step back
108 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"
109 assert url.params == httpx.QueryParams({"a": "123"})

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 110–124why, one step back
110
111112def test_url_join():113 """114 Some basic URL joining tests.115 """116 url = httpx.URL("https://example.org:123/path/to/somewhere")117 assert url.join("/somewhere-else") == "https://example.org:123/somewhere-else"118 assert (119 url.join("somewhere-else") == "https://example.org:123/path/to/somewhere-else"120 )121 assert (122 url.join("../somewhere-else") == "https://example.org:123/path/somewhere-else"123 )124 assert url.join("../../somewhere-else") == "https://example.org:123/somewhere-else"

Expand URL interface (#1601)Tom Christiebefore adoptione67b0dd

lines 125–156why, one step back
125
126127def test_url_set_param_manipulation():128 """129 Some basic URL query parameter manipulation.130 """131 url = httpx.URL("https://example.org:123/?a=123")132 assert url.copy_set_param("a", "456") == "https://example.org:123/?a=456"133134135def test_url_add_param_manipulation():136 """137 Some basic URL query parameter manipulation.138 """139 url = httpx.URL("https://example.org:123/?a=123")140 assert url.copy_add_param("a", "456") == "https://example.org:123/?a=123&a=456"141142143def test_url_remove_param_manipulation():144 """145 Some basic URL query parameter manipulation.146 """147 url = httpx.URL("https://example.org:123/?a=123")148 assert url.copy_remove_param("a") == "https://example.org:123/"149150151def test_url_merge_params_manipulation():152 """153 Some basic URL query parameter manipulation.154 """155 url = httpx.URL("https://example.org:123/?a=123")156 assert url.copy_merge_params({"b": "456"}) == "https://example.org:123/?a=123&b=456"

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 157–158why, one step back
157
158

Fix some cases of merging with base_url (#1532)Tom Christiebefore adoption6cb1672

lines 159–166why, one step back
159def test_relative_url_join():
160 url = httpx.URL("/path/to/somewhere")161 assert url.join("/somewhere-else") == "/somewhere-else"162 assert url.join("somewhere-else") == "/path/to/somewhere-else"163 assert url.join("../somewhere-else") == "/path/somewhere-else"164 assert url.join("../../somewhere-else") == "/somewhere-else"165166

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 167–232why, one step back
167def test_url_join_rfc3986():
168 """169 URL joining tests, as-per reference examples in RFC 3986.170171 https://tools.ietf.org/html/rfc3986#section-5.4172 """173174 url = httpx.URL("http://example.com/b/c/d;p?q")175176 assert url.join("g") == "http://example.com/b/c/g"177 assert url.join("./g") == "http://example.com/b/c/g"178 assert url.join("g/") == "http://example.com/b/c/g/"179 assert url.join("/g") == "http://example.com/g"180 assert url.join("//g") == "http://g"181 assert url.join("?y") == "http://example.com/b/c/d;p?y"182 assert url.join("g?y") == "http://example.com/b/c/g?y"183 assert url.join("#s") == "http://example.com/b/c/d;p?q#s"184 assert url.join("g#s") == "http://example.com/b/c/g#s"185 assert url.join("g?y#s") == "http://example.com/b/c/g?y#s"186 assert url.join(";x") == "http://example.com/b/c/;x"187 assert url.join("g;x") == "http://example.com/b/c/g;x"188 assert url.join("g;x?y#s") == "http://example.com/b/c/g;x?y#s"189 assert url.join("") == "http://example.com/b/c/d;p?q"190 assert url.join(".") == "http://example.com/b/c/"191 assert url.join("./") == "http://example.com/b/c/"192 assert url.join("..") == "http://example.com/b/"193 assert url.join("../") == "http://example.com/b/"194 assert url.join("../g") == "http://example.com/b/g"195 assert url.join("../..") == "http://example.com/"196 assert url.join("../../") == "http://example.com/"197 assert url.join("../../g") == "http://example.com/g"198199 assert url.join("../../../g") == "http://example.com/g"200 assert url.join("../../../../g") == "http://example.com/g"201202 assert url.join("/./g") == "http://example.com/g"203 assert url.join("/../g") == "http://example.com/g"204 assert url.join("g.") == "http://example.com/b/c/g."205 assert url.join(".g") == "http://example.com/b/c/.g"206 assert url.join("g..") == "http://example.com/b/c/g.."207 assert url.join("..g") == "http://example.com/b/c/..g"208209 assert url.join("./../g") == "http://example.com/b/g"210 assert url.join("./g/.") == "http://example.com/b/c/g/"211 assert url.join("g/./h") == "http://example.com/b/c/g/h"212 assert url.join("g/../h") == "http://example.com/b/c/h"213 assert url.join("g;x=1/./y") == "http://example.com/b/c/g;x=1/y"214 assert url.join("g;x=1/../y") == "http://example.com/b/c/y"215216 assert url.join("g?y/./x") == "http://example.com/b/c/g?y/./x"217 assert url.join("g?y/../x") == "http://example.com/b/c/g?y/../x"218 assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"219 assert url.join("g#s/../x") == "http://example.com/b/c/g#s/../x"220221222def test_url_set():223 urls = (224 httpx.URL("http://example.org:123/path/to/somewhere"),225 httpx.URL("http://example.org:123/path/to/somewhere/else"),226 )227228 url_set = set(urls)229230 assert all(url in urls for url in url_set)231232

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

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

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 234–244why, one step back
234 copy_with_kwargs = {
235 "username": "username",236 "password": "password",237 "port": 444,238 "host": "example.net",239 }240 url = httpx.URL("https://example.org")241 new = url.copy_with(**copy_with_kwargs)242 assert str(new) == "https://username:password@example.net:444"243244

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

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

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 246why, one step back
246 copy_with_kwargs = {

Differentiate between 'url.host' and 'url.raw_host' (#1590)Tom Christiebefore adoption39d8ee6

line 247why, one step back
247 "netloc": b"example.net:444",

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

lines 248–255why, one step back
248 }
249 url = httpx.URL("https://example.org")250 new = url.copy_with(**copy_with_kwargs)251 assert str(new) == "https://example.net:444"252253254def test_url_copywith_userinfo_subcomponents():255 copy_with_kwargs = {

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 256–263why, one step back
256 "username": "tom@example.org",
257 "password": "abc123@ %",258 }259 url = httpx.URL("https://example.org")260 new = url.copy_with(**copy_with_kwargs)261 assert str(new) == "https://tom%40example.org:abc123%40%20%25@example.org"262 assert new.username == "tom@example.org"263 assert new.password == "abc123@ %"

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

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

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 265–266why, one step back
265
266

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

lines 267–310why, one step back
267def test_url_copywith_invalid_component():
268 url = httpx.URL("https://example.org")269 with pytest.raises(TypeError):270 url.copy_with(pathh="/incorrect-spelling")271 with pytest.raises(TypeError):272 url.copy_with(userinfo="should be bytes")273274275def test_url_copywith_urlencoded_path():276 url = httpx.URL("https://example.org")277 url = url.copy_with(path="/path to somewhere")278 assert url.path == "/path to somewhere"279 assert url.query == b""280 assert url.raw_path == b"/path%20to%20somewhere"281282283def test_url_copywith_query():284 url = httpx.URL("https://example.org")285 url = url.copy_with(query=b"a=123")286 assert url.path == "/"287 assert url.query == b"a=123"288 assert url.raw_path == b"/?a=123"289290291def test_url_copywith_raw_path():292 url = httpx.URL("https://example.org")293 url = url.copy_with(raw_path=b"/some/path")294 assert url.path == "/some/path"295 assert url.query == b""296 assert url.raw_path == b"/some/path"297298 url = httpx.URL("https://example.org")299 url = url.copy_with(raw_path=b"/some/path?")300 assert url.path == "/some/path"301 assert url.query == b""302 assert url.raw_path == b"/some/path?"303304 url = httpx.URL("https://example.org")305 url = url.copy_with(raw_path=b"/some/path?a=123")306 assert url.path == "/some/path"307 assert url.query == b"a=123"308 assert url.raw_path == b"/some/path?a=123"309310

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 311–313why, one step back
311def test_url_invalid():
312 with pytest.raises(httpx.InvalidURL):313 httpx.URL("https://😇/")

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

lines 314–321why, one step back
314
315316def test_url_invalid_type():317 class ExternalURLClass: # representing external URL class318 pass319320 with pytest.raises(TypeError):321 httpx.URL(ExternalURLClass()) # type: ignore

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

lines 322–344why, one step back
322
323324def test_url_with_empty_query():325 """326 URLs with and without a trailing `?` but an empty query component327 should preserve the information on the raw path.328 """329 url = httpx.URL("https://www.example.com/path")330 assert url.path == "/path"331 assert url.query == b""332 assert url.raw_path == b"/path"333334 url = httpx.URL("https://www.example.com/path?")335 assert url.path == "/path"336 assert url.query == b""337 assert url.raw_path == b"/path?"338339340def test_url_with_url_encoded_path():341 url = httpx.URL("https://www.example.com/path%20to%20somewhere")342 assert url.path == "/path to somewhere"343 assert url.query == b""344 assert url.raw_path == b"/path%20to%20somewhere"

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

lines 345–350why, one step back
345
346347def test_ipv6_url():348 url = httpx.URL("http://[::ffff:192.168.0.1]:5678/")349350 assert url.host == "::ffff:192.168.0.1"

Differentiate between 'url.host' and 'url.raw_host' (#1590)Tom Christiebefore adoption39d8ee6

line 351why, one step back
351 assert url.netloc == b"[::ffff:192.168.0.1]:5678"

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

lines 352–366why, one step back
352
353354@pytest.mark.parametrize(355 "url_str",356 [357 "http://127.0.0.1:1234",358 "http://example.com:1234",359 "http://[::ffff:127.0.0.1]:1234",360 ],361)362@pytest.mark.parametrize("new_host", ["[::ffff:192.168.0.1]", "::ffff:192.168.0.1"])363def test_ipv6_url_copy_with_host(url_str, new_host):364 url = httpx.URL(url_str).copy_with(host=new_host)365366 assert url.host == "::ffff:192.168.0.1"

Differentiate between 'url.host' and 'url.raw_host' (#1590)Tom Christiebefore adoption39d8ee6

line 367why, one step back
367 assert url.netloc == b"[::ffff:192.168.0.1]:1234"

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

lines 368–376why, one step back
368 assert str(url) == "http://[::ffff:192.168.0.1]:1234"
369370371@pytest.mark.parametrize("host", [b"[::ffff:192.168.0.1]", b"::ffff:192.168.0.1"])372def test_ipv6_url_from_raw_url(host):373 raw_url = (b"https", host, 443, b"/")374 url = httpx.URL(raw_url)375376 assert url.host == "::ffff:192.168.0.1"

Perform port normalization for http, https, ws, wss, and ftp schemes (#1603)Tom Christiebefore adoption0a8b44e

lines 377–378why, one step back
377 assert url.netloc == b"[::ffff:192.168.0.1]"
378 assert str(url) == "https://[::ffff:192.168.0.1]/"