Skip to content

Commit a049701

Browse files
authored
Fix usage of proxy.py in test_proxy_functional (#7773) (#7876)
(cherry picked from commit 4d9fc63)
1 parent 5fa260a commit a049701

File tree

1 file changed

+30
-46
lines changed

1 file changed

+30
-46
lines changed

tests/test_proxy_functional.py

+30-46
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import asyncio
2-
import functools
32
import os
43
import pathlib
5-
import platform
4+
import ssl
5+
import sys
66
from re import match as match_regex
77
from unittest import mock
88
from uuid import uuid4
@@ -13,8 +13,8 @@
1313

1414
import aiohttp
1515
from aiohttp import web
16-
from aiohttp.client_exceptions import ClientConnectionError, ClientProxyConnectionError
17-
from aiohttp.helpers import IS_MACOS, IS_WINDOWS, PY_310
16+
from aiohttp.client_exceptions import ClientConnectionError
17+
from aiohttp.helpers import IS_MACOS, IS_WINDOWS
1818

1919
pytestmark = [
2020
pytest.mark.filterwarnings(
@@ -28,20 +28,7 @@
2828
]
2929

3030

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

4633

4734
@pytest.fixture
@@ -51,6 +38,9 @@ def secure_proxy_url(tls_certificate_pem_path):
5138
This fixture also spawns that instance and tears it down after the test.
5239
"""
5340
proxypy_args = [
41+
# --threadless does not work on windows, see
42+
# https://github.com/abhinavsingh/proxy.py/issues/492
43+
"--threaded" if os.name == "nt" else "--threadless",
5444
"--num-workers",
5545
"1", # the tests only send one query anyway
5646
"--hostname",
@@ -112,32 +102,20 @@ async def handler(*args, **kwargs):
112102
)
113103

114104

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

162140

163-
@secure_proxy_xfail_under_py310_linux(raises=AssertionError)
164141
@pytest.mark.parametrize("web_server_endpoint_type", ("https",))
165142
@pytest.mark.usefixtures("loop")
143+
@pytest.mark.skipif(
144+
ASYNCIO_SUPPORTS_TLS_IN_TLS, reason="asyncio on this python supports TLS in TLS"
145+
)
146+
@pytest.mark.filterwarnings(r"ignore:.*ssl.OP_NO_SSL*")
147+
# Filter out the warning from
148+
# https://github.com/abhinavsingh/proxy.py/blob/30574fd0414005dfa8792a6e797023e862bdcf43/proxy/common/utils.py#L226
149+
# otherwise this test will fail because the proxy will die with an error.
166150
async def test_https_proxy_unsupported_tls_in_tls(
167-
client_ssl_ctx,
168-
secure_proxy_url,
169-
web_server_endpoint_type,
151+
client_ssl_ctx: ssl.SSLContext,
152+
secure_proxy_url: URL,
153+
web_server_endpoint_type: str,
170154
) -> None:
171155
"""Ensure connecting to TLS endpoints w/ HTTPS proxy needs patching.
172156

0 commit comments

Comments
 (0)