Skip to content

Commit 4d9fc63

Browse files
authored
Fix usage of proxy.py in test_proxy_functional (#7773)
1 parent 25ef450 commit 4d9fc63

File tree

1 file changed

+29
-47
lines changed

1 file changed

+29
-47
lines changed

tests/test_proxy_functional.py

+29-47
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# type: ignore
22
import asyncio
3-
import functools
43
import os
54
import pathlib
6-
import platform
5+
import ssl
6+
import sys
77
from re import match as match_regex
88
from typing import Any
99
from unittest import mock
@@ -15,8 +15,7 @@
1515

1616
import aiohttp
1717
from aiohttp import web
18-
from aiohttp.client_exceptions import ClientConnectionError, ClientProxyConnectionError
19-
from aiohttp.helpers import PY_310
18+
from aiohttp.client_exceptions import ClientConnectionError
2019

2120
pytestmark = [
2221
pytest.mark.filterwarnings(
@@ -30,20 +29,7 @@
3029
]
3130

3231

33-
secure_proxy_xfail = functools.partial(
34-
pytest.mark.xfail,
35-
(PY_310 and platform.system() != "Darwin") or platform.system() == "Windows",
36-
reason=(
37-
"The secure proxy fixture does not seem to work "
38-
"under Python 3.10 on Linux and any Python on Windows. "
39-
"See https://github.com/abhinavsingh/proxy.py/issues/622."
40-
),
41-
)
42-
43-
ASYNCIO_SUPPORTS_TLS_IN_TLS = hasattr(
44-
asyncio.sslproto._SSLProtocolTransport,
45-
"_start_tls_compatible",
46-
)
32+
ASYNCIO_SUPPORTS_TLS_IN_TLS = sys.version_info >= (3, 11)
4733

4834

4935
@pytest.fixture
@@ -53,7 +39,9 @@ def secure_proxy_url(tls_certificate_pem_path):
5339
This fixture also spawns that instance and tears it down after the test.
5440
"""
5541
proxypy_args = [
56-
"--threadless", # use asyncio
42+
# --threadless does not work on windows, see
43+
# https://github.com/abhinavsingh/proxy.py/issues/492
44+
"--threaded" if os.name == "nt" else "--threadless",
5745
"--num-workers",
5846
"1", # the tests only send one query anyway
5947
"--hostname",
@@ -113,32 +101,20 @@ async def handler(*args, **kwargs):
113101
)
114102

115103

116-
@pytest.fixture
117-
def _pretend_asyncio_supports_tls_in_tls(
118-
monkeypatch,
119-
web_server_endpoint_type,
120-
):
121-
if web_server_endpoint_type != "https" or ASYNCIO_SUPPORTS_TLS_IN_TLS:
122-
return
123-
124-
# for https://github.com/python/cpython/pull/28073
125-
# and https://bugs.python.org/issue37179
126-
monkeypatch.setattr(
127-
asyncio.sslproto._SSLProtocolTransport,
128-
"_start_tls_compatible",
129-
True,
130-
raising=False,
131-
)
132-
133-
134-
@secure_proxy_xfail(raises=ClientProxyConnectionError)
104+
@pytest.mark.skipif(
105+
not ASYNCIO_SUPPORTS_TLS_IN_TLS,
106+
reason="asyncio on this python does not support TLS in TLS",
107+
)
135108
@pytest.mark.parametrize("web_server_endpoint_type", ("http", "https"))
136-
@pytest.mark.usefixtures("_pretend_asyncio_supports_tls_in_tls", "loop")
109+
@pytest.mark.filterwarnings(r"ignore:.*ssl.OP_NO_SSL*")
110+
# Filter out the warning from
111+
# https://github.com/abhinavsingh/proxy.py/blob/30574fd0414005dfa8792a6e797023e862bdcf43/proxy/common/utils.py#L226
112+
# otherwise this test will fail because the proxy will die with an error.
137113
async def test_secure_https_proxy_absolute_path(
138-
client_ssl_ctx,
139-
secure_proxy_url,
140-
web_server_endpoint_url,
141-
web_server_endpoint_payload,
114+
client_ssl_ctx: ssl.SSLContext,
115+
secure_proxy_url: URL,
116+
web_server_endpoint_url: str,
117+
web_server_endpoint_payload: str,
142118
) -> None:
143119
"""Ensure HTTP(S) sites are accessible through a secure proxy."""
144120
conn = aiohttp.TCPConnector()
@@ -158,13 +134,19 @@ async def test_secure_https_proxy_absolute_path(
158134
await conn.close()
159135

160136

161-
@secure_proxy_xfail(raises=AssertionError)
162137
@pytest.mark.parametrize("web_server_endpoint_type", ("https",))
163138
@pytest.mark.usefixtures("loop")
139+
@pytest.mark.skipif(
140+
ASYNCIO_SUPPORTS_TLS_IN_TLS, reason="asyncio on this python supports TLS in TLS"
141+
)
142+
@pytest.mark.filterwarnings(r"ignore:.*ssl.OP_NO_SSL*")
143+
# Filter out the warning from
144+
# https://github.com/abhinavsingh/proxy.py/blob/30574fd0414005dfa8792a6e797023e862bdcf43/proxy/common/utils.py#L226
145+
# otherwise this test will fail because the proxy will die with an error.
164146
async def test_https_proxy_unsupported_tls_in_tls(
165-
client_ssl_ctx,
166-
secure_proxy_url,
167-
web_server_endpoint_type,
147+
client_ssl_ctx: ssl.SSLContext,
148+
secure_proxy_url: URL,
149+
web_server_endpoint_type: str,
168150
) -> None:
169151
"""Ensure connecting to TLS endpoints w/ HTTPS proxy needs patching.
170152

0 commit comments

Comments
 (0)