Exemplo n.º 1
0
async def test_closure(stream: WSStream) -> None:
    assert not stream.closed
    await stream.handle(StreamClosed(stream_id=1))
    assert stream.closed
    await stream.handle(StreamClosed(stream_id=1))
    assert stream.closed
    # It is important that the disconnect message has only been sent
    # once.
    assert stream.app_put.call_args_list == [call({"type": "websocket.disconnect"})]
Exemplo n.º 2
0
async def test_send_response(stream: HTTPStream) -> None:
    await stream.handle(
        Request(stream_id=1,
                http_version="2",
                headers=[],
                raw_path=b"/?a=b",
                method="GET"))
    await stream.app_send(
        cast(HTTPResponseStartEvent, {
            "type": "http.response.start",
            "status": 200,
            "headers": []
        }))
    assert stream.state == ASGIHTTPState.REQUEST
    # Must wait for response before sending anything
    stream.send.assert_not_called()  # type: ignore
    await stream.app_send(
        cast(HTTPResponseBodyEvent, {
            "type": "http.response.body",
            "body": b"Body"
        }))
    assert stream.state == ASGIHTTPState.CLOSED
    stream.send.assert_called()  # type: ignore
    assert stream.send.call_args_list == [  # type: ignore
        call(Response(stream_id=1, headers=[], status_code=200)),
        call(Body(stream_id=1, data=b"Body")),
        call(EndBody(stream_id=1)),
        call(StreamClosed(stream_id=1)),
    ]
    stream.config._log.access.assert_called()  # type: ignore
Exemplo n.º 3
0
async def test_handle_closed(stream: WSStream) -> None:
    await stream.handle(StreamClosed(stream_id=1))
    stream.app_put.assert_called()  # type: ignore
    assert stream.app_put.call_args_list == [  # type: ignore
        call({
            "type": "websocket.disconnect",
            "code": 1006
        })
    ]
Exemplo n.º 4
0
async def test_protocol_handle_data_post_close(protocol: H11Protocol) -> None:
    await protocol.handle(
        RawData(
            data=
            b"POST / HTTP/1.1\r\nHost: hypercorn\r\nContent-Length: 10\r\n\r\n"
        ))
    await protocol.stream_send(StreamClosed(stream_id=1))
    assert protocol.stream is None
    # Key is that this doesn't error
    await protocol.handle(RawData(data=b"abcdefghij"))
Exemplo n.º 5
0
async def test_protocol_instant_recycle(protocol: H11Protocol) -> None:
    data = b"GET / HTTP/1.1\r\nHost: hypercorn\r\n\r\n"
    await protocol.handle(RawData(data=data))
    assert protocol.stream is not None
    await protocol.stream_send(Response(stream_id=1, status_code=200, headers=[]))
    await protocol.stream_send(EndBody(stream_id=1))
    await protocol.handle(RawData(data=data))
    await protocol.stream_send(StreamClosed(stream_id=1))
    # Should have recycled, i.e. a stream should exist
    assert protocol.stream is not None
Exemplo n.º 6
0
async def test_protocol_send_stream_closed(keep_alive: bool, expected: Any,
                                           protocol: H11Protocol) -> None:
    data = b"GET / HTTP/1.1\r\nHost: hypercorn\r\n"
    if keep_alive:
        data += b"\r\n"
    else:
        data += b"Connection: close\r\n\r\n"
    await protocol.handle(RawData(data=data))
    await protocol.stream_send(
        Response(stream_id=1, status_code=200, headers=[]))
    await protocol.stream_send(EndBody(stream_id=1))
    await protocol.stream_send(StreamClosed(stream_id=1))
    protocol.send.assert_called()
    assert protocol.send.call_args_list[2] == call(expected)
