aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Shepelev <temotor@gmail.com>2020-04-02 19:27:42 +0300
committerSergey Shepelev <temotor@gmail.com>2020-04-02 19:27:42 +0300
commite9a98f93c9e29a886231cf02a10b4dafa6793ef2 (patch)
treedaa0e05ce1acbe38d0808264da0fff631b0480ad
parent67463426cf79af766176b1f318fefefd3d5edc23 (diff)
downloadhttplib2-e9a98f93c9e29a886231cf02a10b4dafa6793ef2.tar.gz
python3: no_proxy was not checked with https
fixes https://github.com/httplib2/httplib2/issues/160
-rw-r--r--python3/httplib2/__init__.py2
-rw-r--r--tests/test_proxy.py66
2 files changed, 52 insertions, 16 deletions
diff --git a/python3/httplib2/__init__.py b/python3/httplib2/__init__.py
index 6467c79..00a45ab 100644
--- a/python3/httplib2/__init__.py
+++ b/python3/httplib2/__init__.py
@@ -1284,7 +1284,7 @@ class HTTPSConnectionWithTimeout(http.client.HTTPSConnection):
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 4ec8aea..61d9502 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 test_socks5_auth():
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