async def _stream() -> WSStream:
    stream = WSStream(AsyncMock(), Config(), AsyncMock(), False, None, None,
                      AsyncMock(), 1)
    stream.context.spawn_app.return_value = AsyncMock()  # type: ignore
    stream.app_put = AsyncMock()
    stream.config._log = AsyncMock(spec=Logger)
    return stream
Esempio n. 2
0
async def _stream() -> WSStream:
    stream = WSStream(Config(), False, None, None, CoroutineMock(),
                      CoroutineMock(), 1)
    stream.spawn_app.return_value = CoroutineMock()
    stream.app_put = CoroutineMock()
    stream.config._log = AsyncMock(spec=Logger)
    return stream
Esempio n. 3
0
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"})
    ]
Esempio n. 4
0
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))