Skip to content
Myr

test_url.py

This file

Path

tests/models/test_url.py

206 lines · 6.8 KiB · Python

Last changed by

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

History of this file

Take it

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Drop `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78

line 3why, one step back
3from httpx import URL, InvalidURL

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

line 4why, one step back
4from httpx._models import Origin

Drop sync (#544)Tom Christiebefore adoption206c537

lines 5–76why, one step back
5
67@pytest.mark.parametrize(8 "given,idna,host,scheme,port",9 [10 (11 "http://中国.icom.museum:80/",12 "http://xn--fiqs8s.icom.museum:80/",13 "xn--fiqs8s.icom.museum",14 "http",15 80,16 ),17 (18 "http://Königsgäßchen.de",19 "http://xn--knigsgchen-b4a3dun.de",20 "xn--knigsgchen-b4a3dun.de",21 "http",22 80,23 ),24 ("https://faß.de", "https://xn--fa-hia.de", "xn--fa-hia.de", "https", 443),25 (26 "https://βόλος.com:443",27 "https://xn--nxasmm1c.com:443",28 "xn--nxasmm1c.com",29 "https",30 443,31 ),32 (33 "http://ශ්‍රී.com:444",34 "http://xn--10cl1a0b660p.com:444",35 "xn--10cl1a0b660p.com",36 "http",37 444,38 ),39 (40 "https://نامه‌ای.com:4433",41 "https://xn--mgba3gch31f060k.com:4433",42 "xn--mgba3gch31f060k.com",43 "https",44 4433,45 ),46 ],47 ids=[48 "http_with_port",49 "unicode_tr46_compat",50 "https_without_port",51 "https_with_port",52 "http_with_custom_port",53 "https_with_custom_port",54 ],55)56def test_idna_url(given, idna, host, scheme, port):57 url = URL(given)58 assert url == URL(idna)59 assert url.host == host60 assert url.scheme == scheme61 assert url.port == port626364def test_url():65 url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")66 assert url.scheme == "https"67 assert url.host == "example.org"68 assert url.port == 12369 assert url.authority == "example.org:123"70 assert url.path == "/path/to/somewhere"71 assert url.query == "abc=123"72 assert url.fragment == "anchor"73 assert (74 repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"75 )76

Fix Host header and HSTS when the default port is explicitly included in URL (#649)Tom Christiebefore adoption5ee512d

lines 77–78why, one step back
77 new = url.copy_with(scheme="http", port=None)
78 assert new == URL("http://example.org/path/to/somewhere?abc=123#anchor")

Drop sync (#544)Tom Christiebefore adoption206c537

lines 79–92why, one step back
79 assert new.scheme == "http"
808182def test_url_eq_str():83 url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")84 assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"85 assert str(url) == url868788def test_url_params():89 url = URL("https://example.org:123/path/to/somewhere", params={"a": "123"})90 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"9192 url = URL("https://example.org:123/path/to/somewhere?b=456", params={"a": "123"})

params argument on URL should merge, not replace. (#653)Tom Christiebefore adoption74e5115

line 93why, one step back
93 assert str(url) == "https://example.org:123/path/to/somewhere?b=456&a=123"

Drop sync (#544)Tom Christiebefore adoption206c537

lines 94–206why, one step back
94
9596def test_url_join():97 """98 Some basic URL joining tests.99 """100 url = URL("https://example.org:123/path/to/somewhere")101 assert url.join("/somewhere-else") == "https://example.org:123/somewhere-else"102 assert (103 url.join("somewhere-else") == "https://example.org:123/path/to/somewhere-else"104 )105 assert (106 url.join("../somewhere-else") == "https://example.org:123/path/somewhere-else"107 )108 assert url.join("../../somewhere-else") == "https://example.org:123/somewhere-else"109110111def test_url_join_rfc3986():112 """113 URL joining tests, as-per reference examples in RFC 3986.114115 https://tools.ietf.org/html/rfc3986#section-5.4116 """117118 url = URL("http://example.com/b/c/d;p?q")119120 with pytest.raises(InvalidURL):121 assert url.join("g:h") == "g:h"122123 assert url.join("g") == "http://example.com/b/c/g"124 assert url.join("./g") == "http://example.com/b/c/g"125 assert url.join("g/") == "http://example.com/b/c/g/"126 assert url.join("/g") == "http://example.com/g"127 assert url.join("//g") == "http://g"128 assert url.join("?y") == "http://example.com/b/c/d;p?y"129 assert url.join("g?y") == "http://example.com/b/c/g?y"130 assert url.join("#s") == "http://example.com/b/c/d;p?q#s"131 assert url.join("g#s") == "http://example.com/b/c/g#s"132 assert url.join("g?y#s") == "http://example.com/b/c/g?y#s"133 assert url.join(";x") == "http://example.com/b/c/;x"134 assert url.join("g;x") == "http://example.com/b/c/g;x"135 assert url.join("g;x?y#s") == "http://example.com/b/c/g;x?y#s"136 assert url.join("") == "http://example.com/b/c/d;p?q"137 assert url.join(".") == "http://example.com/b/c/"138 assert url.join("./") == "http://example.com/b/c/"139 assert url.join("..") == "http://example.com/b/"140 assert url.join("../") == "http://example.com/b/"141 assert url.join("../g") == "http://example.com/b/g"142 assert url.join("../..") == "http://example.com/"143 assert url.join("../../") == "http://example.com/"144 assert url.join("../../g") == "http://example.com/g"145146 assert url.join("../../../g") == "http://example.com/g"147 assert url.join("../../../../g") == "http://example.com/g"148149 assert url.join("/./g") == "http://example.com/g"150 assert url.join("/../g") == "http://example.com/g"151 assert url.join("g.") == "http://example.com/b/c/g."152 assert url.join(".g") == "http://example.com/b/c/.g"153 assert url.join("g..") == "http://example.com/b/c/g.."154 assert url.join("..g") == "http://example.com/b/c/..g"155156 assert url.join("./../g") == "http://example.com/b/g"157 assert url.join("./g/.") == "http://example.com/b/c/g/"158 assert url.join("g/./h") == "http://example.com/b/c/g/h"159 assert url.join("g/../h") == "http://example.com/b/c/h"160 assert url.join("g;x=1/./y") == "http://example.com/b/c/g;x=1/y"161 assert url.join("g;x=1/../y") == "http://example.com/b/c/y"162163 assert url.join("g?y/./x") == "http://example.com/b/c/g?y/./x"164 assert url.join("g?y/../x") == "http://example.com/b/c/g?y/../x"165 assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"166 assert url.join("g#s/../x") == "http://example.com/b/c/g#s/../x"167168169def test_url_set():170 urls = (171 URL("http://example.org:123/path/to/somewhere"),172 URL("http://example.org:123/path/to/somewhere/else"),173 )174175 url_set = set(urls)176177 assert all(url in urls for url in url_set)178179180def test_url_full_path_setter():181 url = URL("http://example.org")182183 url.full_path = "http://example.net"184 assert url.full_path == "http://example.net"185186187def test_origin_from_url_string():188 origin = Origin("https://example.com")189 assert origin.scheme == "https"190 assert origin.is_ssl191 assert origin.host == "example.com"192 assert origin.port == 443193194195def test_url_copywith_for_authority():196 copy_with_kwargs = {197 "username": "username",198 "password": "password",199 "port": 444,200 "host": "example.net",201 }202 url = URL("https://example.org")203 new = url.copy_with(**copy_with_kwargs)204 for k, v in copy_with_kwargs.items():205 assert getattr(new, k) == v206 assert str(new) == "https://username:password@example.net:444"