Additional context in InvalidURL exceptions (#2675)
This commit is contained in:
parent
cca62060cb
commit
26dc39213a
@ -287,7 +287,7 @@ def encode_host(host: str) -> str:
|
||||
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):
|
||||
@ -302,7 +302,7 @@ def encode_host(host: str) -> str:
|
||||
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():
|
||||
@ -317,7 +317,7 @@ def encode_host(host: str) -> str:
|
||||
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(
|
||||
@ -338,7 +338,7 @@ 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(
|
||||
|
||||
@ -53,7 +53,7 @@ def test_urlparse_valid_ipv4():
|
||||
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():
|
||||
@ -64,7 +64,7 @@ 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():
|
||||
@ -80,7 +80,7 @@ def test_urlparse_escaped_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
|
||||
@ -100,7 +100,7 @@ def test_urlparse_normalized_port():
|
||||
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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user