def test_websocket_scope_interface(): """ A WebSocket can be instantiated with a scope, and presents a `Mapping` interface. """ async def mock_receive(): pass # pragma: no cover async def mock_send(message): pass # pragma: no cover websocket = WebSocket( { "type": "websocket", "path": "/abc/", "headers": [] }, receive=mock_receive, send=mock_send, ) assert websocket["type"] == "websocket" assert dict(websocket) == { "type": "websocket", "path": "/abc/", "headers": [] } assert len(websocket) == 3
async def asgi(receive, send): nonlocal close_code websocket = WebSocket(scope, receive=receive, send=send) await websocket.accept() try: await websocket.receive_text() except WebSocketDisconnect as exc: close_code = exc.code
async def asgi(receive, send): websocket = WebSocket(scope, receive=receive, send=send) queue = asyncio.Queue() await websocket.accept() await run_until_first_complete( (reader, {"websocket": websocket, "queue": queue}), (writer, {"websocket": websocket, "queue": queue}), ) await websocket.close()
async def asgi(receive, send): websocket = WebSocket(scope, receive=receive, send=send) query_params = dict(websocket.query_params) await websocket.accept() await websocket.send_json({"params": query_params}) await websocket.close()
async def asgi(receive, send): websocket = WebSocket(scope, receive=receive, send=send) await websocket.accept() message = await websocket.receive() assert message["type"] == "websocket.disconnect" message = await websocket.receive()
async def asgi(receive, send): websocket = WebSocket(scope, receive=receive, send=send) await websocket.accept() await websocket.close() await websocket.close()
async def asgi(receive, send): websocket = WebSocket(scope, receive=receive, send=send) assert websocket["subprotocols"] == ["soap", "wamp"] await websocket.accept(subprotocol="wamp") await websocket.close()
async def asgi(receive, send): websocket = WebSocket(scope, receive=receive, send=send) await websocket.accept() message = await websocket.receive_json(mode="binary") await websocket.send_json(message, mode="binary") await websocket.close()
async def asgi(receive, send): websocket = WebSocket(scope, receive=receive, send=send) await websocket.close(status.WS_1001_GOING_AWAY)
async def asgi(receive, send): websocket = WebSocket(scope, receive=receive, send=send) await websocket.accept() async for data in websocket.iter_json(): await websocket.send_json({"message": data})
async def asgi(receive, send): websocket = WebSocket(scope, receive=receive, send=send) await websocket.accept() async for data in websocket.iter_bytes(): await websocket.send_bytes(b"Message was: " + data)
async def asgi(receive, send): websocket = WebSocket(scope, receive=receive, send=send) await websocket.accept() data = await websocket.receive_json() await websocket.send_json({"message": data}) await websocket.close()
async def asgi(receive, send): websocket = WebSocket(scope, receive=receive, send=send) await websocket.accept() await websocket.send_json({"url": str(websocket.url)}) await websocket.close()
async def asgi(receive, send): websocket = WebSocket(scope, receive=receive, send=send) await websocket.accept() data = await websocket.receive_bytes() await websocket.send_bytes(b"Message was: " + data) await websocket.close()
async def asgi(receive, send): websocket = WebSocket(scope, receive=receive, send=send) headers = dict(websocket.headers) await websocket.accept() await websocket.send_json({"headers": headers}) await websocket.close()
async def asgi(receive, send): websocket = WebSocket(scope, receive=receive, send=send) await websocket.accept() await websocket.send_json({"port": websocket.url.port}) await websocket.close()
async def asgi(receive, send): websocket = WebSocket(scope, receive=receive, send=send) await websocket.accept() await websocket.close() await websocket.send({"type": "websocket.close", "code": 1000})