Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_proxy_from_env did not clear proxies already set in the #5557

Merged
merged 1 commit into from Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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