diff --git a/httpx/_client.py b/httpx/_client.py index e9d33a94a3..2a310e26d1 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -43,7 +43,12 @@ URLTypes, ) from ._status_codes import codes -from ._utils import NetRCInfo, get_environment_proxies, get_logger +from ._utils import ( + NetRCInfo, + get_environment_proxies, + get_logger, + should_not_be_proxied, +) logger = get_logger(__name__) @@ -517,7 +522,7 @@ def dispatcher_for_url(self, url: URL) -> SyncDispatcher: Returns the SyncDispatcher instance that should be used for a given URL. This will either be the standard connection pool, or a proxy. """ - if self.proxies: + if self.proxies and not should_not_be_proxied(url): is_default_port = (url.scheme == "http" and url.port == 80) or ( url.scheme == "https" and url.port == 443 ) @@ -1032,7 +1037,7 @@ def dispatcher_for_url(self, url: URL) -> AsyncDispatcher: Returns the AsyncDispatcher instance that should be used for a given URL. This will either be the standard connection pool, or a proxy. """ - if self.proxies: + if self.proxies and not should_not_be_proxied(url): is_default_port = (url.scheme == "http" and url.port == 80) or ( url.scheme == "https" and url.port == 443 ) diff --git a/tests/client/test_proxies.py b/tests/client/test_proxies.py index a1008739c9..e76e520c21 100644 --- a/tests/client/test_proxies.py +++ b/tests/client/test_proxies.py @@ -87,3 +87,32 @@ def test_dispatcher_for_request(url, proxies, expected): def test_unsupported_proxy_scheme(): with pytest.raises(ValueError): httpx.AsyncClient(proxies="ftp://127.0.0.1") + + +@pytest.mark.parametrize( + ["url", "env", "expected"], + [ + ("http://google.com", {}, None), + ( + "http://google.com", + {"HTTP_PROXY": "http://example.com"}, + "http://example.com", + ), + ( + "http://google.com", + {"HTTP_PROXY": "http://example.com", "NO_PROXY": "google.com"}, + None, + ), + ], +) +def test_proxies_environ(monkeypatch, url, env, expected): + for name, value in env.items(): + monkeypatch.setenv(name, value) + + client = httpx.AsyncClient() + dispatcher = client.dispatcher_for_url(httpx.URL(url)) + + if expected is None: + assert dispatcher == client.dispatch + else: + assert dispatcher.proxy_url == expected