| 1 |
from typing import Any |
| 2 |
|
| 3 |
from litestar import WebSocket, websocket |
| 4 |
from litestar.datastructures import State |
| 5 |
from litestar.testing import create_test_client |
| 6 |
|
| 7 |
|
| 8 |
def test_websocket() -> None: |
| 9 |
@websocket(path="/ws") |
| 10 |
async def websocket_handler(socket: WebSocket[Any, Any, State]) -> None: |
| 11 |
await socket.accept() |
| 12 |
recv = await socket.receive_json() |
| 13 |
await socket.send_json({"message": recv}) |
| 14 |
await socket.close() |
| 15 |
|
| 16 |
with create_test_client(route_handlers=[websocket_handler]).websocket_connect("/ws") as ws: |
| 17 |
ws.send_json({"hello": "world"}) |
| 18 |
data = ws.receive_json() |
| 19 |
assert data == {"message": {"hello": "world"}} |