Skip to content
Myr

Improve InvalidURL error message. (#3250)

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 beb501f · Browse files · 3 files+21 added−7 removed
This commit

State

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

Diff stats

3 files+21 added−7 removed

  • tests:+6 added−4 removed
  • other:+15 added−3 removed

3 files+21 added−7 removed

modified:httpx/_urlparse.py+14 added-3 removed
158 unmodified lines
159160 # If a URL includes any ASCII control characters including \t, \r, \n,161 # then treat it as invalid.162 if any(char.isascii() and not char.isprintable() for char in url):163 raise InvalidURL("Invalid non-printable ASCII character in URL")163 char = next(char for char in url if char.isascii() and not char.isprintable())164 idx = url.find(char)165 error = (166 f"Invalid non-printable ASCII character in URL, {char!r} at position {idx}."167 )168 raise InvalidURL(error)169170 # Some keyword arguments require special handling.171 # ------------------------------------------------172
36 unmodified lines
209210 # If a component includes any ASCII control characters including \t, \r, \n,211 # then treat it as invalid.212 if any(char.isascii() and not char.isprintable() for char in value):208 raise InvalidURL(209 f"Invalid non-printable ASCII character in URL component '{key}'"213 char = next(214 char for char in value if char.isascii() and not char.isprintable()215 )216 idx = value.find(char)217 error = (218 f"Invalid non-printable ASCII character in URL {key} component, "219 f"{char!r} at position {idx}."220 )221 raise InvalidURL(error)222223 # Ensure that keyword arguments match as a valid regex.224 if not COMPONENT_REGEX[key].fullmatch(value):225 raise InvalidURL(f"Invalid URL component '{key}'")
280 unmodified lines
modified:tests/models/test_url.py+6 added-4 removed
365 unmodified lines
366367def test_url_non_printing_character_in_url():368 with pytest.raises(httpx.InvalidURL) as exc:369 httpx.URL("https://www.example.com/\n")370 assert str(exc.value) == "Invalid non-printable ASCII character in URL"370 assert str(exc.value) == (371 "Invalid non-printable ASCII character in URL, '\\n' at position 24."372 )373374375def test_url_non_printing_character_in_component():376 with pytest.raises(httpx.InvalidURL) as exc:377 httpx.URL("https://www.example.com", path="/\n")376 assert (377 str(exc.value)378 == "Invalid non-printable ASCII character in URL component 'path'"378 assert str(exc.value) == (379 "Invalid non-printable ASCII character in URL path component, "380 "'\\n' at position 1."381 )382383384# Test for url components
499 unmodified lines
modified:CHANGELOG.md+1 added
10 unmodified lines
11* Support for `zstd` content decoding using the python `zstandard` package is added. Installable using `httpx[zstd]`. (#3139)1213### Fixed1415* Improved error messaging for `InvalidURL` exceptions. (#3250)16* Fix `app` type signature in `ASGITransport`. (#3109)1718## 0.27.0 (21st February, 2024)19
1075 unmodified lines