try:
ipaddress.IPv4Address(host)
except ipaddress.AddressValueError:
- raise InvalidURL("Invalid IPv4 address")
+ raise InvalidURL(f"Invalid IPv4 address: {host!r}")
return host
elif IPv6_STYLE_HOSTNAME.match(host):
try:
ipaddress.IPv6Address(host[1:-1])
except ipaddress.AddressValueError:
- raise InvalidURL("Invalid IPv6 address")
+ raise InvalidURL(f"Invalid IPv6 address: {host!r}")
return host[1:-1]
elif host.isascii():
try:
return idna.encode(host.lower()).decode("ascii")
except idna.IDNAError:
- raise InvalidURL("Invalid IDNA hostname")
+ raise InvalidURL(f"Invalid IDNA hostname: {host!r}")
def normalize_port(
try:
port_as_int = int(port)
except ValueError:
- raise InvalidURL("Invalid port")
+ raise InvalidURL(f"Invalid port: {port!r}")
# See https://url.spec.whatwg.org/#url-miscellaneous
default_port = {"ftp": 21, "http": 80, "https": 443, "ws": 80, "wss": 443}.get(
def test_urlparse_invalid_ipv4():
with pytest.raises(httpx.InvalidURL) as exc:
httpx.URL("https://999.999.999.999/")
- assert str(exc.value) == "Invalid IPv4 address"
+ assert str(exc.value) == "Invalid IPv4 address: '999.999.999.999'"
def test_urlparse_valid_ipv6():
def test_urlparse_invalid_ipv6():
with pytest.raises(httpx.InvalidURL) as exc:
httpx.URL("https://[2001]/")
- assert str(exc.value) == "Invalid IPv6 address"
+ assert str(exc.value) == "Invalid IPv6 address: '[2001]'"
def test_urlparse_unescaped_idna_host():
def test_urlparse_invalid_idna_host():
with pytest.raises(httpx.InvalidURL) as exc:
httpx.URL("https://☃.com/")
- assert str(exc.value) == "Invalid IDNA hostname"
+ assert str(exc.value) == "Invalid IDNA hostname: '☃.com'"
# Tests for different port types
def test_urlparse_invalid_port():
with pytest.raises(httpx.InvalidURL) as exc:
httpx.URL("https://example.com:abc/")
- assert str(exc.value) == "Invalid port"
+ assert str(exc.value) == "Invalid port: 'abc'"
# Tests for path handling