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 fileTake it
1import pytest23import httpx45# Tests for `httpx.URL` instantiation and property accessors.678def 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():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"3334 assert str(url) == "https://example.org:123/path/to/somewhere?abc=123#anchor"35 assert (36 repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"37 )383940def 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?"545556def 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...717273@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 == fragment141142143def test_url_query_encoding():144 url = httpx.URL("https://www.example.com/?a=b c&d=e/f")145 assert url.raw_path == b"/?a=b%20c&d=e/f"146147 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"149150 url = httpx.URL("https://www.example.com/", params={"a": "b c", "d": "e/f"})151 assert url.raw_path == b"/?a=b+c&d=e%2Ff"152153154def 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"})158159 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"})164165166# Tests for username and password167168169@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 == password212213214# Tests for different host types215216217def 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"225226227def test_url_percent_escape_host():228 url = httpx.URL("https://exam le.com/")229 assert url.host == "exam%20le.com"230231232def 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"279280281# Tests for query parameter percent encoding.282#283# Percent-encoding in `params={}` should match browser form behavior.284285286def test_param_with_space():287 # Params passed as form key-value pairs should be form escaped,288 # Including the special case of "+" for space seperators.289 url = httpx.URL("http://webservice", params={"u": "with spaces"})290 assert str(url) == "http://webservice?u=with+spaces"291292293def test_param_requires_encoding():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.303 url = httpx.URL("http://webservice", params={"u": "with%20spaces"})304 assert str(url) == "http://webservice?u=with%2520spaces"305306307def test_param_with_existing_escape_requires_encoding():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.311 url = httpx.URL("http://webservice", params={"u": "http://example.com?q=foo%2Fa"})312 assert str(url) == "http://webservice?u=http%3A%2F%2Fexample.com%3Fq%3Dfoo%252Fa"313314315# 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"338339340# Tests for invalid URLs341342343def test_url_invalid_hostname():344 """345 Ensure that invalid URLs raise an `httpx.InvalidURL` exception.346 """347 with pytest.raises(httpx.InvalidURL):348 httpx.URL("https://😇/")349350351def 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")366 assert str(exc.value) == (367 "Invalid non-printable ASCII character in URL, '\\n' at position 24."368 )369370371def test_url_non_printing_character_in_component():372 with pytest.raises(httpx.InvalidURL) as exc:373 httpx.URL("https://www.example.com", path="/\n")374 assert str(exc.value) == (375 "Invalid non-printable ASCII character in URL path component, "376 "'\\n' at position 1."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")416 assert str(exc.value) == "Relative URLs cannot have a path starting with '//'"417418 with pytest.raises(httpx.InvalidURL) as exc:419 httpx.URL(path=":abc")420 assert str(exc.value) == "Relative URLs cannot have a path starting with ':'"421422423def 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.430431432def test_url_eq_str():433 """434 Ensure that `httpx.URL` supports the equality operator.435 """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) == url439440441def 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)453454455# Tests for TypeErrors when instantiating `httpx.URL`.456457458def 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: ignore468469470def 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()"474475476# Tests for `URL.join()`.477478479def 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"555556557def 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/"563564565# Tests for `URL.copy_with()`.566567568def 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"583584585def 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)613 assert str(new) == "https://tom%40example.org:abc123%40%20%@example.org"614 assert new.username == "tom@example.org"615 assert new.password == "abc123@ %"616 assert new.userinfo == b"tom%40example.org:abc123%40%20%"617618619def 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)674675676# 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()`682683684def 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"690691692def test_url_add_param_manipulation():693 """694 Some basic URL query parameter manipulation.695 """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"698699700def test_url_remove_param_manipulation():701 """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/"706707708def test_url_merge_params_manipulation():709 """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"714715716# Tests for IDNA hostname support.717718719@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 == port787788789def 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'"817818819# Tests for IPv6 hostname support.820821822def 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"827828829def 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]'"838839840@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]/"847848849@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"