Skip to content

Commit b565bbf

Browse files
committed
update websocket test for websockets 14
websockets tests now require Python 3.9 to avoid dealing with the API change
1 parent 05e737e commit b565bbf

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

tests/dummy_http_server.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,37 @@
1313
import websockets
1414

1515

16-
async def process_request(path, request_headers, port):
17-
if path.endswith("/ws"):
16+
async def process_request(connection, request, port):
17+
if request.path.endswith("/ws"):
1818
return None
1919
headers = {
2020
"Content-Type": "text/plain",
21-
"Host": request_headers.get("Host", "None"),
22-
"Origin": request_headers.get("Origin", "None"),
21+
"Host": request.headers.get("Host", "None"),
22+
"Origin": request.headers.get("Origin", "None"),
2323
}
24-
return (HTTPStatus.OK, headers, str(port).encode("utf8"))
24+
return connection.respond(HTTPStatus.OK, headers, str(port).encode("utf8"))
2525

2626

27-
async def send_port(websocket, path):
28-
await websocket.send(str(websocket.port))
27+
async def send_port(websocket):
28+
_ip, port = websocket.local_address
29+
await websocket.send(str(port))
2930

3031

31-
async def main(port):
32+
async def main(port, *, _start_future=None, _stop_future=None):
33+
# allow signaling a stop (in tests)
34+
if _stop_future is None:
35+
_stop_future = asyncio.Future()
36+
3237
async with websockets.serve(
3338
send_port,
3439
host="127.0.0.1",
3540
port=port,
3641
process_request=partial(process_request, port=port),
3742
):
43+
if _start_future:
44+
_start_future.set_result(None)
3845
# wait forever
39-
await asyncio.Future()
46+
await _stop_future
4047

4148

4249
if __name__ == "__main__":

tests/test_dummy_http_server.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import asyncio
2+
import sys
3+
4+
import pytest
5+
import websockets
6+
7+
from .dummy_http_server import main
8+
9+
10+
# quick test that the dummy http server for tests works!
11+
@pytest.mark.skipif(sys.version_info < (3, 9), reason="websockets require Python 3.9")
12+
async def test_dummy_server():
13+
port = 5678
14+
start_future = asyncio.Future()
15+
stop_future = asyncio.Future()
16+
req_url = f"ws://127.0.0.1:{port}/ws"
17+
main_future = asyncio.ensure_future(
18+
main(port, _start_future=start_future, _stop_future=stop_future)
19+
)
20+
await asyncio.wait(
21+
[start_future, main_future], timeout=5, return_when=asyncio.FIRST_COMPLETED
22+
)
23+
if main_future.done():
24+
main_future.result()
25+
async with websockets.connect(req_url) as websocket:
26+
ws_port = await websocket.recv()
27+
assert ws_port == str(port)
28+
stop_future.cancel()
29+
try:
30+
await main_future
31+
except asyncio.CancelledError:
32+
pass

tests/test_proxy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ async def test_check_routes(proxy, username):
499499
assert_equal(before, after)
500500

501501

502+
@pytest.mark.skipif(sys.version_info < (3, 9), reason="websockets require Python 3.9")
502503
async def test_websockets(proxy, launch_backends):
503504
routespec = "/user/username/"
504505
data = {}

0 commit comments

Comments
 (0)