Exemple #1
0
def test_upgraded_connection_does_not_return_to_pool(event_loop):
    pool = FakePoolThrowingOnRelease(
        event_loop, b"http", b"foo.com", 80, None, max_size=2
    )
    connection = ClientConnection(pool.loop, pool)
    connection._upgraded = True
    connection.release()
Exemple #2
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"}'
Exemple #3
0
def test_connection_has_a_response_when_headers_are_complete(
    connection: ClientConnection, ):
    connection.headers = get_example_headers()

    connection.on_headers_complete()

    # when headers are complete, the connection must have a response
    assert connection.response is not None
    response = connection.response

    for name, value in get_example_headers():
        assert response.headers.get_single(name) == value
Exemple #4
0
async def test_connection_handle_chunked_transfer_encoding(
    connection: ClientConnection, ):
    # Arrange fake response...
    connection.headers = get_example_headers()
    connection.headers.append((b"content-type", b"text/plain"))
    connection.headers.append((b"transfer-encoding", b"chunked"))
    connection.on_headers_complete()

    # connection is going to wait for a response:
    assert connection.response is not None
    assert connection.response.content is not None
    assert isinstance(connection.response.content, IncomingContent)
Exemple #5
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
Exemple #6
0
def test_return_connection_does_nothing_if_the_queue_is_full():
    pool = ClientConnectionPool(asyncio.get_event_loop(),
                                b"http",
                                b"foo.com",
                                80,
                                None,
                                max_size=2)

    for i in range(5):
        pool.try_return_connection(ClientConnection(pool.loop, pool))

        if i + 1 >= 2:
            assert pool._idle_connections.full() is True
Exemple #7
0
def connection(pool):
    connection = ClientConnection(pool.loop, pool)
    connection.parser = FakeParser(200)
    yield connection
Exemple #8
0
async def test_connection_send_throws_if_closed(connection: ClientConnection):
    connection.open = False

    with pytest.raises(ConnectionClosedError):
        await connection.send(Request("GET", b"/", None))
Exemple #9
0
def test_return_connection_disposed_pool_does_nothing():
    pool = ClientConnectionPool(asyncio.get_event_loop(), b"http", b"foo.com",
                                80, None)

    pool.dispose()
    pool.try_return_connection(ClientConnection(pool.loop, pool))