From: Evan Lurvey <54965655+evanlurvey@users.noreply.github.com> Date: Mon, 2 Mar 2020 15:51:34 +0000 (-0600) Subject: Enable NO_PROXY environment variable support (#835) X-Git-Tag: 0.12.0~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=707e54c48415e98224ca74e67809a5593b41b03d;p=thirdparty%2Fhttpx.git Enable NO_PROXY environment variable support (#835) * Enabling NO_PROXY env support * Enabling NO_PROXY env var support and writing tests * Update tests/client/test_proxies.py Co-Authored-By: Florimond Manca * Update tests/client/test_proxies.py Co-Authored-By: Florimond Manca Co-authored-by: Florimond Manca --- diff --git a/httpx/_client.py b/httpx/_client.py index e9d33a94..2a310e26 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -43,7 +43,12 @@ from ._models import ( 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 @@ class Client(BaseClient): 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 @@ class AsyncClient(BaseClient): 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 a1008739..e76e520c 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