Skip to content
Myr

test_url.py

This file

Path

tests/models/test_url.py

863 lines · 25.9 KiB · Python

Last changed by

Just use default `safe=...` characters for `urlescape` (#3376)Tom Christiebefore adoption5dda2aa

History of this file

Take it

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

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

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.

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

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():

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

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"

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

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 url = httpx.URL("https://www.example.com/?a=b c&d=e/f")

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

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

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

line 146why, one step back
146

Review urlescape percent-safe set, and use + behavior for form spaces. (#3373)Tom Christiebefore adoptionba2e512

lines 147–149why, one step back
147 url = httpx.URL("https://www.example.com/?a=b+c&d=e/f")
148 assert url.raw_path == b"/?a=b+c&d=e/f"149

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

line 150why, one step back
150 url = httpx.URL("https://www.example.com/", params={"a": "b c", "d": "e/f"})

Just use default `safe=...` characters for `urlescape` (#3376)Tom Christiebefore adoption5dda2aa

line 151why, one step back
151 assert url.raw_path == b"/?a=b+c&d=e%2Ff"

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

lines 152–153why, one step back
152
153

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

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

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

line 158why, one step back
158

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

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

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

lines 164–165why, one step back
164
165

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

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

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

lines 214–226why, one step back
214# Tests for different host types
215216217def test_url_valid_host():218 url = httpx.URL("https://example.com/")219 assert url.host == "example.com"220221222def test_url_normalized_host():223 url = httpx.URL("https://EXAMPLE.com/")224 assert url.host == "example.com"225226

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

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

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

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

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

lines 230–231why, one step back
230
231

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

lines 232–280why, one step back
232def test_url_ipv4_like_host():
233 """rare host names used to quality as IPv4"""234 url = httpx.URL("https://023b76x43144/")235 assert url.host == "023b76x43144"236237238# Tests for different port types239240241def test_url_valid_port():242 url = httpx.URL("https://example.com:123/")243 assert url.port == 123244245246def test_url_normalized_port():247 # If the port matches the scheme default it is normalized to None.248 url = httpx.URL("https://example.com:443/")249 assert url.port is None250251252def test_url_invalid_port():253 with pytest.raises(httpx.InvalidURL) as exc:254 httpx.URL("https://example.com:abc/")255 assert str(exc.value) == "Invalid port: 'abc'"256257258# Tests for path handling259260261def test_url_normalized_path():262 url = httpx.URL("https://example.com/abc/def/../ghi/./jkl")263 assert url.path == "/abc/ghi/jkl"264265266def test_url_escaped_path():267 url = httpx.URL("https://example.com/ /🌟/")268 assert url.raw_path == b"/%20/%F0%9F%8C%9F/"269270271def test_url_leading_dot_prefix_on_absolute_url():272 url = httpx.URL("https://example.com/../abc")273 assert url.path == "/abc"274275276def test_url_leading_dot_prefix_on_relative_url():277 url = httpx.URL("../abc")278 assert url.path == "../abc"279280

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

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

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

lines 284–285why, one step back
284
285

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

line 286why, one step back
286def test_param_with_space():

Review urlescape percent-safe set, and use + behavior for form spaces. (#3373)Tom Christiebefore adoptionba2e512

lines 287–288why, one step back
287 # Params passed as form key-value pairs should be form escaped,
288 # Including the special case of "+" for space seperators.

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

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

Review urlescape percent-safe set, and use + behavior for form spaces. (#3373)Tom Christiebefore adoptionba2e512

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

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

lines 291–292why, one step back
291
292

Review URL percent escaping sets, from whatwg. (#3371)Tom Christiebefore adoptiond293374

line 293why, one step back
293def test_param_requires_encoding():

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

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

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

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

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

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

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

lines 305–307why, one step back
305
306307def test_param_with_existing_escape_requires_encoding():

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

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

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

line 311why, one step back
311 url = httpx.URL("http://webservice", params={"u": "http://example.com?q=foo%2Fa"})

Just use default `safe=...` characters for `urlescape` (#3376)Tom Christiebefore adoption5dda2aa

line 312why, one step back
312 assert str(url) == "http://webservice?u=http%3A%2F%2Fexample.com%3Fq%3Dfoo%252Fa"

Resolve queryparam quoting (#3187)Tom Christiebefore adoptiona7092af

lines 313–337why, one step back
313
314315# Tests for query parameter percent encoding.316#317# Percent-encoding in `url={}` should match browser URL bar behavior.318319320def test_query_with_existing_percent_encoding():321 # Valid percent encoded sequences should not be double encoded.322 url = httpx.URL("http://webservice?u=phrase%20with%20spaces")323 assert str(url) == "http://webservice?u=phrase%20with%20spaces"324325326def test_query_requiring_percent_encoding():327 # Characters that require percent encoding should be encoded.328 url = httpx.URL("http://webservice?u=phrase with spaces")329 assert str(url) == "http://webservice?u=phrase%20with%20spaces"330331332def test_query_with_mixed_percent_encoding():333 # When a mix of encoded and unencoded characters are present,334 # characters that require percent encoding should be encoded,335 # while existing sequences should not be double encoded.336 url = httpx.URL("http://webservice?u=phrase%20with spaces")337 assert str(url) == "http://webservice?u=phrase%20with%20spaces"

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

lines 338–343why, one step back
338
339340# Tests for invalid URLs341342343def test_url_invalid_hostname():

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

lines 344–350why, one step back
344 """
345 Ensure that invalid URLs raise an `httpx.InvalidURL` exception.346 """347 with pytest.raises(httpx.InvalidURL):348 httpx.URL("https://😇/")349350

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

lines 351–365why, one step back
351def test_url_excessively_long_url():
352 with pytest.raises(httpx.InvalidURL) as exc:353 httpx.URL("https://www.example.com/" + "x" * 100_000)354 assert str(exc.value) == "URL too long"355356357def test_url_excessively_long_component():358 with pytest.raises(httpx.InvalidURL) as exc:359 httpx.URL("https://www.example.com", path="/" + "x" * 100_000)360 assert str(exc.value) == "URL component 'path' too long"361362363def test_url_non_printing_character_in_url():364 with pytest.raises(httpx.InvalidURL) as exc:365 httpx.URL("https://www.example.com/\n")

Improve InvalidURL error message. (#3250)Tom Christiebefore adoption7c0cda1

lines 366–368why, one step back
366 assert str(exc.value) == (
367 "Invalid non-printable ASCII character in URL, '\\n' at position 24."368 )

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

lines 369–373why, one step back
369
370371def test_url_non_printing_character_in_component():372 with pytest.raises(httpx.InvalidURL) as exc:373 httpx.URL("https://www.example.com", path="/\n")

Improve InvalidURL error message. (#3250)Tom Christiebefore adoption7c0cda1

lines 374–376why, one step back
374 assert str(exc.value) == (
375 "Invalid non-printable ASCII character in URL path component, "376 "'\\n' at position 1."

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

lines 377–415why, one step back
377 )
378379380# Test for url components381382383def test_url_with_components():384 url = httpx.URL(scheme="https", host="www.example.com", path="/")385386 assert url.scheme == "https"387 assert url.userinfo == b""388 assert url.host == "www.example.com"389 assert url.port is None390 assert url.path == "/"391 assert url.query == b""392 assert url.fragment == ""393394 assert str(url) == "https://www.example.com/"395396397def test_urlparse_with_invalid_component():398 with pytest.raises(TypeError) as exc:399 httpx.URL(scheme="https", host="www.example.com", incorrect="/")400 assert str(exc.value) == "'incorrect' is an invalid keyword argument for URL()"401402403def test_urlparse_with_invalid_scheme():404 with pytest.raises(httpx.InvalidURL) as exc:405 httpx.URL(scheme="~", host="www.example.com", path="/")406 assert str(exc.value) == "Invalid URL component 'scheme'"407408409def test_urlparse_with_invalid_path():410 with pytest.raises(httpx.InvalidURL) as exc:411 httpx.URL(scheme="https", host="www.example.com", path="abc")412 assert str(exc.value) == "For absolute URLs, path must be empty or begin with '/'"413414 with pytest.raises(httpx.InvalidURL) as exc:415 httpx.URL(path="//abc")

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

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

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

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

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

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

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

lines 421–431why, one step back
421
422423def test_url_with_relative_path():424 # This path would be invalid for an absolute URL, but is valid as a relative URL.425 url = httpx.URL(path="abc")426 assert url.path == "abc"427428429# Tests for `httpx.URL` python built-in operators.430431

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

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

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

line 433why, one step back
433 """

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

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

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

line 435why, one step back
435 """

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

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

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

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

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

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

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

lines 458–469why, one step back
458def test_url_invalid_type():
459 """460 Ensure that invalid types on `httpx.URL()` raise a `TypeError`.461 """462463 class ExternalURLClass: # representing external URL class464 pass465466 with pytest.raises(TypeError):467 httpx.URL(ExternalURLClass()) # type: ignore468469

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

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

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

lines 474–478why, one step back
474
475476# Tests for `URL.join()`.477478

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

lines 479–556why, one step back
479def test_url_join():
480 """481 Some basic URL joining tests.482 """483 url = httpx.URL("https://example.org:123/path/to/somewhere")484 assert url.join("/somewhere-else") == "https://example.org:123/somewhere-else"485 assert (486 url.join("somewhere-else") == "https://example.org:123/path/to/somewhere-else"487 )488 assert (489 url.join("../somewhere-else") == "https://example.org:123/path/somewhere-else"490 )491 assert url.join("../../somewhere-else") == "https://example.org:123/somewhere-else"492493494def test_relative_url_join():495 url = httpx.URL("/path/to/somewhere")496 assert url.join("/somewhere-else") == "/somewhere-else"497 assert url.join("somewhere-else") == "/path/to/somewhere-else"498 assert url.join("../somewhere-else") == "/path/somewhere-else"499 assert url.join("../../somewhere-else") == "/somewhere-else"500501502def test_url_join_rfc3986():503 """504 URL joining tests, as-per reference examples in RFC 3986.505506 https://tools.ietf.org/html/rfc3986#section-5.4507 """508509 url = httpx.URL("http://example.com/b/c/d;p?q")510511 assert url.join("g") == "http://example.com/b/c/g"512 assert url.join("./g") == "http://example.com/b/c/g"513 assert url.join("g/") == "http://example.com/b/c/g/"514 assert url.join("/g") == "http://example.com/g"515 assert url.join("//g") == "http://g"516 assert url.join("?y") == "http://example.com/b/c/d;p?y"517 assert url.join("g?y") == "http://example.com/b/c/g?y"518 assert url.join("#s") == "http://example.com/b/c/d;p?q#s"519 assert url.join("g#s") == "http://example.com/b/c/g#s"520 assert url.join("g?y#s") == "http://example.com/b/c/g?y#s"521 assert url.join(";x") == "http://example.com/b/c/;x"522 assert url.join("g;x") == "http://example.com/b/c/g;x"523 assert url.join("g;x?y#s") == "http://example.com/b/c/g;x?y#s"524 assert url.join("") == "http://example.com/b/c/d;p?q"525 assert url.join(".") == "http://example.com/b/c/"526 assert url.join("./") == "http://example.com/b/c/"527 assert url.join("..") == "http://example.com/b/"528 assert url.join("../") == "http://example.com/b/"529 assert url.join("../g") == "http://example.com/b/g"530 assert url.join("../..") == "http://example.com/"531 assert url.join("../../") == "http://example.com/"532 assert url.join("../../g") == "http://example.com/g"533534 assert url.join("../../../g") == "http://example.com/g"535 assert url.join("../../../../g") == "http://example.com/g"536537 assert url.join("/./g") == "http://example.com/g"538 assert url.join("/../g") == "http://example.com/g"539 assert url.join("g.") == "http://example.com/b/c/g."540 assert url.join(".g") == "http://example.com/b/c/.g"541 assert url.join("g..") == "http://example.com/b/c/g.."542 assert url.join("..g") == "http://example.com/b/c/..g"543544 assert url.join("./../g") == "http://example.com/b/g"545 assert url.join("./g/.") == "http://example.com/b/c/g/"546 assert url.join("g/./h") == "http://example.com/b/c/g/h"547 assert url.join("g/../h") == "http://example.com/b/c/h"548 assert url.join("g;x=1/./y") == "http://example.com/b/c/g;x=1/y"549 assert url.join("g;x=1/../y") == "http://example.com/b/c/y"550551 assert url.join("g?y/./x") == "http://example.com/b/c/g?y/./x"552 assert url.join("g?y/../x") == "http://example.com/b/c/g?y/../x"553 assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"554 assert url.join("g#s/../x") == "http://example.com/b/c/g#s/../x"555556

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

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

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

lines 563–564why, one step back
563
564

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

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

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

lines 566–567why, one step back
566
567

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

lines 568–584why, one step back
568def test_copy_with():
569 url = httpx.URL("https://www.example.com/")570 assert str(url) == "https://www.example.com/"571572 url = url.copy_with()573 assert str(url) == "https://www.example.com/"574575 url = url.copy_with(scheme="http")576 assert str(url) == "http://www.example.com/"577578 url = url.copy_with(netloc=b"example.com")579 assert str(url) == "http://example.com/"580581 url = url.copy_with(path="/abc")582 assert str(url) == "http://example.com/abc"583584

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

lines 585–612why, one step back
585def test_url_copywith_authority_subcomponents():
586 copy_with_kwargs = {587 "username": "username",588 "password": "password",589 "port": 444,590 "host": "example.net",591 }592 url = httpx.URL("https://example.org")593 new = url.copy_with(**copy_with_kwargs)594 assert str(new) == "https://username:password@example.net:444"595596597def test_url_copywith_netloc():598 copy_with_kwargs = {599 "netloc": b"example.net:444",600 }601 url = httpx.URL("https://example.org")602 new = url.copy_with(**copy_with_kwargs)603 assert str(new) == "https://example.net:444"604605606def test_url_copywith_userinfo_subcomponents():607 copy_with_kwargs = {608 "username": "tom@example.org",609 "password": "abc123@ %",610 }611 url = httpx.URL("https://example.org")612 new = url.copy_with(**copy_with_kwargs)

Review URL percent escaping sets, from whatwg. (#3371)Tom Christiebefore adoptiond293374

line 613why, one step back
613 assert str(new) == "https://tom%40example.org:abc123%40%20%@example.org"

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

lines 614–615why, one step back
614 assert new.username == "tom@example.org"
615 assert new.password == "abc123@ %"

Review URL percent escaping sets, from whatwg. (#3371)Tom Christiebefore adoptiond293374

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

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

lines 617–675why, one step back
617
618619def test_url_copywith_invalid_component():620 url = httpx.URL("https://example.org")621 with pytest.raises(TypeError):622 url.copy_with(pathh="/incorrect-spelling")623 with pytest.raises(TypeError):624 url.copy_with(userinfo="should be bytes")625626627def test_url_copywith_urlencoded_path():628 url = httpx.URL("https://example.org")629 url = url.copy_with(path="/path to somewhere")630 assert url.path == "/path to somewhere"631 assert url.query == b""632 assert url.raw_path == b"/path%20to%20somewhere"633634635def test_url_copywith_query():636 url = httpx.URL("https://example.org")637 url = url.copy_with(query=b"a=123")638 assert url.path == "/"639 assert url.query == b"a=123"640 assert url.raw_path == b"/?a=123"641642643def test_url_copywith_raw_path():644 url = httpx.URL("https://example.org")645 url = url.copy_with(raw_path=b"/some/path")646 assert url.path == "/some/path"647 assert url.query == b""648 assert url.raw_path == b"/some/path"649650 url = httpx.URL("https://example.org")651 url = url.copy_with(raw_path=b"/some/path?")652 assert url.path == "/some/path"653 assert url.query == b""654 assert url.raw_path == b"/some/path?"655656 url = httpx.URL("https://example.org")657 url = url.copy_with(raw_path=b"/some/path?a=123")658 assert url.path == "/some/path"659 assert url.query == b"a=123"660 assert url.raw_path == b"/some/path?a=123"661662663def test_url_copywith_security():664 """665 Prevent unexpected changes on URL after calling copy_with (CVE-2021-41945)666 """667 with pytest.raises(httpx.InvalidURL):668 httpx.URL("https://u:p@[invalid!]//evilHost/path?t=w#tw")669670 url = httpx.URL("https://example.com/path?t=w#tw")671 bad = "https://xxxx:xxxx@xxxxxxx/xxxxx/xxx?x=x#xxxxx"672 with pytest.raises(httpx.InvalidURL):673 url.copy_with(scheme=bad)674675

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

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

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

lines 682–683why, one step back
682
683

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

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

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

lines 690–691why, one step back
690
691

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

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

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

line 693why, one step back
693 """

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

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

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

line 695why, one step back
695 """

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

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

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

lines 698–699why, one step back
698
699

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

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

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

line 701why, one step back
701 """

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

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

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

line 706why, one step back
706

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

lines 707–708why, one step back
707
708def test_url_merge_params_manipulation():

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

line 709why, one step back
709 """

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

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

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

lines 714–715why, one step back
714
715

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

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

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

lines 717–718why, one step back
717
718

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

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

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

line 787why, one step back
787

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

line 788why, one step back
788

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

lines 789–818why, one step back
789def test_url_unescaped_idna_host():
790 url = httpx.URL("https://中国.icom.museum/")791 assert url.raw_host == b"xn--fiqs8s.icom.museum"792793794def test_url_escaped_idna_host():795 url = httpx.URL("https://xn--fiqs8s.icom.museum/")796 assert url.raw_host == b"xn--fiqs8s.icom.museum"797798799def test_url_invalid_idna_host():800 with pytest.raises(httpx.InvalidURL) as exc:801 httpx.URL("https://☃.com/")802 assert str(exc.value) == "Invalid IDNA hostname: '☃.com'"803804805# Tests for IPv4 hostname support.806807808def test_url_valid_ipv4():809 url = httpx.URL("https://1.2.3.4/")810 assert url.host == "1.2.3.4"811812813def test_url_invalid_ipv4():814 with pytest.raises(httpx.InvalidURL) as exc:815 httpx.URL("https://999.999.999.999/")816 assert str(exc.value) == "Invalid IPv4 address: '999.999.999.999'"817818

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

lines 819–821why, one step back
819# Tests for IPv6 hostname support.
820821

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

lines 822–828why, one step back
822def test_ipv6_url():
823 url = httpx.URL("http://[::ffff:192.168.0.1]:5678/")824825 assert url.host == "::ffff:192.168.0.1"826 assert url.netloc == b"[::ffff:192.168.0.1]:5678"827828

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

lines 829–839why, one step back
829def test_url_valid_ipv6():
830 url = httpx.URL("https://[2001:db8::ff00:42:8329]/")831 assert url.host == "2001:db8::ff00:42:8329"832833834def test_url_invalid_ipv6():835 with pytest.raises(httpx.InvalidURL) as exc:836 httpx.URL("https://[2001]/")837 assert str(exc.value) == "Invalid IPv6 address: '[2001]'"838839

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

lines 840–848why, one step back
840@pytest.mark.parametrize("host", ["[::ffff:192.168.0.1]", "::ffff:192.168.0.1"])
841def test_ipv6_url_from_raw_url(host):842 url = httpx.URL(scheme="https", host=host, port=443, path="/")843844 assert url.host == "::ffff:192.168.0.1"845 assert url.netloc == b"[::ffff:192.168.0.1]"846 assert str(url) == "https://[::ffff:192.168.0.1]/"847848

Bump ruff from 0.0.260 to 0.0.275 (#2753)dependabot[bot]before adoption9d022c0

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