Skip to content

Commit 2dcfde7

Browse files
authored
Improve message for ProxySchemeUnknown exception
1 parent c915450 commit 2dcfde7

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/urllib3/exceptions.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,14 @@ class ProxySchemeUnknown(AssertionError, URLSchemeUnknown):
285285
# TODO(t-8ch): Stop inheriting from AssertionError in v2.0.
286286

287287
def __init__(self, scheme):
288-
message = f"Not supported proxy scheme {scheme}"
288+
# 'localhost' is here because our URL parser parses
289+
# localhost:8080 -> scheme=localhost, remove if we fix this.
290+
if scheme == "localhost":
291+
scheme = None
292+
if scheme is None:
293+
message = "Proxy URL had no scheme, should start with http:// or https://"
294+
else:
295+
message = f"Proxy URL had unsupported scheme {scheme}, should use http:// or https://"
289296
super().__init__(message)
290297

291298

test/with_dummyserver/test_proxy_poolmanager.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
ConnectTimeoutError,
1717
MaxRetryError,
1818
ProxyError,
19+
ProxySchemeUnknown,
1920
ProxySchemeUnsupported,
2021
SSLError,
2122
)
@@ -450,6 +451,27 @@ def test_scheme_host_case_insensitive(self):
450451
r = http.request("GET", f"{self.https_url.upper()}/")
451452
assert r.status == 200
452453

454+
@pytest.mark.parametrize(
455+
"url, error_msg",
456+
[
457+
(
458+
"127.0.0.1",
459+
"Proxy URL had no scheme, should start with http:// or https://",
460+
),
461+
(
462+
"localhost:8080",
463+
"Proxy URL had no scheme, should start with http:// or https://",
464+
),
465+
(
466+
"ftp://google.com",
467+
"Proxy URL had unsupported scheme ftp, should use http:// or https://",
468+
),
469+
],
470+
)
471+
def test_invalid_schema(self, url, error_msg):
472+
with pytest.raises(ProxySchemeUnknown, match=error_msg):
473+
proxy_from_url(url)
474+
453475

454476
@pytest.mark.skipif(not HAS_IPV6, reason="Only runs on IPv6 systems")
455477
class TestIPv6HTTPProxyManager(IPv6HTTPDummyProxyTestCase):

0 commit comments

Comments
 (0)