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 bfa01ab
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,29 +482,56 @@ 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(monkeypatch, 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(
monkeypatch, 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:
def test_proxies_from_env_http_with_auth(mocker, monkeypatch) -> None:
for schema in getproxies_environment().keys():
monkeypatch.delenv(f"{schema}_proxy", False)

url = URL("http://user:pass@aiohttp.io/path")
mocker.patch.dict(os.environ, {"http_proxy": str(url)})
ret = helpers.proxies_from_env()
Expand Down

0 comments on commit bfa01ab

Please sign in to comment.