33
44from contextlib import AsyncExitStack
55from multiprocessing import Process , Queue
6- from typing import Any , Dict , Optional
6+ from typing import Any , Dict
77
88import anyio
99import httpx
1515from typing_extensions import override
1616
1717from .app .echo_ws_app import get_app as get_ws_test_app
18- from .app .tool import TestServer
19- from .conftest import TestServerFixture
18+ from .app .tool import AutoServer
19+ from .conftest import AutoServerFixture
2020from .tool import (
2121 AbstractTestProxy ,
2222 Tool4TestFixture ,
2525DEFAULT_HOST = "127.0.0.1"
2626DEFAULT_PORT = 0 # random port
2727
28- # https://www.python-httpx.org/advanced/#http-proxying
28+ # https://www.python-httpx.org/advanced/proxies/
29+ # NOTE: Foce to connect directly, avoid using system proxies
2930NO_PROXIES : Dict [Any , Any ] = {"all://" : None }
3031
3132
@@ -36,7 +37,7 @@ def _subprocess_run_echo_ws_server(queue: "Queue[str]"):
3637 queue: The queue for subprocess to put the url of echo ws app.
3738 After the server is started, the url will be put into the queue.
3839 """
39- target_ws_server = TestServer (
40+ target_ws_server = AutoServer (
4041 app = get_ws_test_app ().app ,
4142 host = DEFAULT_HOST ,
4243 port = DEFAULT_PORT ,
@@ -55,29 +56,22 @@ async def run():
5556
5657def _subprocess_run_httpx_ws (
5758 queue : "Queue[str]" ,
58- kwargs_async_client : Optional [Dict [str , Any ]] = None ,
59- kwargs_aconnect_ws : Optional [Dict [str , Any ]] = None ,
59+ aconnect_ws_url : str ,
6060):
6161 """Run aconnect_ws in subprocess.
6262
6363 Args:
6464 queue: The queue for subprocess to put something for flag of ws connection established.
65- kwargs_async_client: The kwargs for `httpx.AsyncClient`
66- kwargs_aconnect_ws: The kwargs for `httpx_ws.aconnect_ws`
65+ aconnect_ws_url: The websocket url for aconnect_ws.
6766 """
68- kwargs_async_client = kwargs_async_client or {}
69- kwargs_aconnect_ws = kwargs_aconnect_ws or {}
70-
71- kwargs_async_client .pop ("proxies" , None )
72- kwargs_aconnect_ws .pop ("client" , None )
7367
7468 async def run ():
7569 _exit_stack = AsyncExitStack ()
76- _temp_client = httpx .AsyncClient (proxies = NO_PROXIES , ** kwargs_async_client )
70+ _temp_client = httpx .AsyncClient (mounts = NO_PROXIES )
7771 _ = await _exit_stack .enter_async_context (
7872 aconnect_ws (
7973 client = _temp_client ,
80- ** kwargs_aconnect_ws ,
74+ url = aconnect_ws_url ,
8175 )
8276 )
8377 queue .put ("done" )
@@ -95,32 +89,32 @@ class TestReverseWsProxy(AbstractTestProxy):
9589 @pytest .fixture ()
9690 async def tool_4_test_fixture ( # pyright: ignore[reportIncompatibleMethodOverride]
9791 self ,
98- test_server_fixture : TestServerFixture ,
92+ auto_server_fixture : AutoServerFixture ,
9993 ) -> Tool4TestFixture :
10094 """目标服务器请参考`tests.app.echo_ws_app.get_app`."""
10195 echo_ws_test_model = get_ws_test_app ()
10296 echo_ws_app = echo_ws_test_model .app
10397 echo_ws_get_request = echo_ws_test_model .get_request
10498
105- target_ws_server = await test_server_fixture (
99+ target_ws_server = await auto_server_fixture (
106100 app = echo_ws_app , port = DEFAULT_PORT , host = DEFAULT_HOST
107101 )
108102
109103 target_server_base_url = str (target_ws_server .contx_socket_url )
110104
111- client_for_conn_to_target_server = httpx .AsyncClient (proxies = NO_PROXIES )
105+ client_for_conn_to_target_server = httpx .AsyncClient (mounts = NO_PROXIES )
112106
113107 reverse_ws_app = get_reverse_ws_app (
114108 client = client_for_conn_to_target_server , base_url = target_server_base_url
115109 )
116110
117- proxy_ws_server = await test_server_fixture (
111+ proxy_ws_server = await auto_server_fixture (
118112 app = reverse_ws_app , port = DEFAULT_PORT , host = DEFAULT_HOST
119113 )
120114
121115 proxy_server_base_url = str (proxy_ws_server .contx_socket_url )
122116
123- client_for_conn_to_proxy_server = httpx .AsyncClient (proxies = NO_PROXIES )
117+ client_for_conn_to_proxy_server = httpx .AsyncClient (mounts = NO_PROXIES )
124118
125119 return Tool4TestFixture (
126120 client_for_conn_to_target_server = client_for_conn_to_target_server ,
@@ -189,18 +183,11 @@ async def test_ws_proxy(self, tool_4_test_fixture: Tool4TestFixture) -> None:
189183 # 是因为这里已经有现成的target server,放在这里测试可以节省启动服务器时间
190184
191185 aconnect_ws_subprocess_queue : "Queue[str]" = Queue ()
192-
193- kwargs_async_client = {"proxies" : NO_PROXIES }
194- kwargs_aconnect_ws = {"url" : proxy_server_base_url + "do_nothing" }
195- kwargs = {
196- "kwargs_async_client" : kwargs_async_client ,
197- "kwargs_aconnect_ws" : kwargs_aconnect_ws ,
198- }
186+ aconnect_ws_url = proxy_server_base_url + "do_nothing"
199187
200188 aconnect_ws_subprocess = Process (
201189 target = _subprocess_run_httpx_ws ,
202- args = (aconnect_ws_subprocess_queue ,),
203- kwargs = kwargs ,
190+ args = (aconnect_ws_subprocess_queue , aconnect_ws_url ),
204191 )
205192 aconnect_ws_subprocess .start ()
206193
@@ -246,13 +233,13 @@ async def test_target_server_shutdown_abnormally(self) -> None:
246233 await anyio .sleep (0.1 )
247234 target_server_base_url = subprocess_queue .get ()
248235
249- client_for_conn_to_target_server = httpx .AsyncClient (proxies = NO_PROXIES )
236+ client_for_conn_to_target_server = httpx .AsyncClient (mounts = NO_PROXIES )
250237
251238 reverse_ws_app = get_reverse_ws_app (
252239 client = client_for_conn_to_target_server , base_url = target_server_base_url
253240 )
254241
255- async with TestServer (
242+ async with AutoServer (
256243 app = reverse_ws_app ,
257244 port = DEFAULT_PORT ,
258245 host = DEFAULT_HOST ,
@@ -261,10 +248,10 @@ async def test_target_server_shutdown_abnormally(self) -> None:
261248
262249 async with aconnect_ws (
263250 proxy_server_base_url + "do_nothing" ,
264- httpx .AsyncClient (proxies = NO_PROXIES ),
251+ httpx .AsyncClient (mounts = NO_PROXIES ),
265252 ) as ws0 , aconnect_ws (
266253 proxy_server_base_url + "do_nothing" ,
267- httpx .AsyncClient (proxies = NO_PROXIES ),
254+ httpx .AsyncClient (mounts = NO_PROXIES ),
268255 ) as ws1 :
269256 # force shutdown target server
270257 target_ws_server_subprocess .terminate ()
0 commit comments