Exemplo n.º 7
0
async def test_protocol_instant_recycle(protocol: H11Protocol) -> None:
    data = b"GET / HTTP/1.1\r\nHost: hypercorn\r\n\r\n"
    # This test requires a real event as the handling should pause on
    # the instant receipt
    protocol.can_read = EventWrapper()
    await protocol.handle(RawData(data=data))
    assert protocol.stream is not None
    await protocol.stream_send(
        Response(stream_id=1, status_code=200, headers=[]))
    await protocol.stream_send(EndBody(stream_id=1))
    task = asyncio.ensure_future(protocol.handle(RawData(data=data)))
    await asyncio.sleep(0)  # Switch to task
    await protocol.stream_send(StreamClosed(stream_id=1))
    # Should have recycled, i.e. a stream should exist
    assert protocol.stream is not None
    await asyncio.sleep(0)  # Switch to task
    assert task.done()
Exemplo n.º 8
0
async def test_send_app_error(stream: HTTPStream) -> None:
    await stream.handle(
        Request(stream_id=1, http_version="2", headers=[], raw_path=b"/?a=b", method="GET")
    )
    await stream.app_send(None)
    stream.send.assert_called()  # type: ignore
    assert stream.send.call_args_list == [  # type: ignore
        call(
            Response(
                stream_id=1,
                headers=[(b"content-length", b"0"), (b"connection", b"close")],
                status_code=500,
            )
        ),
        call(EndBody(stream_id=1)),
        call(StreamClosed(stream_id=1)),
    ]
    stream.config._log.access.assert_called()  # type: ignore
Exemplo n.º 9
0
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()
Exemplo n.º 10
0
async def test_protocol_handle_closed(protocol: H11Protocol) -> None:
    await protocol.handle(
        RawData(
            data=
            b"GET / HTTP/1.1\r\nHost: hypercorn\r\nConnection: close\r\n\r\n"))
    stream = protocol.stream
    await protocol.handle(Closed())
    stream.handle.assert_called()
    assert stream.handle.call_args_list == [
        call(
            Request(
                stream_id=1,
                headers=[(b"host", b"hypercorn"), (b"connection", b"close")],
                http_version="1.1",
                method="GET",
                raw_path=b"/",
            )),
        call(EndBody(stream_id=1)),
        call(StreamClosed(stream_id=1)),
    ]
Exemplo n.º 11
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))
Exemplo n.º 12
0
async def test_send_app_error_handshake(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(None)
    stream.send.assert_called()
    assert stream.send.call_args_list == [
        call(
            Response(
                stream_id=1,
                headers=[(b"content-length", b"0"), (b"connection", b"close")],
                status_code=500,
            )),
        call(EndBody(stream_id=1)),
        call(StreamClosed(stream_id=1)),
    ]
    stream.config._log.access.assert_called()
Exemplo n.º 13
0
async def test_protocol_instant_recycle(
        protocol: H11Protocol, event_loop: asyncio.AbstractEventLoop) -> None:
    # This test task acts as the asgi app, spawned tasks act as the
    # server.
    data = b"GET / HTTP/1.1\r\nHost: hypercorn\r\n\r\n"
    # This test requires a real event as the handling should pause on
    # the instant receipt
    protocol.can_read = EventWrapper()
    task = event_loop.create_task(protocol.handle(RawData(data=data)))
    await asyncio.sleep(0)  # Switch to task
    assert protocol.stream is not None
    assert task.done()
    await protocol.stream_send(
        Response(stream_id=1, status_code=200, headers=[]))
    await protocol.stream_send(EndBody(stream_id=1))
    task = event_loop.create_task(protocol.handle(RawData(data=data)))
    await asyncio.sleep(0)  # Switch to task
    await protocol.stream_send(StreamClosed(stream_id=1))
    await asyncio.sleep(0)  # Switch to task
    # Should have recycled, i.e. a stream should exist
    assert protocol.stream is not None
    assert task.done()
Exemplo n.º 14
0
async def test_handle_closed(stream: HTTPStream) -> None:
    await stream.handle(StreamClosed(stream_id=1))
    stream.app_put.assert_called()  # type: ignore
    assert stream.app_put.call_args_list == [call({"type": "http.disconnect"})]  # type: ignore