Skip to content

Commit

Permalink
ws_connect doesn't follow proxy from environment
Browse files Browse the repository at this point in the history
  • Loading branch information
mmasztalerczuk committed Mar 24, 2020
1 parent 79f47ab commit 9d4fd2c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
7 changes: 4 additions & 3 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,16 @@ class ProxyInfo:

def proxies_from_env() -> Dict[str, ProxyInfo]:
proxy_urls = {k: URL(v) for k, v in getproxies().items()
if k in ('http', 'https')}
if k in ('http', 'https', 'ws', 'wss')}
netrc_obj = netrc_from_env()
stripped = {k: strip_auth_from_url(v) for k, v in proxy_urls.items()}
ret = {}
for proto, val in stripped.items():
proxy, auth = val
if proxy.scheme == 'https':
if proxy.scheme in ('https', 'wss'):
client_logger.warning(
"HTTPS proxies %s are not supported, ignoring", proxy)
"%s proxies %s are not supported, ignoring",
proxy.scheme.upper(), proxy)
continue
if netrc_obj and auth is None:
auth_from_netrc = None
Expand Down
4 changes: 2 additions & 2 deletions docs/client_advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,8 @@ Contrary to the ``requests`` library, it won't read environment
variables by default. But you can do so by passing
``trust_env=True`` into :class:`aiohttp.ClientSession`
constructor for extracting proxy configuration from
*HTTP_PROXY* or *HTTPS_PROXY* *environment variables* (both are case
insensitive)::
*HTTP_PROXY*, *HTTPS_PROXY*, *WS_PROXY* or *WSS_PROXY* *environment
variables* (both are case insensitive)::

async with aiohttp.ClientSession(trust_env=True) as session:
async with session.get("http://python.org") as resp:
Expand Down
29 changes: 27 additions & 2 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,15 @@ def test_proxies_from_env_http(mocker) -> None:
assert ret['http'].proxy_auth is None


def test_proxies_from_env_http_proxy_for_ws_proto(mocker) -> None:
url = URL('http://aiohttp.io/path')
mocker.patch.dict(os.environ, {'ws_proxy': str(url)})
ret = helpers.proxies_from_env()
assert ret.keys() == {'ws'}
assert ret['ws'].proxy == url
assert ret['ws'].proxy_auth is None


def test_proxies_from_env_http_proxy_for_https_proto(mocker) -> None:
url = URL('http://aiohttp.io/path')
mocker.patch.dict(os.environ, {'https_proxy': str(url)})
Expand All @@ -431,14 +440,30 @@ def test_proxies_from_env_http_proxy_for_https_proto(mocker) -> None:
assert ret['https'].proxy == url
assert ret['https'].proxy_auth is None

def test_proxies_from_env_http_proxy_for_wss_proto(mocker) -> None:
url = URL('http://aiohttp.io/path')
mocker.patch.dict(os.environ, {'wss_proxy': str(url)})
ret = helpers.proxies_from_env()
assert ret.keys() == {'wss'}
assert ret['wss'].proxy == url
assert ret['wss'].proxy_auth is None


def test_proxies_from_env_https_proxy_skipped(mocker) -> None:
url = URL('https://aiohttp.io/path')
mocker.patch.dict(os.environ, {'https_proxy': str(url)})
log = mocker.patch('aiohttp.log.client_logger.warning')
assert helpers.proxies_from_env() == {}
log.assert_called_with('HTTPS proxies %s are not supported, ignoring',
URL('https://aiohttp.io/path'))
log.assert_called_with('%s proxies %s are not supported, ignoring',
'HTTPS', URL('https://aiohttp.io/path'))

def test_proxies_from_env_wss_proxy_skipped(mocker) -> None:
url = URL('wss://aiohttp.io/path')
mocker.patch.dict(os.environ, {'wss_proxy': str(url)})
log = mocker.patch('aiohttp.log.client_logger.warning')
assert helpers.proxies_from_env() == {}
log.assert_called_with('%s proxies %s are not supported, ignoring',
'WSS', URL('wss://aiohttp.io/path'))


def test_proxies_from_env_http_with_auth(mocker) -> None:
Expand Down

0 comments on commit 9d4fd2c

Please sign in to comment.