Skip to content

Commit 9838429

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

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

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_invalid_url.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import httpx
2+
import pytest
3+
4+
5+
def test_invalid_url_with_scheme_no_host():
6+
"""
7+
Regression test for: https://github.com/encode/httpx/issues/1832
8+
URLs with scheme but no host should raise InvalidURL.
9+
"""
10+
with httpx.Client() as client:
11+
with pytest.raises(httpx.InvalidURL) as exc:
12+
client.get("https:/google.com")
13+
assert "has scheme but missing host" in str(exc.value)
14+
15+
with pytest.raises(httpx.InvalidURL) as exc:
16+
client.get("https:///google.com")
17+
assert "has scheme but missing host" in str(exc.value)

0 commit comments

Comments
 (0)