MCPcopy Create free account
hub / github.com/encode/starlette / test_websocket_endpoint_on_receive_json

Function test_websocket_endpoint_on_receive_json

tests/test_endpoints.py:109–126  ·  view source on GitHub ↗
(
    test_client_factory: TestClientFactory,
)

Source from the content-addressed store, hash-verified

107
108
109def test_websocket_endpoint_on_receive_json(
110 test_client_factory: TestClientFactory,
111) -> None:
112 class WebSocketApp(WebSocketEndpoint):
113 encoding = "json"
114
115 async def on_receive(self, websocket: WebSocket, data: str) -> None:
116 await websocket.send_json({"message": data})
117
118 client = test_client_factory(WebSocketApp)
119 with client.websocket_connect("/ws") as websocket:
120 websocket.send_json({"hello": "world"})
121 data = websocket.receive_json()
122 assert data == {"message": {"hello": "world"}}
123
124 with pytest.raises(RuntimeError):
125 with client.websocket_connect("/ws") as websocket:
126 websocket.send_text("Hello world")
127
128
129def test_websocket_endpoint_on_receive_json_binary(

Callers

nothing calls this directly

Calls 5

test_client_factoryFunction · 0.85
websocket_connectMethod · 0.80
send_jsonMethod · 0.45
receive_jsonMethod · 0.45
send_textMethod · 0.45

Tested by

no test coverage detected