Skip to content

Commit 1ae9b60

Browse files
committed
Improve message for ProxySchemeUnknown exception
1 parent dd00949 commit 1ae9b60

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/urllib3/exceptions.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,17 @@ class ProxySchemeUnknown(AssertionError, URLSchemeUnknown):
289289
# TODO(t-8ch): Stop inheriting from AssertionError in v2.0.
290290

291291
def __init__(self, scheme):
292-
message = "Not supported proxy scheme %s" % scheme
292+
# 'localhost' is here because our URL parser parses
293+
# localhost:8080 -> scheme=localhost, remove if we fix this.
294+
if scheme == "localhost":
295+
scheme = None
296+
if scheme is None:
297+
message = "Proxy URL had no scheme, should start with http:// or https://"
298+
else:
299+
message = (
300+
"Proxy URL had unsupported scheme %s, should use http:// or https://"
301+
% scheme
302+
)
293303
super(ProxySchemeUnknown, self).__init__(message)
294304

295305

test/with_dummyserver/test_proxy_poolmanager.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
ConnectTimeoutError,
2424
MaxRetryError,
2525
ProxyError,
26+
ProxySchemeUnknown,
2627
ProxySchemeUnsupported,
2728
SSLError,
2829
)
@@ -502,6 +503,27 @@ def test_scheme_host_case_insensitive(self):
502503
r = http.request("GET", "%s/" % self.https_url.upper())
503504
assert r.status == 200
504505

506+
@pytest.mark.parametrize(
507+
"url, error_msg",
508+
[
509+
(
510+
"127.0.0.1",
511+
"Proxy URL had no scheme, should start with http:// or https://",
512+
),
513+
(
514+
"localhost:8080",
515+
"Proxy URL had no scheme, should start with http:// or https://",
516+
),
517+
(
518+
"ftp://google.com",
519+
"Proxy URL had unsupported scheme ftp, should use http:// or https://",
520+
),
521+
],
522+
)
523+
def test_invalid_schema(self, url, error_msg):
524+
with pytest.raises(ProxySchemeUnknown, match=error_msg):
525+
proxy_from_url(url)
526+
505527

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

0 commit comments

Comments
 (0)