async def test_ws_handler_default_will_accept_every_connect(): sended_messages = await run_ws_handler_with_messages( websocket(close_immediatly), [{ "type": "websocket.connect" }], scope_type="websocket") assert sended_messages == [ call({"type": "websocket.accept"}), call(ws_close()) ]
async def test_ws_handler_can_have_custom_on_conenct(): async def never_accept_connect(scope): return ws_close() sended_messages = await run_ws_handler_with_messages( websocket(close_immediatly, on_connect=never_accept_connect), [{ "type": "websocket.connect" }], scope_type="websocket") assert sended_messages == [call(ws_close())]
async def test_ws_handler_can_be_non_generators(): async def ws_just_return_first_message(scope, receiver): async for msg in receiver: return ws_receive(msg) async def ws_return_nothing(scope, receiver): pass sender = Mock().send async_sender = awaitable_mock(sender) fake_messages = list(map(ws_receive, ["a", b"", "1", b"2"])) receiver = receive_func_from_coll(fake_messages) await _ws_async_generator_client(ws_just_return_first_message, {}, tuple(), receiver, async_sender) assert sender.mock_calls == [call(fake_messages[0]), call(ws_close())] sender.reset_mock() await _ws_async_generator_client(ws_return_nothing, {}, tuple(), receiver, async_sender) assert sender.mock_calls == [call(ws_close())]
async def never_accept_connect(scope): return ws_close()
async def close_immediatly(scope, receive): yield ws_close()
async def ws_close_actively(scope, receiver): yield ws_close(2000)