def create_websocket_connection( self, send: ASGISend, receive: ASGIReceive ) -> WebSocketConnection: self._websocket_connection = WebSocketConnection( send, receive, self.scope.get("subprotocols", []) ) return self._websocket_connection
async def test_websocket_receive(send, receive, message_stack): msg = {"text": "hello", "type": "websocket.receive"} message_stack.append(msg) ws = WebSocketConnection(send, receive) text = await ws.receive() assert text == msg["text"]
async def test_websocket_accept_with_no_subprotocols(send, receive, message_stack): ws = WebSocketConnection(send, receive) await ws.accept() assert len(message_stack) == 1 message = message_stack.popleft() assert message["type"] == "websocket.accept" assert message["subprotocol"] == "" assert "bytes" not in message
async def test_websocket_accept_with_subprotocol(send, receive, message_stack): subprotocols = ["graphql-ws"] ws = WebSocketConnection(send, receive, subprotocols) await ws.accept(subprotocols) assert len(message_stack) == 1 message = message_stack.popleft() assert message["type"] == "websocket.accept" assert message["subprotocol"] == "graphql-ws" assert "bytes" not in message
async def test_websocket_send(send, receive, message_stack): text_string = "hello" text_bytes = b"hello" ws = WebSocketConnection(send, receive) await ws.send(text_string) await ws.send(text_bytes) assert len(message_stack) == 2 message = message_stack.popleft() assert message["type"] == "websocket.send" assert message["text"] == text_string assert "bytes" not in message message = message_stack.popleft() assert message["type"] == "websocket.send" assert message["bytes"] == text_bytes assert "text" not in message
def create_websocket_connection( self, send: ASGISend, receive: ASGIReceive ) -> WebSocketConnection: self._websocket_connection = WebSocketConnection(send, receive) return self._websocket_connection