Skip to content
Myr

test_url.py

This file

Path

tests/models/test_url.py

881 lines · 26.4 KiB · Python

Last changed by

Add URL parsing tests from WHATWG (#3188)Tom Christiebefore adoptiondb9072f

History of this file

Take it

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

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 1–4why, one step back
1import pytest
23import httpx4

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

line 5why, one step back
5# Tests for `httpx.URL` instantiation and property accessors.

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 6why, one step back
6

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 7–24why, one step back
7
8def test_basic_url():9 url = httpx.URL("https://www.example.com/")1011 assert url.scheme == "https"12 assert url.userinfo == b""13 assert url.netloc == b"www.example.com"14 assert url.host == "www.example.com"15 assert url.port is None16 assert url.path == "/"17 assert url.query == b""18 assert url.fragment == ""1920 assert str(url) == "https://www.example.com/"21 assert repr(url) == "URL('https://www.example.com/')"222324def test_complete_url():

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 25–32why, one step back
25 url = httpx.URL("https://example.org:123/path/to/somewhere?abc=123#anchor")
26 assert url.scheme == "https"27 assert url.host == "example.org"28 assert url.port == 12329 assert url.path == "/path/to/somewhere"30 assert url.query == b"abc=123"31 assert url.raw_path == b"/path/to/somewhere?abc=123"32 assert url.fragment == "anchor"

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 33–34why, one step back
33
34 assert str(url) == "https://example.org:123/path/to/somewhere?abc=123#anchor"

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 35–39why, one step back
35 assert (
36 repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"37 )3839

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 40–55why, one step back
40def test_url_with_empty_query():
41 """42 URLs with and without a trailing `?` but an empty query component43 should preserve the information on the raw path.44 """45 url = httpx.URL("https://www.example.com/path")46 assert url.path == "/path"47 assert url.query == b""48 assert url.raw_path == b"/path"4950 url = httpx.URL("https://www.example.com/path?")51 assert url.path == "/path"52 assert url.query == b""53 assert url.raw_path == b"/path?"5455

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 56–72why, one step back
56def test_url_no_scheme():
57 url = httpx.URL("://example.com")58 assert url.scheme == ""59 assert url.host == "example.com"60 assert url.path == "/"616263def test_url_no_authority():64 url = httpx.URL("http://")65 assert url.scheme == "http"66 assert url.host == ""67 assert url.path == "/"686970# Tests for percent encoding across path, query, and fragment...7172

Cleanup URL percent-encoding behavior. (#2990)Tom Christiebefore adoptiona11fc38

lines 73–140why, one step back
73@pytest.mark.parametrize(
74 "url,raw_path,path,query,fragment",75 [76 # URL with unescaped chars in path.77 (78 "https://example.com/!$&'()*+,;= abc ABC 123 :/[]@",79 b"/!$&'()*+,;=%20abc%20ABC%20123%20:/[]@",80 "/!$&'()*+,;= abc ABC 123 :/[]@",81 b"",82 "",83 ),84 # URL with escaped chars in path.85 (86 "https://example.com/!$&'()*+,;=%20abc%20ABC%20123%20:/[]@",87 b"/!$&'()*+,;=%20abc%20ABC%20123%20:/[]@",88 "/!$&'()*+,;= abc ABC 123 :/[]@",89 b"",90 "",91 ),92 # URL with mix of unescaped and escaped chars in path.93 # WARNING: This has the incorrect behaviour, adding the test as an interim step.94 (95 "https://example.com/ %61%62%63",96 b"/%20%61%62%63",97 "/ abc",98 b"",99 "",100 ),101 # URL with unescaped chars in query.102 (103 "https://example.com/?!$&'()*+,;= abc ABC 123 :/[]@?",104 b"/?!$&'()*+,;=%20abc%20ABC%20123%20:/[]@?",105 "/",106 b"!$&'()*+,;=%20abc%20ABC%20123%20:/[]@?",107 "",108 ),109 # URL with escaped chars in query.110 (111 "https://example.com/?!$&%27()*+,;=%20abc%20ABC%20123%20:%2F[]@?",112 b"/?!$&%27()*+,;=%20abc%20ABC%20123%20:%2F[]@?",113 "/",114 b"!$&%27()*+,;=%20abc%20ABC%20123%20:%2F[]@?",115 "",116 ),117 # URL with mix of unescaped and escaped chars in query.118 (119 "https://example.com/?%20%97%98%99",120 b"/?%20%97%98%99",121 "/",122 b"%20%97%98%99",123 "",124 ),125 # URL encoding characters in fragment.126 (127 "https://example.com/#!$&'()*+,;= abc ABC 123 :/[]@?#",128 b"/",129 "/",130 b"",131 "!$&'()*+,;= abc ABC 123 :/[]@?#",132 ),133 ],134)135def test_path_query_fragment(url, raw_path, path, query, fragment):136 url = httpx.URL(url)137 assert url.raw_path == raw_path138 assert url.path == path139 assert url.query == query140 assert url.fragment == fragment

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 141–142why, one step back
141
142

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 143–144why, one step back
143def test_url_query_encoding():
144 """

Cleanup URL percent-encoding behavior. (#2990)Tom Christiebefore adoptiona11fc38

line 145why, one step back
145 URL query parameters should use '%20' for encoding spaces,

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 146–152why, one step back
146 and should treat '/' as a safe character. This behaviour differs
147 across clients, but we're matching browser behaviour here.148149 See https://github.com/encode/httpx/issues/2536150 and https://github.com/encode/httpx/discussions/2460151 """152 url = httpx.URL("https://www.example.com/?a=b c&d=e/f")

Cleanup URL percent-encoding behavior. (#2990)Tom Christiebefore adoptiona11fc38

line 153why, one step back
153 assert url.raw_path == b"/?a=b%20c&d=e/f"

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 154–158why, one step back
154
155 url = httpx.URL("https://www.example.com/", params={"a": "b c", "d": "e/f"})156 assert url.raw_path == b"/?a=b%20c&d=e%2Ff"157158

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 159–162why, one step back
159def test_url_params():
160 url = httpx.URL("https://example.org:123/path/to/somewhere", params={"a": "123"})161 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"162 assert url.params == httpx.QueryParams({"a": "123"})

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

line 163why, one step back
163

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 164–168why, one step back
164 url = httpx.URL(
165 "https://example.org:123/path/to/somewhere?b=456", params={"a": "123"}166 )167 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"168 assert url.params == httpx.QueryParams({"a": "123"})

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 169–170why, one step back
169
170

Allow URLs where username or password contains unescaped '@'. (#2986)Tom Christiebefore adoptionb471f01

lines 171–218why, one step back
171# Tests for username and password
172173174@pytest.mark.parametrize(175 "url,userinfo,username,password",176 [177 # username and password in URL.178 (179 "https://username:password@example.com",180 b"username:password",181 "username",182 "password",183 ),184 # username and password in URL with percent escape sequences.185 (186 "https://username%40gmail.com:pa%20ssword@example.com",187 b"username%40gmail.com:pa%20ssword",188 "username@gmail.com",189 "pa ssword",190 ),191 (192 "https://user%20name:p%40ssword@example.com",193 b"user%20name:p%40ssword",194 "user name",195 "p@ssword",196 ),197 # username and password in URL without percent escape sequences.198 (199 "https://username@gmail.com:pa ssword@example.com",200 b"username%40gmail.com:pa%20ssword",201 "username@gmail.com",202 "pa ssword",203 ),204 (205 "https://user name:p@ssword@example.com",206 b"user%20name:p%40ssword",207 "user name",208 "p@ssword",209 ),210 ],211)212def test_url_username_and_password(url, userinfo, username, password):213 url = httpx.URL(url)214 assert url.userinfo == userinfo215 assert url.username == username216 assert url.password == password217218

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 219–231why, one step back
219# Tests for different host types
220221222def test_url_valid_host():223 url = httpx.URL("https://example.com/")224 assert url.host == "example.com"225226227def test_url_normalized_host():228 url = httpx.URL("https://EXAMPLE.com/")229 assert url.host == "example.com"230231

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

line 232why, one step back
232def test_url_percent_escape_host():

Add URL parsing tests from WHATWG (#3188)Tom Christiebefore adoptiondb9072f

lines 233–234why, one step back
233 url = httpx.URL("https://exam le.com/")
234 assert url.host == "exam%20le.com"

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

lines 235–236why, one step back
235
236

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 237–285why, one step back
237def test_url_ipv4_like_host():
238 """rare host names used to quality as IPv4"""239 url = httpx.URL("https://023b76x43144/")240 assert url.host == "023b76x43144"241242243# Tests for different port types244245246def test_url_valid_port():247 url = httpx.URL("https://example.com:123/")248 assert url.port == 123249250251def test_url_normalized_port():252 # If the port matches the scheme default it is normalized to None.253 url = httpx.URL("https://example.com:443/")254 assert url.port is None255256257def test_url_invalid_port():258 with pytest.raises(httpx.InvalidURL) as exc:259 httpx.URL("https://example.com:abc/")260 assert str(exc.value) == "Invalid port: 'abc'"261262263# Tests for path handling264265266def test_url_normalized_path():267 url = httpx.URL("https://example.com/abc/def/../ghi/./jkl")268 assert url.path == "/abc/ghi/jkl"269270271def test_url_escaped_path():272 url = httpx.URL("https://example.com/ /🌟/")273 assert url.raw_path == b"/%20/%F0%9F%8C%9F/"274275276def test_url_leading_dot_prefix_on_absolute_url():277 url = httpx.URL("https://example.com/../abc")278 assert url.path == "/abc"279280281def test_url_leading_dot_prefix_on_relative_url():282 url = httpx.URL("../abc")283 assert url.path == "../abc"284285

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

lines 286–288why, one step back
286# Tests for query parameter percent encoding.
287#288# Percent-encoding in `params={}` should match browser form behavior.

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 289–290why, one step back
289
290

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

lines 291–292why, one step back
291def test_param_with_space():
292 # Params passed as form key-value pairs should be escaped.

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 293–297why, one step back
293 url = httpx.URL("http://webservice", params={"u": "with spaces"})
294 assert str(url) == "http://webservice?u=with%20spaces"295296297def test_param_does_not_require_encoding():

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

lines 298–306why, one step back
298 # Params passed as form key-value pairs should be escaped.
299 url = httpx.URL("http://webservice", params={"u": "%"})300 assert str(url) == "http://webservice?u=%25"301302303def test_param_with_percent_encoded():304 # Params passed as form key-value pairs should always be escaped,305 # even if they include a valid escape sequence.306 # We want to match browser form behaviour here.

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

line 307why, one step back
307 url = httpx.URL("http://webservice", params={"u": "with%20spaces"})

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

line 308why, one step back
308 assert str(url) == "http://webservice?u=with%2520spaces"

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 309–311why, one step back
309
310311def test_param_with_existing_escape_requires_encoding():

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

lines 312–314why, one step back
312 # Params passed as form key-value pairs should always be escaped,
313 # even if they include a valid escape sequence.314 # We want to match browser form behaviour here.

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 315–316why, one step back
315 url = httpx.URL("http://webservice", params={"u": "http://example.com?q=foo%2Fa"})
316 assert str(url) == "http://webservice?u=http%3A%2F%2Fexample.com%3Fq%3Dfoo%252Fa"

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

lines 317–341why, one step back
317
318319# Tests for query parameter percent encoding.320#321# Percent-encoding in `url={}` should match browser URL bar behavior.322323324def test_query_with_existing_percent_encoding():325 # Valid percent encoded sequences should not be double encoded.326 url = httpx.URL("http://webservice?u=phrase%20with%20spaces")327 assert str(url) == "http://webservice?u=phrase%20with%20spaces"328329330def test_query_requiring_percent_encoding():331 # Characters that require percent encoding should be encoded.332 url = httpx.URL("http://webservice?u=phrase with spaces")333 assert str(url) == "http://webservice?u=phrase%20with%20spaces"334335336def test_query_with_mixed_percent_encoding():337 # When a mix of encoded and unencoded characters are present,338 # characters that require percent encoding should be encoded,339 # while existing sequences should not be double encoded.340 url = httpx.URL("http://webservice?u=phrase%20with spaces")341 assert str(url) == "http://webservice?u=phrase%20with%20spaces"

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 342–347why, one step back
342
343344# Tests for invalid URLs345346347def test_url_invalid_hostname():

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 348–354why, one step back
348 """
349 Ensure that invalid URLs raise an `httpx.InvalidURL` exception.350 """351 with pytest.raises(httpx.InvalidURL):352 httpx.URL("https://😇/")353354

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 355–417why, one step back
355def test_url_excessively_long_url():
356 with pytest.raises(httpx.InvalidURL) as exc:357 httpx.URL("https://www.example.com/" + "x" * 100_000)358 assert str(exc.value) == "URL too long"359360361def test_url_excessively_long_component():362 with pytest.raises(httpx.InvalidURL) as exc:363 httpx.URL("https://www.example.com", path="/" + "x" * 100_000)364 assert str(exc.value) == "URL component 'path' too long"365366367def test_url_non_printing_character_in_url():368 with pytest.raises(httpx.InvalidURL) as exc:369 httpx.URL("https://www.example.com/\n")370 assert str(exc.value) == "Invalid non-printable ASCII character in URL"371372373def test_url_non_printing_character_in_component():374 with pytest.raises(httpx.InvalidURL) as exc:375 httpx.URL("https://www.example.com", path="/\n")376 assert (377 str(exc.value)378 == "Invalid non-printable ASCII character in URL component 'path'"379 )380381382# Test for url components383384385def test_url_with_components():386 url = httpx.URL(scheme="https", host="www.example.com", path="/")387388 assert url.scheme == "https"389 assert url.userinfo == b""390 assert url.host == "www.example.com"391 assert url.port is None392 assert url.path == "/"393 assert url.query == b""394 assert url.fragment == ""395396 assert str(url) == "https://www.example.com/"397398399def test_urlparse_with_invalid_component():400 with pytest.raises(TypeError) as exc:401 httpx.URL(scheme="https", host="www.example.com", incorrect="/")402 assert str(exc.value) == "'incorrect' is an invalid keyword argument for URL()"403404405def test_urlparse_with_invalid_scheme():406 with pytest.raises(httpx.InvalidURL) as exc:407 httpx.URL(scheme="~", host="www.example.com", path="/")408 assert str(exc.value) == "Invalid URL component 'scheme'"409410411def test_urlparse_with_invalid_path():412 with pytest.raises(httpx.InvalidURL) as exc:413 httpx.URL(scheme="https", host="www.example.com", path="abc")414 assert str(exc.value) == "For absolute URLs, path must be empty or begin with '/'"415416 with pytest.raises(httpx.InvalidURL) as exc:417 httpx.URL(path="//abc")

Add URL parsing tests from WHATWG (#3188)Tom Christiebefore adoptiondb9072f

line 418why, one step back
418 assert str(exc.value) == "Relative URLs cannot have a path starting with '//'"

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 419–421why, one step back
419
420 with pytest.raises(httpx.InvalidURL) as exc:421 httpx.URL(path=":abc")

Add URL parsing tests from WHATWG (#3188)Tom Christiebefore adoptiondb9072f

line 422why, one step back
422 assert str(exc.value) == "Relative URLs cannot have a path starting with ':'"

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 423–433why, one step back
423
424425def test_url_with_relative_path():426 # This path would be invalid for an absolute URL, but is valid as a relative URL.427 url = httpx.URL(path="abc")428 assert url.path == "abc"429430431# Tests for `httpx.URL` python built-in operators.432433

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 434why, one step back
434def test_url_eq_str():

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

line 435why, one step back
435 """

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

line 436why, one step back
436 Ensure that `httpx.URL` supports the equality operator.

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

line 437why, one step back
437 """

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 438–442why, one step back
438 url = httpx.URL("https://example.org:123/path/to/somewhere?abc=123#anchor")
439 assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"440 assert str(url) == url441442

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 443–456why, one step back
443def test_url_set():
444 """445 Ensure that `httpx.URL` instances can be used in sets.446 """447 urls = (448 httpx.URL("http://example.org:123/path/to/somewhere"),449 httpx.URL("http://example.org:123/path/to/somewhere/else"),450 )451452 url_set = set(urls)453454 assert all(url in urls for url in url_set)455456

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 457–459why, one step back
457# Tests for TypeErrors when instantiating `httpx.URL`.
458459

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 460–471why, one step back
460def test_url_invalid_type():
461 """462 Ensure that invalid types on `httpx.URL()` raise a `TypeError`.463 """464465 class ExternalURLClass: # representing external URL class466 pass467468 with pytest.raises(TypeError):469 httpx.URL(ExternalURLClass()) # type: ignore470471

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 472–475why, one step back
472def test_url_with_invalid_component():
473 with pytest.raises(TypeError) as exc:474 httpx.URL(scheme="https", host="www.example.com", incorrect="/")475 assert str(exc.value) == "'incorrect' is an invalid keyword argument for URL()"

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 476–480why, one step back
476
477478# Tests for `URL.join()`.479480

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 481–558why, one step back
481def test_url_join():
482 """483 Some basic URL joining tests.484 """485 url = httpx.URL("https://example.org:123/path/to/somewhere")486 assert url.join("/somewhere-else") == "https://example.org:123/somewhere-else"487 assert (488 url.join("somewhere-else") == "https://example.org:123/path/to/somewhere-else"489 )490 assert (491 url.join("../somewhere-else") == "https://example.org:123/path/somewhere-else"492 )493 assert url.join("../../somewhere-else") == "https://example.org:123/somewhere-else"494495496def test_relative_url_join():497 url = httpx.URL("/path/to/somewhere")498 assert url.join("/somewhere-else") == "/somewhere-else"499 assert url.join("somewhere-else") == "/path/to/somewhere-else"500 assert url.join("../somewhere-else") == "/path/somewhere-else"501 assert url.join("../../somewhere-else") == "/somewhere-else"502503504def test_url_join_rfc3986():505 """506 URL joining tests, as-per reference examples in RFC 3986.507508 https://tools.ietf.org/html/rfc3986#section-5.4509 """510511 url = httpx.URL("http://example.com/b/c/d;p?q")512513 assert url.join("g") == "http://example.com/b/c/g"514 assert url.join("./g") == "http://example.com/b/c/g"515 assert url.join("g/") == "http://example.com/b/c/g/"516 assert url.join("/g") == "http://example.com/g"517 assert url.join("//g") == "http://g"518 assert url.join("?y") == "http://example.com/b/c/d;p?y"519 assert url.join("g?y") == "http://example.com/b/c/g?y"520 assert url.join("#s") == "http://example.com/b/c/d;p?q#s"521 assert url.join("g#s") == "http://example.com/b/c/g#s"522 assert url.join("g?y#s") == "http://example.com/b/c/g?y#s"523 assert url.join(";x") == "http://example.com/b/c/;x"524 assert url.join("g;x") == "http://example.com/b/c/g;x"525 assert url.join("g;x?y#s") == "http://example.com/b/c/g;x?y#s"526 assert url.join("") == "http://example.com/b/c/d;p?q"527 assert url.join(".") == "http://example.com/b/c/"528 assert url.join("./") == "http://example.com/b/c/"529 assert url.join("..") == "http://example.com/b/"530 assert url.join("../") == "http://example.com/b/"531 assert url.join("../g") == "http://example.com/b/g"532 assert url.join("../..") == "http://example.com/"533 assert url.join("../../") == "http://example.com/"534 assert url.join("../../g") == "http://example.com/g"535536 assert url.join("../../../g") == "http://example.com/g"537 assert url.join("../../../../g") == "http://example.com/g"538539 assert url.join("/./g") == "http://example.com/g"540 assert url.join("/../g") == "http://example.com/g"541 assert url.join("g.") == "http://example.com/b/c/g."542 assert url.join(".g") == "http://example.com/b/c/.g"543 assert url.join("g..") == "http://example.com/b/c/g.."544 assert url.join("..g") == "http://example.com/b/c/..g"545546 assert url.join("./../g") == "http://example.com/b/g"547 assert url.join("./g/.") == "http://example.com/b/c/g/"548 assert url.join("g/./h") == "http://example.com/b/c/g/h"549 assert url.join("g/../h") == "http://example.com/b/c/h"550 assert url.join("g;x=1/./y") == "http://example.com/b/c/g;x=1/y"551 assert url.join("g;x=1/../y") == "http://example.com/b/c/y"552553 assert url.join("g?y/./x") == "http://example.com/b/c/g?y/./x"554 assert url.join("g?y/../x") == "http://example.com/b/c/g?y/../x"555 assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"556 assert url.join("g#s/../x") == "http://example.com/b/c/g#s/../x"557558

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 559–564why, one step back
559def test_resolution_error_1833():
560 """561 See https://github.com/encode/httpx/issues/1833562 """563 url = httpx.URL("https://example.com/?[]")564 assert url.join("/") == "https://example.com/"

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 565–566why, one step back
565
566

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

line 567why, one step back
567# Tests for `URL.copy_with()`.

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 568–569why, one step back
568
569

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 570–586why, one step back
570def test_copy_with():
571 url = httpx.URL("https://www.example.com/")572 assert str(url) == "https://www.example.com/"573574 url = url.copy_with()575 assert str(url) == "https://www.example.com/"576577 url = url.copy_with(scheme="http")578 assert str(url) == "http://www.example.com/"579580 url = url.copy_with(netloc=b"example.com")581 assert str(url) == "http://example.com/"582583 url = url.copy_with(path="/abc")584 assert str(url) == "http://example.com/abc"585586

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 587–677why, one step back
587def test_url_copywith_authority_subcomponents():
588 copy_with_kwargs = {589 "username": "username",590 "password": "password",591 "port": 444,592 "host": "example.net",593 }594 url = httpx.URL("https://example.org")595 new = url.copy_with(**copy_with_kwargs)596 assert str(new) == "https://username:password@example.net:444"597598599def test_url_copywith_netloc():600 copy_with_kwargs = {601 "netloc": b"example.net:444",602 }603 url = httpx.URL("https://example.org")604 new = url.copy_with(**copy_with_kwargs)605 assert str(new) == "https://example.net:444"606607608def test_url_copywith_userinfo_subcomponents():609 copy_with_kwargs = {610 "username": "tom@example.org",611 "password": "abc123@ %",612 }613 url = httpx.URL("https://example.org")614 new = url.copy_with(**copy_with_kwargs)615 assert str(new) == "https://tom%40example.org:abc123%40%20%25@example.org"616 assert new.username == "tom@example.org"617 assert new.password == "abc123@ %"618 assert new.userinfo == b"tom%40example.org:abc123%40%20%25"619620621def test_url_copywith_invalid_component():622 url = httpx.URL("https://example.org")623 with pytest.raises(TypeError):624 url.copy_with(pathh="/incorrect-spelling")625 with pytest.raises(TypeError):626 url.copy_with(userinfo="should be bytes")627628629def test_url_copywith_urlencoded_path():630 url = httpx.URL("https://example.org")631 url = url.copy_with(path="/path to somewhere")632 assert url.path == "/path to somewhere"633 assert url.query == b""634 assert url.raw_path == b"/path%20to%20somewhere"635636637def test_url_copywith_query():638 url = httpx.URL("https://example.org")639 url = url.copy_with(query=b"a=123")640 assert url.path == "/"641 assert url.query == b"a=123"642 assert url.raw_path == b"/?a=123"643644645def test_url_copywith_raw_path():646 url = httpx.URL("https://example.org")647 url = url.copy_with(raw_path=b"/some/path")648 assert url.path == "/some/path"649 assert url.query == b""650 assert url.raw_path == b"/some/path"651652 url = httpx.URL("https://example.org")653 url = url.copy_with(raw_path=b"/some/path?")654 assert url.path == "/some/path"655 assert url.query == b""656 assert url.raw_path == b"/some/path?"657658 url = httpx.URL("https://example.org")659 url = url.copy_with(raw_path=b"/some/path?a=123")660 assert url.path == "/some/path"661 assert url.query == b"a=123"662 assert url.raw_path == b"/some/path?a=123"663664665def test_url_copywith_security():666 """667 Prevent unexpected changes on URL after calling copy_with (CVE-2021-41945)668 """669 with pytest.raises(httpx.InvalidURL):670 httpx.URL("https://u:p@[invalid!]//evilHost/path?t=w#tw")671672 url = httpx.URL("https://example.com/path?t=w#tw")673 bad = "https://xxxx:xxxx@xxxxxxx/xxxxx/xxx?x=x#xxxxx"674 with pytest.raises(httpx.InvalidURL):675 url.copy_with(scheme=bad)676677

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 678–683why, one step back
678# Tests for copy-modifying-parameters methods.
679#680# `URL.copy_set_param()`681# `URL.copy_add_param()`682# `URL.copy_remove_param()`683# `URL.copy_merge_params()`

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 684–685why, one step back
684
685

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 686–691why, one step back
686def test_url_set_param_manipulation():
687 """688 Some basic URL query parameter manipulation.689 """690 url = httpx.URL("https://example.org:123/?a=123")691 assert url.copy_set_param("a", "456") == "https://example.org:123/?a=456"

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 692–693why, one step back
692
693

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

line 694why, one step back
694def test_url_add_param_manipulation():

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 695why, one step back
695 """

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

line 696why, one step back
696 Some basic URL query parameter manipulation.

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 697why, one step back
697 """

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 698–699why, one step back
698 url = httpx.URL("https://example.org:123/?a=123")
699 assert url.copy_add_param("a", "456") == "https://example.org:123/?a=123&a=456"

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 700–701why, one step back
700
701

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

line 702why, one step back
702def test_url_remove_param_manipulation():

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 703why, one step back
703 """

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 704–707why, one step back
704 Some basic URL query parameter manipulation.
705 """706 url = httpx.URL("https://example.org:123/?a=123")707 assert url.copy_remove_param("a") == "https://example.org:123/"

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 708why, one step back
708

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 709–710why, one step back
709
710def test_url_merge_params_manipulation():

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 711why, one step back
711 """

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 712–715why, one step back
712 Some basic URL query parameter manipulation.
713 """714 url = httpx.URL("https://example.org:123/?a=123")715 assert url.copy_merge_params({"b": "456"}) == "https://example.org:123/?a=123&b=456"

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 716–717why, one step back
716
717

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

line 718why, one step back
718# Tests for IDNA hostname support.

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 719–720why, one step back
719
720

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 721–788why, one step back
721@pytest.mark.parametrize(
722 "given,idna,host,raw_host,scheme,port",723 [724 (725 "http://中国.icom.museum:80/",726 "http://xn--fiqs8s.icom.museum:80/",727 "中国.icom.museum",728 b"xn--fiqs8s.icom.museum",729 "http",730 None,731 ),732 (733 "http://Königsgäßchen.de",734 "http://xn--knigsgchen-b4a3dun.de",735 "königsgäßchen.de",736 b"xn--knigsgchen-b4a3dun.de",737 "http",738 None,739 ),740 (741 "https://faß.de",742 "https://xn--fa-hia.de",743 "faß.de",744 b"xn--fa-hia.de",745 "https",746 None,747 ),748 (749 "https://βόλος.com:443",750 "https://xn--nxasmm1c.com:443",751 "βόλος.com",752 b"xn--nxasmm1c.com",753 "https",754 None,755 ),756 (757 "http://ශ්‍රී.com:444",758 "http://xn--10cl1a0b660p.com:444",759 "ශ්‍රී.com",760 b"xn--10cl1a0b660p.com",761 "http",762 444,763 ),764 (765 "https://نامه‌ای.com:4433",766 "https://xn--mgba3gch31f060k.com:4433",767 "نامه‌ای.com",768 b"xn--mgba3gch31f060k.com",769 "https",770 4433,771 ),772 ],773 ids=[774 "http_with_port",775 "unicode_tr46_compat",776 "https_without_port",777 "https_with_port",778 "http_with_custom_port",779 "https_with_custom_port",780 ],781)782def test_idna_url(given, idna, host, raw_host, scheme, port):783 url = httpx.URL(given)784 assert url == httpx.URL(idna)785 assert url.host == host786 assert url.raw_host == raw_host787 assert url.scheme == scheme788 assert url.port == port

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 789why, one step back
789

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

line 790why, one step back
790

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 791–820why, one step back
791def test_url_unescaped_idna_host():
792 url = httpx.URL("https://中国.icom.museum/")793 assert url.raw_host == b"xn--fiqs8s.icom.museum"794795796def test_url_escaped_idna_host():797 url = httpx.URL("https://xn--fiqs8s.icom.museum/")798 assert url.raw_host == b"xn--fiqs8s.icom.museum"799800801def test_url_invalid_idna_host():802 with pytest.raises(httpx.InvalidURL) as exc:803 httpx.URL("https://☃.com/")804 assert str(exc.value) == "Invalid IDNA hostname: '☃.com'"805806807# Tests for IPv4 hostname support.808809810def test_url_valid_ipv4():811 url = httpx.URL("https://1.2.3.4/")812 assert url.host == "1.2.3.4"813814815def test_url_invalid_ipv4():816 with pytest.raises(httpx.InvalidURL) as exc:817 httpx.URL("https://999.999.999.999/")818 assert str(exc.value) == "Invalid IPv4 address: '999.999.999.999'"819820

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 821–823why, one step back
821# Tests for IPv6 hostname support.
822823

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 824–830why, one step back
824def test_ipv6_url():
825 url = httpx.URL("http://[::ffff:192.168.0.1]:5678/")826827 assert url.host == "::ffff:192.168.0.1"828 assert url.netloc == b"[::ffff:192.168.0.1]:5678"829830

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 831–841why, one step back
831def test_url_valid_ipv6():
832 url = httpx.URL("https://[2001:db8::ff00:42:8329]/")833 assert url.host == "2001:db8::ff00:42:8329"834835836def test_url_invalid_ipv6():837 with pytest.raises(httpx.InvalidURL) as exc:838 httpx.URL("https://[2001]/")839 assert str(exc.value) == "Invalid IPv6 address: '[2001]'"840841

Reorganise tests in 'test_url.py' (#2981)Tom Christiebefore adoption724eced

lines 842–850why, one step back
842@pytest.mark.parametrize("host", ["[::ffff:192.168.0.1]", "::ffff:192.168.0.1"])
843def test_ipv6_url_from_raw_url(host):844 url = httpx.URL(scheme="https", host=host, port=443, path="/")845846 assert url.host == "::ffff:192.168.0.1"847 assert url.netloc == b"[::ffff:192.168.0.1]"848 assert str(url) == "https://[::ffff:192.168.0.1]/"849850

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 851–865why, one step back
851@pytest.mark.parametrize(
852 "url_str",853 [854 "http://127.0.0.1:1234",855 "http://example.com:1234",856 "http://[::ffff:127.0.0.1]:1234",857 ],858)859@pytest.mark.parametrize("new_host", ["[::ffff:192.168.0.1]", "::ffff:192.168.0.1"])860def test_ipv6_url_copy_with_host(url_str, new_host):861 url = httpx.URL(url_str).copy_with(host=new_host)862863 assert url.host == "::ffff:192.168.0.1"864 assert url.netloc == b"[::ffff:192.168.0.1]:1234"865 assert str(url) == "http://[::ffff:192.168.0.1]:1234"

Moving test cases into 'test_url.py' (#2982)Tom Christiebefore adoption5b5f6d8

lines 866–881why, one step back
866
867868# Test for deprecated API869870871def test_url_raw_compatibility():872 """873 Test case for the (to-be-deprecated) `url.raw` accessor.874 """875 url = httpx.URL("https://www.example.com/path")876 scheme, host, port, raw_path = url.raw877878 assert scheme == b"https"879 assert host == b"www.example.com"880 assert port is None881 assert raw_path == b"/path"