Skip to content
Myr

Reorganise tests in 'test_url.py' (#2981)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Reorganise tests in 'test_url.py' * Linting

authored by Tom Christie · committed by GitHub · parent 9ef08c7 · Browse files · 1 file+209 added−172 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+209 added−172 removed

  • tests:+209 added−172 removed

1 file+209 added−172 removed

modified:tests/models/test_url.py+209 added-172 removed
1 unmodified line
23import httpx456@pytest.mark.parametrize(7 "given,idna,host,raw_host,scheme,port",8 [9 (10 "http://中国.icom.museum:80/",11 "http://xn--fiqs8s.icom.museum:80/",12 "中国.icom.museum",13 b"xn--fiqs8s.icom.museum",14 "http",15 None,16 ),17 (18 "http://Königsgäßchen.de",19 "http://xn--knigsgchen-b4a3dun.de",20 "königsgäßchen.de",21 b"xn--knigsgchen-b4a3dun.de",22 "http",23 None,24 ),25 (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 (34 "https://βόλος.com:443",35 "https://xn--nxasmm1c.com:443",36 "βόλος.com",37 b"xn--nxasmm1c.com",38 "https",39 None,40 ),41 (42 "http://ශ්‍රී.com:444",43 "http://xn--10cl1a0b660p.com:444",44 "ශ්‍රී.com",45 b"xn--10cl1a0b660p.com",46 "http",47 444,48 ),49 (50 "https://نامه‌ای.com:4433",51 "https://xn--mgba3gch31f060k.com:4433",52 "نامه‌ای.com",53 b"xn--mgba3gch31f060k.com",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)67def test_idna_url(given, idna, host, raw_host, scheme, port):68 url = httpx.URL(given)69 assert url == httpx.URL(idna)70 assert url.host == host71 assert url.raw_host == raw_host72 assert url.scheme == scheme73 assert url.port == port74756def test_url():7 url = httpx.URL("https://example.org:123/path/to/somewhere?abc=123#anchor")8 assert url.scheme == "https"9 assert url.host == "example.org"
10 unmodified lines
20 assert new == httpx.URL("http://example.org/path/to/somewhere?abc=123#anchor")21 assert new.scheme == "http"222324def test_url_with_empty_query():25 """26 URLs with and without a trailing `?` but an empty query component27 should preserve the information on the raw path.28 """29 url = httpx.URL("https://www.example.com/path")30 assert url.path == "/path"31 assert url.query == b""32 assert url.raw_path == b"/path"3334 url = httpx.URL("https://www.example.com/path?")35 assert url.path == "/path"36 assert url.query == b""37 assert url.raw_path == b"/path?"383940def test_url_query_encoding():41 """42 URL query parameters should use '%20' to encoding spaces,43 and should treat '/' as a safe character. This behaviour differs44 across clients, but we're matching browser behaviour here.4546 See https://github.com/encode/httpx/issues/253647 and https://github.com/encode/httpx/discussions/246048 """49 url = httpx.URL("https://www.example.com/?a=b c&d=e/f")50 assert url.raw_path == b"/?a=b%20c&d=e%2Ff"5152 url = httpx.URL("https://www.example.com/", params={"a": "b c", "d": "e/f"})53 assert url.raw_path == b"/?a=b%20c&d=e%2Ff"545556def test_url_with_url_encoded_path():57 url = httpx.URL("https://www.example.com/path%20to%20somewhere")58 assert url.path == "/path to somewhere"59 assert url.query == b""60 assert url.raw_path == b"/path%20to%20somewhere"616263def test_url_raw_compatibility():64 url = httpx.URL("https://www.example.com/path")65 scheme, host, port, raw_path = url.raw6667 assert scheme == b"https"68 assert host == b"www.example.com"69 assert port is None70 assert raw_path == b"/path"717273def test_url_invalid():74 """75 Ensure that invalid URLs raise an `httpx.InvalidURL` exception.76 """77 with pytest.raises(httpx.InvalidURL):78 httpx.URL("https://😇/")798081def test_url_eq_str():82 """83 Ensure that `httpx.URL` supports the equality operator,84 and can be compared against plain strings.85 """86 url = httpx.URL("https://example.org:123/path/to/somewhere?abc=123#anchor")87 assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"88 assert str(url) == url899091def test_url_set():92 """93 Ensure that `httpx.URL` instances can be used in sets.94 """95 urls = (96 httpx.URL("http://example.org:123/path/to/somewhere"),97 httpx.URL("http://example.org:123/path/to/somewhere/else"),98 )99100 url_set = set(urls)101102 assert all(url in urls for url in url_set)103104105def test_url_invalid_type():106 """107 Ensure that invalid types on `httpx.URL()` raise a `TypeError`.108 """109110 class ExternalURLClass: # representing external URL class111 pass112113 with pytest.raises(TypeError):114 httpx.URL(ExternalURLClass()) # type: ignore115116117# Tests for `QueryParams`.118119120def test_url_params():121 url = httpx.URL("https://example.org:123/path/to/somewhere", params={"a": "123"})122 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"123 assert url.params == httpx.QueryParams({"a": "123"})
4 unmodified lines
128 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"129 assert url.params == httpx.QueryParams({"a": "123"})130131132# Tests for `URL.join()`.133134135def test_url_join():136 """137 Some basic URL joining tests.138 """
7 unmodified lines
146 )147 assert url.join("../../somewhere-else") == "https://example.org:123/somewhere-else"148149127def 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"157158150def test_relative_url_join():151 url = httpx.URL("/path/to/somewhere")152 assert url.join("/somewhere-else") == "/somewhere-else"153 assert url.join("somewhere-else") == "/path/to/somewhere-else"
55 unmodified lines
209 assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"210 assert url.join("g#s/../x") == "http://example.com/b/c/g#s/../x"211212222def 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 )213def test_resolution_error_1833():214 """215 See https://github.com/encode/httpx/issues/1833216 """217 url = httpx.URL("https://example.com/?[]")218 assert url.join("/") == "https://example.com/"219228 url_set = set(urls)220230 assert all(url in urls for url in url_set)221# Tests for `URL.copy_with()`.222223224def test_url_copywith_authority_subcomponents():225 copy_with_kwargs = {
85 unmodified lines
311 with pytest.raises(httpx.InvalidURL):312 url.copy_with(scheme=bad)313314324def test_url_invalid():325 with pytest.raises(httpx.InvalidURL):326 httpx.URL("https://😇/")315# Tests for copy-modifying-parameters methods.316#317# `URL.copy_set_param()`318# `URL.copy_add_param()`319# `URL.copy_remove_param()`320# `URL.copy_merge_params()`321322329def test_url_invalid_type():330 class ExternalURLClass: # representing external URL class331 pass323def test_url_set_param_manipulation():324 """325 Some basic URL query parameter manipulation.326 """327 url = httpx.URL("https://example.org:123/?a=123")328 assert url.copy_set_param("a", "456") == "https://example.org:123/?a=456"329333 with pytest.raises(TypeError):334 httpx.URL(ExternalURLClass()) # type: ignore330336337def test_url_with_empty_query():331def test_url_add_param_manipulation():332 """339 URLs with and without a trailing `?` but an empty query component340 should preserve the information on the raw path.333 Some basic URL query parameter manipulation.334 """342 url = httpx.URL("https://www.example.com/path")343 assert url.path == "/path"344 assert url.query == b""345 assert url.raw_path == b"/path"335 url = httpx.URL("https://example.org:123/?a=123")336 assert url.copy_add_param("a", "456") == "https://example.org:123/?a=123&a=456"337347 url = httpx.URL("https://www.example.com/path?")348 assert url.path == "/path"349 assert url.query == b""350 assert url.raw_path == b"/path?"338352353def test_url_query_encoding():339def test_url_remove_param_manipulation():340 """355 URL query parameters should use '%20' to encoding spaces,356 and should treat '/' as a safe character. This behaviour differs357 across clients, but we're matching browser behaviour here.341 Some basic URL query parameter manipulation.342 """343 url = httpx.URL("https://example.org:123/?a=123")344 assert url.copy_remove_param("a") == "https://example.org:123/"345359 See https://github.com/encode/httpx/issues/2536360 and https://github.com/encode/httpx/discussions/2460346347def test_url_merge_params_manipulation():348 """362 url = httpx.URL("https://www.example.com/?a=b c&d=e/f")363 assert url.raw_path == b"/?a=b%20c&d=e%2Ff"349 Some basic URL query parameter manipulation.350 """351 url = httpx.URL("https://example.org:123/?a=123")352 assert url.copy_merge_params({"b": "456"}) == "https://example.org:123/?a=123&b=456"353365 url = httpx.URL("https://www.example.com/", params={"a": "b c", "d": "e/f"})366 assert url.raw_path == b"/?a=b%20c&d=e%2Ff"354355# Tests for IDNA hostname support.356369def test_url_with_url_encoded_path():370 url = httpx.URL("https://www.example.com/path%20to%20somewhere")371 assert url.path == "/path to somewhere"372 assert url.query == b""373 assert url.raw_path == b"/path%20to%20somewhere"357358@pytest.mark.parametrize(359 "given,idna,host,raw_host,scheme,port",360 [361 (362 "http://中国.icom.museum:80/",363 "http://xn--fiqs8s.icom.museum:80/",364 "中国.icom.museum",365 b"xn--fiqs8s.icom.museum",366 "http",367 None,368 ),369 (370 "http://Königsgäßchen.de",371 "http://xn--knigsgchen-b4a3dun.de",372 "königsgäßchen.de",373 b"xn--knigsgchen-b4a3dun.de",374 "http",375 None,376 ),377 (378 "https://faß.de",379 "https://xn--fa-hia.de",380 "faß.de",381 b"xn--fa-hia.de",382 "https",383 None,384 ),385 (386 "https://βόλος.com:443",387 "https://xn--nxasmm1c.com:443",388 "βόλος.com",389 b"xn--nxasmm1c.com",390 "https",391 None,392 ),393 (394 "http://ශ්‍රී.com:444",395 "http://xn--10cl1a0b660p.com:444",396 "ශ්‍රී.com",397 b"xn--10cl1a0b660p.com",398 "http",399 444,400 ),401 (402 "https://نامه‌ای.com:4433",403 "https://xn--mgba3gch31f060k.com:4433",404 "نامه‌ای.com",405 b"xn--mgba3gch31f060k.com",406 "https",407 4433,408 ),409 ],410 ids=[411 "http_with_port",412 "unicode_tr46_compat",413 "https_without_port",414 "https_with_port",415 "http_with_custom_port",416 "https_with_custom_port",417 ],418)419def test_idna_url(given, idna, host, raw_host, scheme, port):420 url = httpx.URL(given)421 assert url == httpx.URL(idna)422 assert url.host == host423 assert url.raw_host == raw_host424 assert url.scheme == scheme425 assert url.port == port426427428# Tests for IPv6 hostname support.429430431def test_ipv6_url():432 url = httpx.URL("http://[::ffff:192.168.0.1]:5678/")433434 assert url.host == "::ffff:192.168.0.1"435 assert url.netloc == b"[::ffff:192.168.0.1]:5678"436437438@pytest.mark.parametrize("host", ["[::ffff:192.168.0.1]", "::ffff:192.168.0.1"])439def test_ipv6_url_from_raw_url(host):440 url = httpx.URL(scheme="https", host=host, port=443, path="/")441442 assert url.host == "::ffff:192.168.0.1"443 assert url.netloc == b"[::ffff:192.168.0.1]"444 assert str(url) == "https://[::ffff:192.168.0.1]/"445446447@pytest.mark.parametrize(448 "url_str",449 [450 "http://127.0.0.1:1234",
7 unmodified lines
458459 assert url.host == "::ffff:192.168.0.1"460 assert url.netloc == b"[::ffff:192.168.0.1]:1234"461 assert str(url) == "http://[::ffff:192.168.0.1]:1234"398399400@pytest.mark.parametrize("host", ["[::ffff:192.168.0.1]", "::ffff:192.168.0.1"])401def test_ipv6_url_from_raw_url(host):402 url = httpx.URL(scheme="https", host=host, port=443, path="/")403404 assert url.host == "::ffff:192.168.0.1"405 assert url.netloc == b"[::ffff:192.168.0.1]"406 assert str(url) == "https://[::ffff:192.168.0.1]/"407408409def test_resolution_error_1833():410 """411 See https://github.com/encode/httpx/issues/1833412 """413 url = httpx.URL("https://example.com/?[]")414 assert url.join("/") == "https://example.com/"415416417def test_url_raw_compatibility():418 url = httpx.URL("https://www.example.com/path")419 scheme, host, port, raw_path = url.raw420421 assert scheme == b"https"422 assert host == b"www.example.com"423 assert port is None424 assert raw_path == b"/path"