async def test_send_connection(stream: WSStream) -> None: await stream.handle( Request( stream_id=1, http_version="2", headers=[(b"sec-websocket-version", b"13")], raw_path=b"/", method="GET", )) await stream.app_send( cast(WebsocketAcceptEvent, {"type": "websocket.accept"})) await stream.app_send( cast(WebsocketSendEvent, { "type": "websocket.send", "text": "hello" })) await stream.app_send( cast(WebsocketCloseEvent, {"type": "websocket.close"})) stream.send.assert_called() # type: ignore assert stream.send.call_args_list == [ # type: ignore call(Response(stream_id=1, headers=[], status_code=200)), call(Data(stream_id=1, data=b"\x81\x05hello")), call(Data(stream_id=1, data=b"\x88\x02\x03\xe8")), call(EndData(stream_id=1)), ]
async def test_pings(stream: WSStream, event_loop: asyncio.AbstractEventLoop) -> None: stream.config.websocket_ping_interval = 0.1 await stream.handle( Request( stream_id=1, http_version="2", headers=[(b"sec-websocket-version", b"13")], raw_path=b"/?a=b", method="GET", ) ) async with TaskGroup(event_loop) as task_group: stream.context = Context(task_group) await stream.app_send({"type": "websocket.accept"}) stream.app_put = AsyncMock() await asyncio.sleep(0.15) assert stream.send.call_args_list == [ call(Response(stream_id=1, headers=[], status_code=200)), call(Data(stream_id=1, data=b"\x89\x00")), call(Data(stream_id=1, data=b"\x89\x00")), ] await stream.handle(StreamClosed(stream_id=1))
async def test_handle_connection(stream: WSStream) -> None: await stream.handle( Request( stream_id=1, http_version="2.0", headers=[(b"sec-websocket-version", b"13")], raw_path=b"/?a=b", method="GET", ) ) await stream.app_send({"type": "websocket.accept"}) stream.app_put = CoroutineMock() await stream.handle(Data(stream_id=1, data=b"\x81\x85&`\x13\x0eN\x05\x7fbI")) stream.app_put.assert_called() assert stream.app_put.call_args_list == [ call({"type": "websocket.receive", "bytes": None, "text": "hello"}) ]
async def test_send_app_error_connected(stream: WSStream) -> None: await stream.handle( Request( stream_id=1, http_version="2", headers=[(b"sec-websocket-version", b"13")], raw_path=b"/", method="GET", )) await stream.app_send({"type": "websocket.accept"}) await stream.app_send(None) stream.send.assert_called() assert stream.send.call_args_list == [ call(Response(stream_id=1, headers=[], status_code=200)), call(Data(stream_id=1, data=b"\x88\x02\x03\xe8")), call(StreamClosed(stream_id=1)), ] stream.config._log.access.assert_called()
async def test_protocol_send_data(protocol: H11Protocol) -> None: await protocol.stream_send(Data(stream_id=1, data=b"hello")) protocol.send.assert_called() assert protocol.send.call_args_list == [call(RawData(data=b"hello"))]