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
Provenance
- Commit
724eced- Tree
0d32492- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
9ef08c7
1 file changed+209 added−172 removed
modified:tests/models/test_url.py+209 added-172 removed
1 unmodified line2 3 import httpx4 5 6-@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-)67-def 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 == port74-75-6 def 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 lines20 assert new == httpx.URL("http://example.org/path/to/somewhere?abc=123#anchor")21 assert new.scheme == "http"22 23 24+def 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"33+34+ 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?"38+39+40+def 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.45+46+ 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"51+52+ 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"54+55+56+def 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"61+62+63+def test_url_raw_compatibility():64+ url = httpx.URL("https://www.example.com/path")65+ scheme, host, port, raw_path = url.raw66+67+ assert scheme == b"https"68+ assert host == b"www.example.com"69+ assert port is None70+ assert raw_path == b"/path"71+72+73+def 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://😇/")79+80+81 def 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) == url89 90 91+def 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+ )99+100+ url_set = set(urls)101+102+ assert all(url in urls for url in url_set)103+104+105+def test_url_invalid_type():106+ """107+ Ensure that invalid types on `httpx.URL()` raise a `TypeError`.108+ """109+110+ class ExternalURLClass: # representing external URL class111+ pass112+113+ with pytest.raises(TypeError):114+ httpx.URL(ExternalURLClass()) # type: ignore115+116+117+# Tests for `QueryParams`.118+119+120 def 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 lines128 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"129 assert url.params == httpx.QueryParams({"a": "123"})130 131 132+# Tests for `URL.join()`.133+134+135 def test_url_join():136 """137 Some basic URL joining tests.138 """7 unmodified lines146 )147 assert url.join("../../somewhere-else") == "https://example.org:123/somewhere-else"148 149 127-def 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"133-134-135-def 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"141-142-143-def 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/"149-150-151-def 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"157-158-150 def 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 lines209 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"211 212 222-def 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- )213+def 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/"219 228- url_set = set(urls)220 230- assert all(url in urls for url in url_set)221+# Tests for `URL.copy_with()`.222 223 224 def test_url_copywith_authority_subcomponents():225 copy_with_kwargs = {85 unmodified lines311 with pytest.raises(httpx.InvalidURL):312 url.copy_with(scheme=bad)313 314 324-def 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()`321 322 329-def test_url_invalid_type():330- class ExternalURLClass: # representing external URL class331- pass323+def 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"329 333- with pytest.raises(TypeError):334- httpx.URL(ExternalURLClass()) # type: ignore330 336-337-def test_url_with_empty_query():331+def 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"337 347- 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?"338 352-353-def test_url_query_encoding():339+def 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/"345 359- See https://github.com/encode/httpx/issues/2536360- and https://github.com/encode/httpx/discussions/2460346+347+def 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"353 365- 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"354 355+# Tests for IDNA hostname support.356 369-def 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"357 358+@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+)419+def 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 == port426 427+428+# Tests for IPv6 hostname support.429+430+431 def test_ipv6_url():432 url = httpx.URL("http://[::ffff:192.168.0.1]:5678/")433 434 assert url.host == "::ffff:192.168.0.1"435 assert url.netloc == b"[::ffff:192.168.0.1]:5678"436 437 438+@pytest.mark.parametrize("host", ["[::ffff:192.168.0.1]", "::ffff:192.168.0.1"])439+def test_ipv6_url_from_raw_url(host):440+ url = httpx.URL(scheme="https", host=host, port=443, path="/")441+442+ 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]/"445+446+447 @pytest.mark.parametrize(448 "url_str",449 [450 "http://127.0.0.1:1234",7 unmodified lines458 459 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"398-399-400-@pytest.mark.parametrize("host", ["[::ffff:192.168.0.1]", "::ffff:192.168.0.1"])401-def test_ipv6_url_from_raw_url(host):402- url = httpx.URL(scheme="https", host=host, port=443, path="/")403-404- 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]/"407-408-409-def 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/"415-416-417-def test_url_raw_compatibility():418- url = httpx.URL("https://www.example.com/path")419- scheme, host, port, raw_path = url.raw420-421- assert scheme == b"https"422- assert host == b"www.example.com"423- assert port is None424- assert raw_path == b"/path"