Improve message for ProxySchemeUnknown exception
MODIFY
src/urllib3/exceptions.py
src/urllib3/exceptions.py
@@ -285,7 +285,14 @@ class ProxySchemeUnknown(AssertionError, URLSchemeUnknown):
# TODO(t-8ch): Stop inheriting from AssertionError in v2.0.
def __init__(self, scheme):
- message = f"Not supported proxy scheme {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().__init__(message)

MODIFY
test/with_dummyserver/test_proxy_poolmanager.py
test/with_dummyserver/test_proxy_poolmanager.py
@@ -16,6 +16,7 @@ from urllib3.exceptions import (
ConnectTimeoutError,
MaxRetryError,
ProxyError,
+ ProxySchemeUnknown,
ProxySchemeUnsupported,
SSLError,
)
@@ -450,6 +451,27 @@ class TestHTTPProxyManager(HTTPDummyProxyTestCase):
r = http.request("GET", f"{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):

Improve message for ProxySchemeUnknown exception