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
Provenance
- Commit
7c0cda1- Tree
773d503- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
beb501f
3 files changed+21 added−7 removed
modified:httpx/_urlparse.py+14 added-3 removed
158 unmodified lines159 160 # 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)169 170 # Some keyword arguments require special handling.171 # ------------------------------------------------172 36 unmodified lines209 210 # 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)222 223 # 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 linesmodified:tests/models/test_url.py+6 added-4 removed
365 unmodified lines366 367 def 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+ )373 374 375 def 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 )382 383 384 # Test for url components499 unmodified linesmodified:CHANGELOG.md+1 added
10 unmodified lines11 * Support for `zstd` content decoding using the python `zstandard` package is added. Installable using `httpx[zstd]`. (#3139)12 13 ### Fixed14 15+* Improved error messaging for `InvalidURL` exceptions. (#3250)16 * Fix `app` type signature in `ASGITransport`. (#3109)17 18 ## 0.27.0 (21st February, 2024)19 1075 unmodified lines