Ejemplo n.º 1
0
async def test_connection_handle_expect_100_continue_and_1xx(
    connection: ClientConnection,
):
    # Arrange
    connection.open = True
    connection.headers = get_example_headers()
    connection.parser = FakeParser(100)
    connection.transport = FakeTransport()

    request = Request(
        "POST",
        b"/",
        headers=[(b"content-type", b"application/json"), (b"expect", b"100-continue")],
    ).with_content(JSONContent({"id": "1", "name": "foo"}))

    # Arrange future response...
    connection.headers = get_example_headers()
    connection.parser = FakeParser(100)
    # trick: simulate a complete response before the request is sent;
    # here is legit because we are simulating a proper scenario
    connection.on_headers_complete()

    try:
        await asyncio.wait_for(connection.send(request), 0.01)
    except TimeoutError:
        pass

    # The first message must include only headers without body,
    # the second the body (received after the 100 response arrived)

    assert (
        connection.transport.messages[0]
        == b"POST / HTTP/1.1\r\ncontent-type: application/json\r\nexpect: 100-continue\r\n\r\n"
    )
    assert connection.transport.messages[1] == b'{"id":"1","name":"foo"}'
Ejemplo n.º 2
0
async def test_connection_handle_upgrades(connection: ClientConnection, ):
    connection.headers = get_example_headers()
    connection.parser = FakeParser(101)
    connection.transport = object()

    connection.on_headers_complete()

    with pytest.raises(UpgradeResponse) as upgrade_response:
        await connection._wait_response()

    assert upgrade_response.value.response is connection.response
    assert upgrade_response.value.transport is connection.transport
    assert connection._upgraded is True