Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/hyperlink/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1667,11 +1667,16 @@ def to_uri(self):
new_path = _encode_path_parts(
self.path, has_scheme=bool(self.scheme), rooted=False, maximal=True
)
new_host = (
self.host
if not self.host
else idna_encode(self.host, uts46=True).decode("ascii")
)
# IP literals must not be IDNA-encoded (colons fail idna).
if not self.host:
new_host = self.host
else:
family, _ = parse_host(self.host)
new_host = (
self.host
if family is not None
else idna_encode(self.host, uts46=True).decode("ascii")
)
return self.replace(
userinfo=new_userinfo,
host=new_host,
Expand Down
9 changes: 9 additions & 0 deletions src/hyperlink/test/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,15 @@ def test_idna(self):
self.assertEqual(u2.to_text(), "https://xn--bcher-kva.ch")
self.assertEqual(u2.to_iri().to_text(), "https://bücher.ch")

def test_to_uri_ip_literal(self):
# type: () -> None
ipv6 = URL.from_text("http://[::1]/")
self.assertEqual(ipv6.to_uri().to_text(), "http://[::1]/")
self.assertEqual(ipv6.to_uri().host, "::1")
ipv4 = URL.from_text("http://127.0.0.1/")
self.assertEqual(ipv4.to_uri().to_text(), "http://127.0.0.1/")
self.assertEqual(ipv4.to_uri().host, "127.0.0.1")

def test_netloc_slashes(self):
# type: () -> None

Expand Down