From fdd12b9d61a77bdbdb8d1632dfd37bb047dbdc02 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 26 Sep 2024 16:16:09 -0500 Subject: [PATCH] cleanup --- tests/test_url_parsing.py | 25 +++++++++++++++++++++++++ yarl/_url.py | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/test_url_parsing.py b/tests/test_url_parsing.py index 4fe95185..92065b5f 100644 --- a/tests/test_url_parsing.py +++ b/tests/test_url_parsing.py @@ -604,3 +604,28 @@ def test_schemes_that_require_host(scheme: str) -> None: ) with pytest.raises(ValueError, match=expect): URL(f"{scheme}://:1") + + +@pytest.mark.parametrize( + ("url", "hostname", "hostname_without_brackets"), + [ + ("http://[::1]", "[::1]", "::1"), + ("http://[::1]:8080", "[::1]", "::1"), + ("http://127.0.0.1:8080", "127.0.0.1", "127.0.0.1"), + ( + "http://xn--jxagkqfkduily1i.eu", + "xn--jxagkqfkduily1i.eu", + "xn--jxagkqfkduily1i.eu", + ), + ], +) +def test_ipv6_url_round_trips( + url: str, hostname: str, hostname_without_brackets: str +) -> None: + """Verify that IPv6 URLs round-trip correctly.""" + parsed = URL(url) + assert parsed._val.hostname == hostname_without_brackets + assert parsed.raw_host == hostname_without_brackets + assert parsed.host_subcomponent == hostname + assert str(parsed) == url + assert str(URL(str(parsed))) == url diff --git a/yarl/_url.py b/yarl/_url.py index a61a7f1d..37e4739f 100644 --- a/yarl/_url.py +++ b/yarl/_url.py @@ -395,7 +395,7 @@ def __str__(self) -> str: netloc=self._make_netloc( self.raw_user, self.raw_password, - self.raw_host, + self.host_subcomponent, port, encode_host=False, )