diff options
Diffstat (limited to 'tests/test_proxy.py')
-rw-r--r-- | tests/test_proxy.py | 68 |
1 files changed, 51 insertions, 17 deletions
diff --git a/tests/test_proxy.py b/tests/test_proxy.py index 375367f..edafe01 100644 --- a/tests/test_proxy.py +++ b/tests/test_proxy.py @@ -32,20 +32,22 @@ def test_from_url_ident(): pi = httplib2.proxy_info_from_url("http://zoidberg:fish@someproxy:99") assert pi.proxy_host == "someproxy" assert pi.proxy_port == 99 - assert pi.proxy_user == b"zoidberg" - assert pi.proxy_pass == b"fish" + assert pi.proxy_user == "zoidberg" + 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,35 @@ 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: + 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: + 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 |