Skip to content

Commit 34c3a80

Browse files
author
rodrigo.nogueira
committed
Fix inconsistent invalid URL handling (#1832)
1 parent ae1b9f6 commit 34c3a80

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

httpx/_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ def _merge_url(self, url: URL | str) -> URL:
394394
to create the URL used for the outgoing request.
395395
"""
396396
merge_url = URL(url)
397+
if merge_url.scheme and not merge_url.host:
398+
raise InvalidURL(f"Invalid URL '{url}': has scheme but missing host")
397399
if merge_url.is_relative_url:
398400
# To merge URLs we always append to the base URL. To get this
399401
# behaviour correct we always ensure the base URL ends in a '/'

tests/client/test_client.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ def test_get_invalid_url(server, url):
4444
client.get(url)
4545

4646

47+
def test_get_invalid_url_with_scheme_no_host():
48+
"""
49+
Regression test for: https://github.com/encode/httpx/issues/1832
50+
URLs with scheme but no host should raise InvalidURL.
51+
"""
52+
with httpx.Client() as client:
53+
with pytest.raises(httpx.InvalidURL) as exc:
54+
client.get("https:/google.com")
55+
assert "has scheme but missing host" in str(exc.value)
56+
57+
with pytest.raises(httpx.InvalidURL) as exc:
58+
client.get("https:///google.com")
59+
assert "has scheme but missing host" in str(exc.value)
60+
61+
4762
def test_build_request(server):
4863
url = server.url.copy_with(path="/echo_headers")
4964
headers = {"Custom-header": "value"}

0 commit comments

Comments
 (0)