Skip to content

Commit

Permalink
Fix round-trip of IPv6 addresses when converting to a string (#1158)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
bdraco and pre-commit-ci[bot] authored Sep 26, 2024
1 parent 7d013d2 commit 7128872
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES/1157.bugfix.rst
1 change: 1 addition & 0 deletions CHANGES/1158.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed IPv6 addresses missing brackets when the :class:`~yarl.URL` was converted to a string -- by :user:`bdraco`.
15 changes: 10 additions & 5 deletions tests/test_url_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@ def test_build_simple():

def test_url_build_ipv6():
u = URL.build(scheme="http", host="::1")
assert str(u) == "http://::1"
assert str(u) == "http://[::1]"


def test_url_build_ipv6_brackets():
u = URL.build(scheme="http", host="[::1]")
assert str(u) == "http://::1"
def test_url_build_ipv6_brackets_encoded():
u = URL.build(scheme="http", host="[::1]", encoded=True)
assert str(u) == "http://[::1]"


def test_url_build_ipv6_brackets_not_encoded():
u = URL.build(scheme="http", host="::1", encoded=False)
assert str(u) == "http://[::1]"


def test_url_ipv4_in_ipv6():
u = URL.build(scheme="http", host="2001:db8:122:344::192.0.2.33")
assert str(u) == "http://2001:db8:122:344::c000:221"
assert str(u) == "http://[2001:db8:122:344::c000:221]"


def test_build_with_scheme():
Expand Down
25 changes: 25 additions & 0 deletions tests/test_url_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_url_round_trips(
url: str, hostname: str, hostname_without_brackets: str
) -> None:
"""Verify that 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
2 changes: 1 addition & 1 deletion yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down

0 comments on commit 7128872

Please sign in to comment.