3
3
4
4
from contextlib import AsyncExitStack
5
5
from multiprocessing import Process , Queue
6
- from typing import Any , Dict , Optional
6
+ from typing import Any , Dict
7
7
8
8
import anyio
9
9
import httpx
15
15
from typing_extensions import override
16
16
17
17
from .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
20
20
from .tool import (
21
21
AbstractTestProxy ,
22
22
Tool4TestFixture ,
25
25
DEFAULT_HOST = "127.0.0.1"
26
26
DEFAULT_PORT = 0 # random port
27
27
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
29
30
NO_PROXIES : Dict [Any , Any ] = {"all://" : None }
30
31
31
32
@@ -36,7 +37,7 @@ def _subprocess_run_echo_ws_server(queue: "Queue[str]"):
36
37
queue: The queue for subprocess to put the url of echo ws app.
37
38
After the server is started, the url will be put into the queue.
38
39
"""
39
- target_ws_server = TestServer (
40
+ target_ws_server = AutoServer (
40
41
app = get_ws_test_app ().app ,
41
42
host = DEFAULT_HOST ,
42
43
port = DEFAULT_PORT ,
@@ -55,29 +56,22 @@ async def run():
55
56
56
57
def _subprocess_run_httpx_ws (
57
58
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 ,
60
60
):
61
61
"""Run aconnect_ws in subprocess.
62
62
63
63
Args:
64
64
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.
67
66
"""
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 )
73
67
74
68
async def run ():
75
69
_exit_stack = AsyncExitStack ()
76
- _temp_client = httpx .AsyncClient (proxies = NO_PROXIES , ** kwargs_async_client )
70
+ _temp_client = httpx .AsyncClient (mounts = NO_PROXIES )
77
71
_ = await _exit_stack .enter_async_context (
78
72
aconnect_ws (
79
73
client = _temp_client ,
80
- ** kwargs_aconnect_ws ,
74
+ url = aconnect_ws_url ,
81
75
)
82
76
)
83
77
queue .put ("done" )
@@ -95,32 +89,32 @@ class TestReverseWsProxy(AbstractTestProxy):
95
89
@pytest .fixture ()
96
90
async def tool_4_test_fixture ( # pyright: ignore[reportIncompatibleMethodOverride]
97
91
self ,
98
- test_server_fixture : TestServerFixture ,
92
+ auto_server_fixture : AutoServerFixture ,
99
93
) -> Tool4TestFixture :
100
94
"""目标服务器请参考`tests.app.echo_ws_app.get_app`."""
101
95
echo_ws_test_model = get_ws_test_app ()
102
96
echo_ws_app = echo_ws_test_model .app
103
97
echo_ws_get_request = echo_ws_test_model .get_request
104
98
105
- target_ws_server = await test_server_fixture (
99
+ target_ws_server = await auto_server_fixture (
106
100
app = echo_ws_app , port = DEFAULT_PORT , host = DEFAULT_HOST
107
101
)
108
102
109
103
target_server_base_url = str (target_ws_server .contx_socket_url )
110
104
111
- client_for_conn_to_target_server = httpx .AsyncClient (proxies = NO_PROXIES )
105
+ client_for_conn_to_target_server = httpx .AsyncClient (mounts = NO_PROXIES )
112
106
113
107
reverse_ws_app = get_reverse_ws_app (
114
108
client = client_for_conn_to_target_server , base_url = target_server_base_url
115
109
)
116
110
117
- proxy_ws_server = await test_server_fixture (
111
+ proxy_ws_server = await auto_server_fixture (
118
112
app = reverse_ws_app , port = DEFAULT_PORT , host = DEFAULT_HOST
119
113
)
120
114
121
115
proxy_server_base_url = str (proxy_ws_server .contx_socket_url )
122
116
123
- client_for_conn_to_proxy_server = httpx .AsyncClient (proxies = NO_PROXIES )
117
+ client_for_conn_to_proxy_server = httpx .AsyncClient (mounts = NO_PROXIES )
124
118
125
119
return Tool4TestFixture (
126
120
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:
189
183
# 是因为这里已经有现成的target server,放在这里测试可以节省启动服务器时间
190
184
191
185
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"
199
187
200
188
aconnect_ws_subprocess = Process (
201
189
target = _subprocess_run_httpx_ws ,
202
- args = (aconnect_ws_subprocess_queue ,),
203
- kwargs = kwargs ,
190
+ args = (aconnect_ws_subprocess_queue , aconnect_ws_url ),
204
191
)
205
192
aconnect_ws_subprocess .start ()
206
193
@@ -246,13 +233,13 @@ async def test_target_server_shutdown_abnormally(self) -> None:
246
233
await anyio .sleep (0.1 )
247
234
target_server_base_url = subprocess_queue .get ()
248
235
249
- client_for_conn_to_target_server = httpx .AsyncClient (proxies = NO_PROXIES )
236
+ client_for_conn_to_target_server = httpx .AsyncClient (mounts = NO_PROXIES )
250
237
251
238
reverse_ws_app = get_reverse_ws_app (
252
239
client = client_for_conn_to_target_server , base_url = target_server_base_url
253
240
)
254
241
255
- async with TestServer (
242
+ async with AutoServer (
256
243
app = reverse_ws_app ,
257
244
port = DEFAULT_PORT ,
258
245
host = DEFAULT_HOST ,
@@ -261,10 +248,10 @@ async def test_target_server_shutdown_abnormally(self) -> None:
261
248
262
249
async with aconnect_ws (
263
250
proxy_server_base_url + "do_nothing" ,
264
- httpx .AsyncClient (proxies = NO_PROXIES ),
251
+ httpx .AsyncClient (mounts = NO_PROXIES ),
265
252
) as ws0 , aconnect_ws (
266
253
proxy_server_base_url + "do_nothing" ,
267
- httpx .AsyncClient (proxies = NO_PROXIES ),
254
+ httpx .AsyncClient (mounts = NO_PROXIES ),
268
255
) as ws1 :
269
256
# force shutdown target server
270
257
target_ws_server_subprocess .terminate ()
0 commit comments