Skip to content

Commit

Permalink
Improve message for ProxySchemeUnknown exception
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Dec 10, 2020
1 parent dd00949 commit 915481d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/urllib3/exceptions.py
Expand Up @@ -289,7 +289,14 @@ class ProxySchemeUnknown(AssertionError, URLSchemeUnknown):
# TODO(t-8ch): Stop inheriting from AssertionError in v2.0.

def __init__(self, scheme):
message = "Not supported proxy scheme %s" % scheme
# 'localhost' is here because our URL parser parses
# localhost:8080 -> scheme=localhost, remove if we fix this.
if scheme == "localhost":
scheme = None
if scheme is None:
message = "Proxy URL had no scheme, should start with http:// or https://"
else:
message = f"Proxy URL had unsupported scheme {scheme}, should use http:// or https://"
super(ProxySchemeUnknown, self).__init__(message)


Expand Down
22 changes: 22 additions & 0 deletions test/with_dummyserver/test_proxy_poolmanager.py
Expand Up @@ -23,6 +23,7 @@
ConnectTimeoutError,
MaxRetryError,
ProxyError,
ProxySchemeUnknown,
ProxySchemeUnsupported,
SSLError,
)
Expand Down Expand Up @@ -502,6 +503,27 @@ def test_scheme_host_case_insensitive(self):
r = http.request("GET", "%s/" % self.https_url.upper())
assert r.status == 200

@pytest.mark.parametrize(
"url, error_msg",
[
(
"127.0.0.1",
"Proxy URL had no scheme, should start with http:// or https://",
),
(
"localhost:8080",
"Proxy URL had no scheme, should start with http:// or https://",
),
(
"ftp://google.com",
"Proxy URL had unsupported scheme ftp, should use http:// or https://",
),
],
)
def test_invalid_schema(self, url, error_msg):
with pytest.raises(ProxySchemeUnknown, match=error_msg):
proxy_from_url(url)


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

0 comments on commit 915481d

Please sign in to comment.