Skip to content

Commit

Permalink
test_proxy_from_env did not clear proxies already set in the
Browse files Browse the repository at this point in the history
environment. Added code to first clear proxies that were already set.

Refactored the parametrize input on test_proxy_from_env to work more like test_get_env_proxy_for_url to be consistent.
  • Loading branch information
scirelli committed Mar 23, 2021
1 parent 1a4126a commit 964a427
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGES/5554.bugfix
@@ -0,0 +1 @@
Fixes failing tests when an environment variable <scheme>_proxy is set.
69 changes: 51 additions & 18 deletions tests/test_helpers.py
Expand Up @@ -2,7 +2,6 @@
import asyncio
import base64
import gc
import os
import platform
from math import isclose, modf
from unittest import mock
Expand Down Expand Up @@ -482,35 +481,69 @@ def test_set_content_disposition_bad_param() -> None:
# --------------------- proxies_from_env ------------------------------


@pytest.mark.parametrize("protocol", ["http", "https", "ws", "wss"])
def test_proxies_from_env(monkeypatch, protocol) -> None:
url = URL("http://aiohttp.io/path")
monkeypatch.setenv(protocol + "_proxy", str(url))
@pytest.mark.parametrize(
("proxy_env_vars", "url_input", "expected_scheme"),
(
({"http_proxy": "http://aiohttp.io/path"}, "http://aiohttp.io/path", "http"),
({"https_proxy": "http://aiohttp.io/path"}, "http://aiohttp.io/path", "https"),
({"ws_proxy": "http://aiohttp.io/path"}, "http://aiohttp.io/path", "ws"),
({"wss_proxy": "http://aiohttp.io/path"}, "http://aiohttp.io/path", "wss"),
),
indirect=["proxy_env_vars"],
ids=("http", "https", "ws", "wss"),
)
@pytest.mark.usefixtures("proxy_env_vars")
def test_proxies_from_env(url_input, expected_scheme) -> None:
url = URL(url_input)
ret = helpers.proxies_from_env()
assert ret.keys() == {protocol}
assert ret[protocol].proxy == url
assert ret[protocol].proxy_auth is None
assert ret.keys() == {expected_scheme}
assert ret[expected_scheme].proxy == url
assert ret[expected_scheme].proxy_auth is None


@pytest.mark.parametrize("protocol", ["https", "wss"])
def test_proxies_from_env_skipped(monkeypatch, caplog, protocol) -> None:
url = URL(protocol + "://aiohttp.io/path")
monkeypatch.setenv(protocol + "_proxy", str(url))
@pytest.mark.parametrize(
("proxy_env_vars", "url_input", "expected_scheme"),
(
(
{"https_proxy": "https://aiohttp.io/path"},
"https://aiohttp.io/path",
"https",
),
({"wss_proxy": "wss://aiohttp.io/path"}, "wss://aiohttp.io/path", "wss"),
),
indirect=["proxy_env_vars"],
ids=("https", "wss"),
)
@pytest.mark.usefixtures("proxy_env_vars")
def test_proxies_from_env_skipped(caplog, url_input, expected_scheme) -> None:
url = URL(url_input)
assert helpers.proxies_from_env() == {}
assert len(caplog.records) == 1
log_message = "{proto!s} proxies {url!s} are not supported, ignoring".format(
proto=protocol.upper(), url=url
proto=expected_scheme.upper(), url=url
)
assert caplog.record_tuples == [("aiohttp.client", 30, log_message)]


def test_proxies_from_env_http_with_auth(mocker) -> None:
@pytest.mark.parametrize(
("proxy_env_vars", "url_input", "expected_scheme"),
(
(
{"http_proxy": "http://user:pass@aiohttp.io/path"},
"http://user:pass@aiohttp.io/path",
"http",
),
),
indirect=["proxy_env_vars"],
ids=("http",),
)
@pytest.mark.usefixtures("proxy_env_vars")
def test_proxies_from_env_http_with_auth(url_input, expected_scheme) -> None:
url = URL("http://user:pass@aiohttp.io/path")
mocker.patch.dict(os.environ, {"http_proxy": str(url)})
ret = helpers.proxies_from_env()
assert ret.keys() == {"http"}
assert ret["http"].proxy == url.with_user(None)
proxy_auth = ret["http"].proxy_auth
assert ret.keys() == {expected_scheme}
assert ret[expected_scheme].proxy == url.with_user(None)
proxy_auth = ret[expected_scheme].proxy_auth
assert proxy_auth.login == "user"
assert proxy_auth.password == "pass"
assert proxy_auth.encoding == "latin1"
Expand Down

0 comments on commit 964a427

Please sign in to comment.