diff --git a/python3/httplib2/__init__.py b/python3/httplib2/__init__.py index 6467c790..00a45ab0 100644 --- a/python3/httplib2/__init__.py +++ b/python3/httplib2/__init__.py @@ -1284,7 +1284,7 @@ def __init__( def connect(self): """Connect to a host on a given (SSL) port.""" - if self.proxy_info and self.proxy_info.isgood(): + if self.proxy_info and self.proxy_info.isgood() and self.proxy_info.applies_to(self.host): use_proxy = True proxy_type, proxy_host, proxy_port, proxy_rdns, proxy_user, proxy_pass, proxy_headers = ( self.proxy_info.astuple() diff --git a/tests/test_proxy.py b/tests/test_proxy.py index 4ec8aea9..61d9502b 100644 --- a/tests/test_proxy.py +++ b/tests/test_proxy.py @@ -36,16 +36,18 @@ def test_from_url_ident(): assert pi.proxy_pass == "fish" -def test_from_env(): - os.environ["http_proxy"] = "http://myproxy.example.com:8080" +def test_from_env(monkeypatch): + assert os.environ.get("http_proxy") is None + monkeypatch.setenv("http_proxy", "http://myproxy.example.com:8080") pi = httplib2.proxy_info_from_environment() assert pi.proxy_host == "myproxy.example.com" assert pi.proxy_port == 8080 -def test_from_env_https(): - os.environ["http_proxy"] = "http://myproxy.example.com:80" - os.environ["https_proxy"] = "http://myproxy.example.com:81" +def test_from_env_https(monkeypatch): + assert os.environ.get("http_proxy") is None + monkeypatch.setenv("http_proxy", "http://myproxy.example.com:80") + monkeypatch.setenv("https_proxy", "http://myproxy.example.com:81") pi = httplib2.proxy_info_from_environment("https") assert pi.proxy_host == "myproxy.example.com" assert pi.proxy_port == 81 @@ -57,10 +59,10 @@ def test_from_env_none(): assert pi is None -def test_applies_to(): - os.environ["http_proxy"] = "http://myproxy.example.com:80" - os.environ["https_proxy"] = "http://myproxy.example.com:81" - os.environ["no_proxy"] = "localhost,example.com,.wildcard" +def test_applies_to(monkeypatch): + monkeypatch.setenv("http_proxy", "http://myproxy.example.com:80") + monkeypatch.setenv("https_proxy", "http://myproxy.example.com:81") + monkeypatch.setenv("no_proxy", "localhost,example.com,.wildcard") pi = httplib2.proxy_info_from_environment() assert not pi.applies_to("localhost") assert pi.applies_to("www.google.com") @@ -71,18 +73,18 @@ def test_applies_to(): assert not pi.applies_to("pub.sub.wildcard") -def test_noproxy_trailing_comma(): - os.environ["http_proxy"] = "http://myproxy.example.com:80" - os.environ["no_proxy"] = "localhost,other.host," +def test_noproxy_trailing_comma(monkeypatch): + monkeypatch.setenv("http_proxy", "http://myproxy.example.com:80") + monkeypatch.setenv("no_proxy", "localhost,other.host,") pi = httplib2.proxy_info_from_environment() assert not pi.applies_to("localhost") assert not pi.applies_to("other.host") assert pi.applies_to("example.domain") -def test_noproxy_star(): - os.environ["http_proxy"] = "http://myproxy.example.com:80" - os.environ["NO_PROXY"] = "*" +def test_noproxy_star(monkeypatch): + monkeypatch.setenv("http_proxy", "http://myproxy.example.com:80") + monkeypatch.setenv("NO_PROXY", "*") pi = httplib2.proxy_info_from_environment() for host in ("localhost", "169.254.38.192", "www.google.com"): assert not pi.applies_to(host) @@ -171,3 +173,37 @@ def proxy_conn(client, tick): http = httplib2.Http(proxy_info=proxy_info) with tests.assert_raises(httplib2.socks.Socks5AuthError): http.request(uri, "GET") + + +def test_functional_noproxy_star_http(monkeypatch): + def handler(request): + if request.method == "CONNECT": + return tests.http_response_bytes( + status="400 Expected direct", headers={"connection": "close"}, + ) + return tests.http_response_bytes() + + with tests.server_request(handler) as uri: + uri_parsed = urllib.parse.urlparse(uri) + monkeypatch.setenv("http_proxy", uri) + monkeypatch.setenv("no_proxy", "*") + http = httplib2.Http() + response, _ = http.request(uri, "GET") + assert response.status == 200 + + +def test_functional_noproxy_star_https(monkeypatch): + def handler(request): + if request.method == "CONNECT": + return tests.http_response_bytes( + status="400 Expected direct", headers={"connection": "close"}, + ) + return tests.http_response_bytes() + + with tests.server_request(handler, tls=True) as uri: + uri_parsed = urllib.parse.urlparse(uri) + monkeypatch.setenv("https_proxy", uri) + monkeypatch.setenv("no_proxy", "*") + http = httplib2.Http(ca_certs=tests.CA_CERTS) + response, _ = http.request(uri, "GET") + assert response.status == 200