Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable NO_PROXY environment variable support #835

Merged
merged 4 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions httpx/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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
)
Expand Down
16 changes: 16 additions & 0 deletions tests/client/test_proxies.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

import httpx
from httpx import URL
evanlurvey marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.parametrize(
Expand Down Expand Up @@ -87,3 +88,18 @@ 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")


def test_no_proxy_returns_correct_dispatcher(monkeypatch):
monkeypatch.setenv("HTTP_PROXY", "http://example.com")
monkeypatch.setenv("NO_PROXY", "google.com")
client = httpx.AsyncClient()
dispatcher = client.dispatcher_for_url(URL("http://google.com"))
assert dispatcher == client.dispatch


def test_no_proxy_not_set_returns_correct_dispatcher(monkeypatch):
monkeypatch.setenv("HTTP_PROXY", "http://example.com")
client = httpx.AsyncClient()
dispatcher = client.dispatcher_for_url(URL("http://google.com"))
assert dispatcher == client.proxies["http"]
evanlurvey marked this conversation as resolved.
Show resolved Hide resolved