diff --git a/src/rfc3986/iri.py b/src/rfc3986/iri.py index 205221e..eeec4a0 100644 --- a/src/rfc3986/iri.py +++ b/src/rfc3986/iri.py @@ -126,6 +126,7 @@ def encode( # noqa: C901 Function that encodes each part of the host component If not given will raise an exception if the IRI contains a host component. + :raises InvalidAuthority: If a nonempty authority cannot be parsed. :rtype: uri.URIReference :returns: A URI reference """ @@ -150,6 +151,9 @@ def idna_encoder(name: str) -> t.Union[str, bytes]: raise exceptions.InvalidAuthority(self.authority) return name + # The convenience properties suppress InvalidAuthority, which + # would silently discard an unparseable authority here. + self.authority_info() authority = "" if self.host: authority = ".".join( diff --git a/tests/test_iri.py b/tests/test_iri.py index 385ca67..a1d3d4f 100644 --- a/tests/test_iri.py +++ b/tests/test_iri.py @@ -72,3 +72,67 @@ def test_encode_invalid_iri(iri): iri_ref = rfc3986.iri_reference(iri) with pytest.raises(InvalidAuthority): iri_ref.encode() + + +@pytest.mark.parametrize( + "authority", + [ + "example.com:bad", + "example.com:80:90", + "user:password@exa mple.com", + "[::g]", + "[::1", + "[::1]suffix", + "256.1.2.3", + "bad\x80.example", + ], +) +@pytest.mark.parametrize( + "custom_encoder", [pytest.param(False, marks=requires_idna), True] +) +def test_encode_rejects_unparseable_authority(authority, custom_encoder): + reference = rfc3986.iri_reference( + "https://" + authority + "/path?query#fragment" + ) + calls = [] + + def encoder(label): + calls.append(label) + return label + + with pytest.raises(InvalidAuthority): + if custom_encoder: + reference.encode(idna_encoder=encoder) + else: + reference.encode() + assert reference.authority == authority + assert calls == [] + + +@pytest.mark.parametrize( + "value", + [ + "https://user:password@example.com:080/path?query#fragment", + "https://[::1]:443/path", + "https://[fe80::1%25eth0]/path", + "https://user@:80/path", + "mailto:user@example.com", + "/relative/path?query#fragment", + ], +) +@pytest.mark.parametrize( + "custom_encoder", [pytest.param(False, marks=requires_idna), True] +) +def test_encode_preserves_parseable_authority(value, custom_encoder): + reference = rfc3986.iri_reference(value) + if custom_encoder: + encoded = reference.encode(idna_encoder=lambda label: label) + else: + encoded = reference.encode() + assert encoded.unsplit() == value + + +@requires_idna +def test_encode_empty_port_retains_existing_normalization(): + reference = rfc3986.iri_reference("https://example.com:/path") + assert reference.encode().unsplit() == "https://example.com/path"