| 1 |
from unittest.mock import AsyncMock |
| 2 |
|
| 3 |
import pytest |
| 4 |
import trio |
| 5 |
from hypercorn.typing import WebsocketScope |
| 6 |
|
| 7 |
from quart_trio.app import QuartTrio |
| 8 |
from quart_trio.asgi import TrioASGIWebsocketConnection |
| 9 |
|
| 10 |
|
| 11 |
@pytest.mark.trio |
| 12 |
async def test_websocket_complete_on_disconnect() -> None: |
| 13 |
scope: WebsocketScope = { |
| 14 |
"type": "websocket", |
| 15 |
"asgi": {}, |
| 16 |
"http_version": "1.1", |
| 17 |
"scheme": "wss", |
| 18 |
"path": "ws://quart/path", |
| 19 |
"raw_path": b"/", |
| 20 |
"query_string": b"", |
| 21 |
"root_path": "", |
| 22 |
"headers": [(b"host", b"quart")], |
| 23 |
"client": ("127.0.0.1", 80), |
| 24 |
"server": None, |
| 25 |
"subprotocols": [], |
| 26 |
"extensions": {"websocket.http.response": {}}, |
| 27 |
"state": {}, # type: ignore[typeddict-item] |
| 28 |
} |
| 29 |
connection = TrioASGIWebsocketConnection(QuartTrio(__name__), scope) |
| 30 |
websocket = connection._create_websocket_from_scope(AsyncMock()) |
| 31 |
send_channel, receive_channel = trio.open_memory_channel[dict](0) |
| 32 |
async with trio.open_nursery() as nursery: |
| 33 |
nursery.start_soon( |
| 34 |
connection.handle_messages, nursery, websocket, receive_channel.receive # type: ignore |
| 35 |
) |
| 36 |
await send_channel.send({"type": "websocket.disconnect"}) |
| 37 |
await trio.sleep(1) # Simulate doing something else |
| 38 |
assert nursery.cancel_scope.cancelled_caught |