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__)
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
)
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
)
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