예제 #1
0
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
예제 #2
0
 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
예제 #3
0
 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()
예제 #4
0
 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()
예제 #5
0
 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()
예제 #6
0
 async def asgi(receive, send):
     websocket = WebSocket(scope, receive=receive, send=send)
     await websocket.accept()
     await websocket.close()
     await websocket.close()
예제 #7
0
 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()
예제 #8
0
 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()
예제 #9
0
 async def asgi(receive, send):
     websocket = WebSocket(scope, receive=receive, send=send)
     await websocket.close(status.WS_1001_GOING_AWAY)
예제 #10
0
 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})
예제 #11
0
 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)
예제 #12
0
 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()
예제 #13
0
 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()
예제 #14
0
 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()
예제 #15
0
 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()
예제 #16
0
 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()
예제 #17
0
 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})