Skip to content
Myr

test_url.py

This file

Path

tests/models/test_url.py

842 lines · 24.7 KiB · Python

Last changed by

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

History of this file

Take 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    """145    URL query parameters should use '%20' for encoding spaces,146    and should treat '/' as a safe character. This behaviour differs147    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")153    assert url.raw_path == b"/?a=b%20c&d=e/f"154155    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"157158159def 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"})163164    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"})169170171# Tests for username and password172173174@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 == password217218219# Tests for different host types220221222def 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"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 optional percent encoding282283284def test_param_requires_encoding():285    url = httpx.URL("http://webservice", params={"u": "with spaces"})286    assert str(url) == "http://webservice?u=with%20spaces"287288289def test_param_does_not_require_encoding():290    url = httpx.URL("http://webservice", params={"u": "with%20spaces"})291    assert str(url) == "http://webservice?u=with%20spaces"292293294def test_param_with_existing_escape_requires_encoding():295    url = httpx.URL("http://webservice", params={"u": "http://example.com?q=foo%2Fa"})296    assert str(url) == "http://webservice?u=http%3A%2F%2Fexample.com%3Fq%3Dfoo%252Fa"297298299# Tests for invalid URLs300301302def test_url_invalid_hostname():303    """304    Ensure that invalid URLs raise an `httpx.InvalidURL` exception.305    """306    with pytest.raises(httpx.InvalidURL):307        httpx.URL("https://😇/")308309310def test_url_excessively_long_url():311    with pytest.raises(httpx.InvalidURL) as exc:312        httpx.URL("https://www.example.com/" + "x" * 100_000)313    assert str(exc.value) == "URL too long"314315316def test_url_excessively_long_component():317    with pytest.raises(httpx.InvalidURL) as exc:318        httpx.URL("https://www.example.com", path="/" + "x" * 100_000)319    assert str(exc.value) == "URL component 'path' too long"320321322def test_url_non_printing_character_in_url():323    with pytest.raises(httpx.InvalidURL) as exc:324        httpx.URL("https://www.example.com/\n")325    assert str(exc.value) == "Invalid non-printable ASCII character in URL"326327328def test_url_non_printing_character_in_component():329    with pytest.raises(httpx.InvalidURL) as exc:330        httpx.URL("https://www.example.com", path="/\n")331    assert (332        str(exc.value)333        == "Invalid non-printable ASCII character in URL component 'path'"334    )335336337# Test for url components338339340def test_url_with_components():341    url = httpx.URL(scheme="https", host="www.example.com", path="/")342343    assert url.scheme == "https"344    assert url.userinfo == b""345    assert url.host == "www.example.com"346    assert url.port is None347    assert url.path == "/"348    assert url.query == b""349    assert url.fragment == ""350351    assert str(url) == "https://www.example.com/"352353354def test_urlparse_with_invalid_component():355    with pytest.raises(TypeError) as exc:356        httpx.URL(scheme="https", host="www.example.com", incorrect="/")357    assert str(exc.value) == "'incorrect' is an invalid keyword argument for URL()"358359360def test_urlparse_with_invalid_scheme():361    with pytest.raises(httpx.InvalidURL) as exc:362        httpx.URL(scheme="~", host="www.example.com", path="/")363    assert str(exc.value) == "Invalid URL component 'scheme'"364365366def test_urlparse_with_invalid_path():367    with pytest.raises(httpx.InvalidURL) as exc:368        httpx.URL(scheme="https", host="www.example.com", path="abc")369    assert str(exc.value) == "For absolute URLs, path must be empty or begin with '/'"370371    with pytest.raises(httpx.InvalidURL) as exc:372        httpx.URL(path="//abc")373    assert (374        str(exc.value)375        == "URLs with no authority component cannot have a path starting with '//'"376    )377378    with pytest.raises(httpx.InvalidURL) as exc:379        httpx.URL(path=":abc")380    assert (381        str(exc.value)382        == "URLs with no scheme component cannot have a path starting with ':'"383    )384385386def test_url_with_relative_path():387    # This path would be invalid for an absolute URL, but is valid as a relative URL.388    url = httpx.URL(path="abc")389    assert url.path == "abc"390391392# Tests for `httpx.URL` python built-in operators.393394395def test_url_eq_str():396    """397    Ensure that `httpx.URL` supports the equality operator.398    """399    url = httpx.URL("https://example.org:123/path/to/somewhere?abc=123#anchor")400    assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"401    assert str(url) == url402403404def test_url_set():405    """406    Ensure that `httpx.URL` instances can be used in sets.407    """408    urls = (409        httpx.URL("http://example.org:123/path/to/somewhere"),410        httpx.URL("http://example.org:123/path/to/somewhere/else"),411    )412413    url_set = set(urls)414415    assert all(url in urls for url in url_set)416417418# Tests for TypeErrors when instantiating `httpx.URL`.419420421def test_url_invalid_type():422    """423    Ensure that invalid types on `httpx.URL()` raise a `TypeError`.424    """425426    class ExternalURLClass:  # representing external URL class427        pass428429    with pytest.raises(TypeError):430        httpx.URL(ExternalURLClass())  # type: ignore431432433def test_url_with_invalid_component():434    with pytest.raises(TypeError) as exc:435        httpx.URL(scheme="https", host="www.example.com", incorrect="/")436    assert str(exc.value) == "'incorrect' is an invalid keyword argument for URL()"437438439# Tests for `URL.join()`.440441442def test_url_join():443    """444    Some basic URL joining tests.445    """446    url = httpx.URL("https://example.org:123/path/to/somewhere")447    assert url.join("/somewhere-else") == "https://example.org:123/somewhere-else"448    assert (449        url.join("somewhere-else") == "https://example.org:123/path/to/somewhere-else"450    )451    assert (452        url.join("../somewhere-else") == "https://example.org:123/path/somewhere-else"453    )454    assert url.join("../../somewhere-else") == "https://example.org:123/somewhere-else"455456457def test_relative_url_join():458    url = httpx.URL("/path/to/somewhere")459    assert url.join("/somewhere-else") == "/somewhere-else"460    assert url.join("somewhere-else") == "/path/to/somewhere-else"461    assert url.join("../somewhere-else") == "/path/somewhere-else"462    assert url.join("../../somewhere-else") == "/somewhere-else"463464465def test_url_join_rfc3986():466    """467    URL joining tests, as-per reference examples in RFC 3986.468469    https://tools.ietf.org/html/rfc3986#section-5.4470    """471472    url = httpx.URL("http://example.com/b/c/d;p?q")473474    assert url.join("g") == "http://example.com/b/c/g"475    assert url.join("./g") == "http://example.com/b/c/g"476    assert url.join("g/") == "http://example.com/b/c/g/"477    assert url.join("/g") == "http://example.com/g"478    assert url.join("//g") == "http://g"479    assert url.join("?y") == "http://example.com/b/c/d;p?y"480    assert url.join("g?y") == "http://example.com/b/c/g?y"481    assert url.join("#s") == "http://example.com/b/c/d;p?q#s"482    assert url.join("g#s") == "http://example.com/b/c/g#s"483    assert url.join("g?y#s") == "http://example.com/b/c/g?y#s"484    assert url.join(";x") == "http://example.com/b/c/;x"485    assert url.join("g;x") == "http://example.com/b/c/g;x"486    assert url.join("g;x?y#s") == "http://example.com/b/c/g;x?y#s"487    assert url.join("") == "http://example.com/b/c/d;p?q"488    assert url.join(".") == "http://example.com/b/c/"489    assert url.join("./") == "http://example.com/b/c/"490    assert url.join("..") == "http://example.com/b/"491    assert url.join("../") == "http://example.com/b/"492    assert url.join("../g") == "http://example.com/b/g"493    assert url.join("../..") == "http://example.com/"494    assert url.join("../../") == "http://example.com/"495    assert url.join("../../g") == "http://example.com/g"496497    assert url.join("../../../g") == "http://example.com/g"498    assert url.join("../../../../g") == "http://example.com/g"499500    assert url.join("/./g") == "http://example.com/g"501    assert url.join("/../g") == "http://example.com/g"502    assert url.join("g.") == "http://example.com/b/c/g."503    assert url.join(".g") == "http://example.com/b/c/.g"504    assert url.join("g..") == "http://example.com/b/c/g.."505    assert url.join("..g") == "http://example.com/b/c/..g"506507    assert url.join("./../g") == "http://example.com/b/g"508    assert url.join("./g/.") == "http://example.com/b/c/g/"509    assert url.join("g/./h") == "http://example.com/b/c/g/h"510    assert url.join("g/../h") == "http://example.com/b/c/h"511    assert url.join("g;x=1/./y") == "http://example.com/b/c/g;x=1/y"512    assert url.join("g;x=1/../y") == "http://example.com/b/c/y"513514    assert url.join("g?y/./x") == "http://example.com/b/c/g?y/./x"515    assert url.join("g?y/../x") == "http://example.com/b/c/g?y/../x"516    assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"517    assert url.join("g#s/../x") == "http://example.com/b/c/g#s/../x"518519520def test_resolution_error_1833():521    """522    See https://github.com/encode/httpx/issues/1833523    """524    url = httpx.URL("https://example.com/?[]")525    assert url.join("/") == "https://example.com/"526527528# Tests for `URL.copy_with()`.529530531def test_copy_with():532    url = httpx.URL("https://www.example.com/")533    assert str(url) == "https://www.example.com/"534535    url = url.copy_with()536    assert str(url) == "https://www.example.com/"537538    url = url.copy_with(scheme="http")539    assert str(url) == "http://www.example.com/"540541    url = url.copy_with(netloc=b"example.com")542    assert str(url) == "http://example.com/"543544    url = url.copy_with(path="/abc")545    assert str(url) == "http://example.com/abc"546547548def test_url_copywith_authority_subcomponents():549    copy_with_kwargs = {550        "username": "username",551        "password": "password",552        "port": 444,553        "host": "example.net",554    }555    url = httpx.URL("https://example.org")556    new = url.copy_with(**copy_with_kwargs)557    assert str(new) == "https://username:password@example.net:444"558559560def test_url_copywith_netloc():561    copy_with_kwargs = {562        "netloc": b"example.net:444",563    }564    url = httpx.URL("https://example.org")565    new = url.copy_with(**copy_with_kwargs)566    assert str(new) == "https://example.net:444"567568569def test_url_copywith_userinfo_subcomponents():570    copy_with_kwargs = {571        "username": "tom@example.org",572        "password": "abc123@ %",573    }574    url = httpx.URL("https://example.org")575    new = url.copy_with(**copy_with_kwargs)576    assert str(new) == "https://tom%40example.org:abc123%40%20%25@example.org"577    assert new.username == "tom@example.org"578    assert new.password == "abc123@ %"579    assert new.userinfo == b"tom%40example.org:abc123%40%20%25"580581582def test_url_copywith_invalid_component():583    url = httpx.URL("https://example.org")584    with pytest.raises(TypeError):585        url.copy_with(pathh="/incorrect-spelling")586    with pytest.raises(TypeError):587        url.copy_with(userinfo="should be bytes")588589590def test_url_copywith_urlencoded_path():591    url = httpx.URL("https://example.org")592    url = url.copy_with(path="/path to somewhere")593    assert url.path == "/path to somewhere"594    assert url.query == b""595    assert url.raw_path == b"/path%20to%20somewhere"596597598def test_url_copywith_query():599    url = httpx.URL("https://example.org")600    url = url.copy_with(query=b"a=123")601    assert url.path == "/"602    assert url.query == b"a=123"603    assert url.raw_path == b"/?a=123"604605606def test_url_copywith_raw_path():607    url = httpx.URL("https://example.org")608    url = url.copy_with(raw_path=b"/some/path")609    assert url.path == "/some/path"610    assert url.query == b""611    assert url.raw_path == b"/some/path"612613    url = httpx.URL("https://example.org")614    url = url.copy_with(raw_path=b"/some/path?")615    assert url.path == "/some/path"616    assert url.query == b""617    assert url.raw_path == b"/some/path?"618619    url = httpx.URL("https://example.org")620    url = url.copy_with(raw_path=b"/some/path?a=123")621    assert url.path == "/some/path"622    assert url.query == b"a=123"623    assert url.raw_path == b"/some/path?a=123"624625626def test_url_copywith_security():627    """628    Prevent unexpected changes on URL after calling copy_with (CVE-2021-41945)629    """630    with pytest.raises(httpx.InvalidURL):631        httpx.URL("https://u:p@[invalid!]//evilHost/path?t=w#tw")632633    url = httpx.URL("https://example.com/path?t=w#tw")634    bad = "https://xxxx:xxxx@xxxxxxx/xxxxx/xxx?x=x#xxxxx"635    with pytest.raises(httpx.InvalidURL):636        url.copy_with(scheme=bad)637638639# Tests for copy-modifying-parameters methods.640#641# `URL.copy_set_param()`642# `URL.copy_add_param()`643# `URL.copy_remove_param()`644# `URL.copy_merge_params()`645646647def test_url_set_param_manipulation():648    """649    Some basic URL query parameter manipulation.650    """651    url = httpx.URL("https://example.org:123/?a=123")652    assert url.copy_set_param("a", "456") == "https://example.org:123/?a=456"653654655def test_url_add_param_manipulation():656    """657    Some basic URL query parameter manipulation.658    """659    url = httpx.URL("https://example.org:123/?a=123")660    assert url.copy_add_param("a", "456") == "https://example.org:123/?a=123&a=456"661662663def test_url_remove_param_manipulation():664    """665    Some basic URL query parameter manipulation.666    """667    url = httpx.URL("https://example.org:123/?a=123")668    assert url.copy_remove_param("a") == "https://example.org:123/"669670671def test_url_merge_params_manipulation():672    """673    Some basic URL query parameter manipulation.674    """675    url = httpx.URL("https://example.org:123/?a=123")676    assert url.copy_merge_params({"b": "456"}) == "https://example.org:123/?a=123&b=456"677678679# Tests for IDNA hostname support.680681682@pytest.mark.parametrize(683    "given,idna,host,raw_host,scheme,port",684    [685        (686            "http://中国.icom.museum:80/",687            "http://xn--fiqs8s.icom.museum:80/",688            "中国.icom.museum",689            b"xn--fiqs8s.icom.museum",690            "http",691            None,692        ),693        (694            "http://Königsgäßchen.de",695            "http://xn--knigsgchen-b4a3dun.de",696            "königsgäßchen.de",697            b"xn--knigsgchen-b4a3dun.de",698            "http",699            None,700        ),701        (702            "https://faß.de",703            "https://xn--fa-hia.de",704            "faß.de",705            b"xn--fa-hia.de",706            "https",707            None,708        ),709        (710            "https://βόλος.com:443",711            "https://xn--nxasmm1c.com:443",712            "βόλος.com",713            b"xn--nxasmm1c.com",714            "https",715            None,716        ),717        (718            "http://ශ්‍රී.com:444",719            "http://xn--10cl1a0b660p.com:444",720            "ශ්‍රී.com",721            b"xn--10cl1a0b660p.com",722            "http",723            444,724        ),725        (726            "https://نامه‌ای.com:4433",727            "https://xn--mgba3gch31f060k.com:4433",728            "نامه‌ای.com",729            b"xn--mgba3gch31f060k.com",730            "https",731            4433,732        ),733    ],734    ids=[735        "http_with_port",736        "unicode_tr46_compat",737        "https_without_port",738        "https_with_port",739        "http_with_custom_port",740        "https_with_custom_port",741    ],742)743def test_idna_url(given, idna, host, raw_host, scheme, port):744    url = httpx.URL(given)745    assert url == httpx.URL(idna)746    assert url.host == host747    assert url.raw_host == raw_host748    assert url.scheme == scheme749    assert url.port == port750751752def test_url_unescaped_idna_host():753    url = httpx.URL("https://中国.icom.museum/")754    assert url.raw_host == b"xn--fiqs8s.icom.museum"755756757def test_url_escaped_idna_host():758    url = httpx.URL("https://xn--fiqs8s.icom.museum/")759    assert url.raw_host == b"xn--fiqs8s.icom.museum"760761762def test_url_invalid_idna_host():763    with pytest.raises(httpx.InvalidURL) as exc:764        httpx.URL("https://☃.com/")765    assert str(exc.value) == "Invalid IDNA hostname: '☃.com'"766767768# Tests for IPv4 hostname support.769770771def test_url_valid_ipv4():772    url = httpx.URL("https://1.2.3.4/")773    assert url.host == "1.2.3.4"774775776def test_url_invalid_ipv4():777    with pytest.raises(httpx.InvalidURL) as exc:778        httpx.URL("https://999.999.999.999/")779    assert str(exc.value) == "Invalid IPv4 address: '999.999.999.999'"780781782# Tests for IPv6 hostname support.783784785def test_ipv6_url():786    url = httpx.URL("http://[::ffff:192.168.0.1]:5678/")787788    assert url.host == "::ffff:192.168.0.1"789    assert url.netloc == b"[::ffff:192.168.0.1]:5678"790791792def test_url_valid_ipv6():793    url = httpx.URL("https://[2001:db8::ff00:42:8329]/")794    assert url.host == "2001:db8::ff00:42:8329"795796797def test_url_invalid_ipv6():798    with pytest.raises(httpx.InvalidURL) as exc:799        httpx.URL("https://[2001]/")800    assert str(exc.value) == "Invalid IPv6 address: '[2001]'"801802803@pytest.mark.parametrize("host", ["[::ffff:192.168.0.1]", "::ffff:192.168.0.1"])804def test_ipv6_url_from_raw_url(host):805    url = httpx.URL(scheme="https", host=host, port=443, path="/")806807    assert url.host == "::ffff:192.168.0.1"808    assert url.netloc == b"[::ffff:192.168.0.1]"809    assert str(url) == "https://[::ffff:192.168.0.1]/"810811812@pytest.mark.parametrize(813    "url_str",814    [815        "http://127.0.0.1:1234",816        "http://example.com:1234",817        "http://[::ffff:127.0.0.1]:1234",818    ],819)820@pytest.mark.parametrize("new_host", ["[::ffff:192.168.0.1]", "::ffff:192.168.0.1"])821def test_ipv6_url_copy_with_host(url_str, new_host):822    url = httpx.URL(url_str).copy_with(host=new_host)823824    assert url.host == "::ffff:192.168.0.1"825    assert url.netloc == b"[::ffff:192.168.0.1]:1234"826    assert str(url) == "http://[::ffff:192.168.0.1]:1234"827828829# Test for deprecated API830831832def test_url_raw_compatibility():833    """834    Test case for the (to-be-deprecated) `url.raw` accessor.835    """836    url = httpx.URL("https://www.example.com/path")837    scheme, host, port, raw_path = url.raw838839    assert scheme == b"https"840    assert host == b"www.example.com"841    assert port is None842    assert raw_path == b"/path"