MCPcopy
hub / github.com/encode/starlette / test_websocket_endpoint_on_receive_json

Function test_websocket_endpoint_on_receive_json

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

Source from the content-addressed store, hash-verified

96
97
98def test_websocket_endpoint_on_receive_json(
99 test_client_factory: TestClientFactory,
100) -> None:
101 class WebSocketApp(WebSocketEndpoint):
102 encoding = "json"
103
104 async def on_receive(self, websocket: WebSocket, data: str) -> None:
105 await websocket.send_json({"message": data})
106
107 client = test_client_factory(WebSocketApp)
108 with client.websocket_connect("/ws") as websocket:
109 websocket.send_json({"hello": "world"})
110 data = websocket.receive_json()
111 assert data == {"message": {"hello": "world"}}
112
113 with pytest.raises(RuntimeError):
114 with client.websocket_connect("/ws") as websocket:
115 websocket.send_text("Hello world")
116
117
118def 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