示例#1
0
async def test_protocol_send_body(protocol: H11Protocol) -> None:
    await protocol.handle(
        RawData(
            data=
            b"GET / HTTP/1.1\r\nHost: hypercorn\r\nConnection: close\r\n\r\n"))
    await protocol.stream_send(
        Response(stream_id=1,
                 status_code=200,
                 headers=[(b"content-length", b"5")]))
    await protocol.stream_send(Body(stream_id=1, data=b"hello"))
    protocol.send.assert_called()  # type: ignore
    assert protocol.send.call_args_list == [  # type: ignore
        call(Updated(idle=False)),
        call(
            RawData(
                data=
                b"HTTP/1.1 200 \r\ncontent-length: 5\r\ndate: Thu, 01 Jan 1970 01:23:20 GMT\r\nserver: hypercorn-h11\r\nConnection: close\r\n\r\n"  # noqa: E501
            )),
        call(RawData(data=b"hello")),
    ]
示例#2
0
                 status_code=200,
                 headers=[(b"content-length", b"5")]))
    await protocol.stream_send(Body(stream_id=1, data=b"hello"))
    protocol.send.assert_called()
    assert protocol.send.call_args_list == [
        call(
            RawData(
                data=
                b"HTTP/1.1 200 \r\ncontent-length: 5\r\ndate: Thu, 01 Jan 1970 01:23:20 GMT\r\nserver: hypercorn-h11\r\nconnection: close\r\n\r\n"  # noqa: E501
            )),
        call(RawData(data=b"hello")),
    ]


@pytest.mark.asyncio
@pytest.mark.parametrize("keep_alive, expected", [(True, Updated()),
                                                  (False, Closed())])
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)