Skip to content
Myr

test_concurrency.py

This file

Path

tests/test_concurrency.py

65 lines · 2.1 KiB · Python

Last changed by

Add support for proxy tunnels for Python 3.6 + asyncio. (#521)Seth Michael Larsonbefore adoption331be99

History of this file

Take it

1import pytest2import trio34from httpx import AsyncioBackend, HTTPVersionConfig, SSLConfig, TimeoutConfig5from httpx.concurrency.trio import TrioBackend678@pytest.mark.parametrize(9    "backend, get_cipher",10    [11        pytest.param(12            AsyncioBackend(),13            lambda stream: stream.stream_writer.get_extra_info("cipher", default=None),14            marks=pytest.mark.asyncio,15        ),16        pytest.param(17            TrioBackend(),18            lambda stream: (19                stream.stream.cipher()20                if isinstance(stream.stream, trio.SSLStream)21                else None22            ),23            marks=pytest.mark.trio,24        ),25    ],26)27async def test_start_tls_on_socket_stream(https_server, backend, get_cipher):28    """29    See that the concurrency backend can make a connection without TLS then30    start TLS on an existing connection.31    """32    ctx = SSLConfig().load_ssl_context_no_verify(HTTPVersionConfig())33    timeout = TimeoutConfig(5)3435    stream = await backend.open_tcp_stream(36        https_server.url.host, https_server.url.port, None, timeout37    )3839    try:40        assert stream.is_connection_dropped() is False41        assert get_cipher(stream) is None4243        stream = await stream.start_tls(https_server.url.host, ctx, timeout)44        assert stream.is_connection_dropped() is False45        assert get_cipher(stream) is not None4647        await stream.write(b"GET / HTTP/1.1\r\n\r\n")4849        # stream.read() only gives us *up to* as much data as we ask for. In order to50        # cleanly close the stream, we must read until the end of the HTTP response.51        read = b""52        ended = False53        for _ in range(5):  # Try read some (not too large) number of times...54            read += await stream.read(8192, timeout)55            # We know we're at the end of the response when we've received the body plus56            # the terminating CRLFs.57            if b"Hello, world!" in read and read.endswith(b"\r\n\r\n"):58                ended = True59                break6061        assert ended62        assert read.startswith(b"HTTP/1.1 200 OK\r\n")6364    finally:65        await stream.close()