Skip to content
Myr

test_concurrency.py

This file

Path

tests/test_concurrency.py

112 lines · 3.6 KiB · Python

Last changed by

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

History of this file

Take it

1import pytest2import trio34from httpx import AsyncioBackend, SSLConfig, Timeout5from httpx.concurrency.trio import TrioBackend6from tests.concurrency import run_concurrently789def get_asyncio_cipher(stream):10    return stream.stream_writer.get_extra_info("cipher", default=None)111213def get_trio_cipher(stream):14    return stream.stream.cipher() if isinstance(stream.stream, trio.SSLStream) else None151617async def read_response(stream, timeout: float, should_contain: bytes) -> bytes:18    # stream.read() only gives us *up to* as much data as we ask for. In order to19    # cleanly close the stream, we must read until the end of the HTTP response.20    response = b""21    ended = False2223    for _ in range(5):  # Try read some (not too large) number of times...24        response += await stream.read(8192, timeout)25        # We know we're at the end of the response when we've received the body plus26        # the terminating CRLFs.27        if should_contain in response and response.endswith(b"\r\n\r\n"):28            ended = True29            break3031    assert ended32    return response333435@pytest.mark.parametrize(36    "backend, get_cipher",37    [38        pytest.param(AsyncioBackend(), get_asyncio_cipher, marks=pytest.mark.asyncio),39        pytest.param(TrioBackend(), get_trio_cipher, marks=pytest.mark.trio),40    ],41)42async def test_start_tls_on_tcp_socket_stream(https_server, backend, get_cipher):43    ctx = SSLConfig().load_ssl_context_no_verify()44    timeout = Timeout(5)4546    stream = await backend.open_tcp_stream(47        https_server.url.host, https_server.url.port, None, timeout48    )4950    try:51        assert stream.is_connection_dropped() is False52        assert get_cipher(stream) is None5354        stream = await stream.start_tls(https_server.url.host, ctx, timeout)55        assert stream.is_connection_dropped() is False56        assert get_cipher(stream) is not None5758        await stream.write(b"GET / HTTP/1.1\r\n\r\n")5960        response = await read_response(stream, timeout, should_contain=b"Hello, world")61        assert response.startswith(b"HTTP/1.1 200 OK\r\n")6263    finally:64        await stream.close()656667@pytest.mark.parametrize(68    "backend, get_cipher",69    [70        pytest.param(AsyncioBackend(), get_asyncio_cipher, marks=pytest.mark.asyncio),71        pytest.param(TrioBackend(), get_trio_cipher, marks=pytest.mark.trio),72    ],73)74async def test_start_tls_on_uds_socket_stream(https_uds_server, backend, get_cipher):75    ctx = SSLConfig().load_ssl_context_no_verify()76    timeout = Timeout(5)7778    stream = await backend.open_uds_stream(79        https_uds_server.config.uds, https_uds_server.url.host, None, timeout80    )8182    try:83        assert stream.is_connection_dropped() is False84        assert get_cipher(stream) is None8586        stream = await stream.start_tls(https_uds_server.url.host, ctx, timeout)87        assert stream.is_connection_dropped() is False88        assert get_cipher(stream) is not None8990        await stream.write(b"GET / HTTP/1.1\r\n\r\n")9192        response = await read_response(stream, timeout, should_contain=b"Hello, world")93        assert response.startswith(b"HTTP/1.1 200 OK\r\n")9495    finally:96        await stream.close()979899async def test_concurrent_read(server, backend):100    """101    Regression test for: https://github.com/encode/httpx/issues/527102    """103    stream = await backend.open_tcp_stream(104        server.url.host, server.url.port, ssl_context=None, timeout=Timeout(5)105    )106    try:107        await stream.write(b"GET / HTTP/1.1\r\n\r\n")108        await run_concurrently(109            backend, lambda: stream.read(10), lambda: stream.read(10)110        )111    finally:112        await stream.close()