Skip to content
Myr

Additional context in InvalidURL exceptions (#2675)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by GitHub · parent cca6206 · Browse files · 2 files+8 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+8 added−8 removed

  • tests:+4 added−4 removed
  • other:+4 added−4 removed

2 files+8 added−8 removed

modified:httpx/_urlparse.py+4 added-4 removed
285 unmodified lines
286 # IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet287 try:288 ipaddress.IPv4Address(host)289 except ipaddress.AddressValueError:290 raise InvalidURL("Invalid IPv4 address")290 raise InvalidURL(f"Invalid IPv4 address: {host!r}")291 return host292293 elif IPv6_STYLE_HOSTNAME.match(host):294 # Validate IPv6 hostnames like [...]
6 unmodified lines
301 # square bracket characters are allowed in the URI syntax."302 try:303 ipaddress.IPv6Address(host[1:-1])304 except ipaddress.AddressValueError:305 raise InvalidURL("Invalid IPv6 address")305 raise InvalidURL(f"Invalid IPv6 address: {host!r}")306 return host[1:-1]307308 elif host.isascii():309 # Regular ASCII hostnames
6 unmodified lines
316 # IDNA hostnames317 try:318 return idna.encode(host.lower()).decode("ascii")319 except idna.IDNAError:320 raise InvalidURL("Invalid IDNA hostname")320 raise InvalidURL(f"Invalid IDNA hostname: {host!r}")321322323def normalize_port(324 port: typing.Optional[typing.Union[str, int]], scheme: str
12 unmodified lines
337338 try:339 port_as_int = int(port)340 except ValueError:341 raise InvalidURL("Invalid port")341 raise InvalidURL(f"Invalid port: {port!r}")342343 # See https://url.spec.whatwg.org/#url-miscellaneous344 default_port = {"ftp": 21, "http": 80, "https": 443, "ws": 80, "wss": 443}.get(345 scheme
110 unmodified lines
modified:tests/test_urlparse.py+4 added-4 removed
51 unmodified lines
5253def test_urlparse_invalid_ipv4():54 with pytest.raises(httpx.InvalidURL) as exc:55 httpx.URL("https://999.999.999.999/")56 assert str(exc.value) == "Invalid IPv4 address"56 assert str(exc.value) == "Invalid IPv4 address: '999.999.999.999'"575859def test_urlparse_valid_ipv6():60 url = httpx.URL("https://[2001:db8::ff00:42:8329]/")
2 unmodified lines
6364def test_urlparse_invalid_ipv6():65 with pytest.raises(httpx.InvalidURL) as exc:66 httpx.URL("https://[2001]/")67 assert str(exc.value) == "Invalid IPv6 address"67 assert str(exc.value) == "Invalid IPv6 address: '[2001]'"686970def test_urlparse_unescaped_idna_host():71 url = httpx.URL("https://中国.icom.museum/")
7 unmodified lines
7980def test_urlparse_invalid_idna_host():81 with pytest.raises(httpx.InvalidURL) as exc:82 httpx.URL("https://☃.com/")83 assert str(exc.value) == "Invalid IDNA hostname"83 assert str(exc.value) == "Invalid IDNA hostname: '☃.com'"848586# Tests for different port types87
11 unmodified lines
99100def test_urlparse_invalid_port():101 with pytest.raises(httpx.InvalidURL) as exc:102 httpx.URL("https://example.com:abc/")103 assert str(exc.value) == "Invalid port"103 assert str(exc.value) == "Invalid port: 'abc'"104105106# Tests for path handling107
142 unmodified lines