Skip to content

Commit

Permalink
python3: no_proxy was not checked with https
Browse files Browse the repository at this point in the history
fixes #160
  • Loading branch information
temoto committed Apr 2, 2020
1 parent 6746342 commit e9a98f9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
2 changes: 1 addition & 1 deletion python3/httplib2/__init__.py
Expand Up @@ -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()
Expand Down
66 changes: 51 additions & 15 deletions tests/test_proxy.py
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit e9a98f9

Please sign in to comment.