]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Enable NO_PROXY environment variable support (#835)
authorEvan Lurvey <54965655+evanlurvey@users.noreply.github.com>
Mon, 2 Mar 2020 15:51:34 +0000 (09:51 -0600)
committerGitHub <noreply@github.com>
Mon, 2 Mar 2020 15:51:34 +0000 (16:51 +0100)
* 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 <florimond.manca@gmail.com>
* Update tests/client/test_proxies.py

Co-Authored-By: Florimond Manca <florimond.manca@gmail.com>
Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
httpx/_client.py
tests/client/test_proxies.py

index e9d33a94a37d19ec54ac2fd1253033b473d3e1c3..2a310e26d18810d371bd7f184840e04910248ab7 100644 (file)
@@ -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
             )
index a1008739c970d530647ce1192f94bbe2043a6464..e76e520c21e627a91b2fa772cadd3463df3046f7 100644 (file)
@@ -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