async def test_start_tls_on_socket_stream(https_server): """ See that the backend can make a connection without TLS then start TLS on an existing connection. """ backend = AsyncioBackend() ctx = SSLConfig().load_ssl_context_no_verify(HTTPVersionConfig()) timeout = TimeoutConfig(5) stream = await backend.open_tcp_stream( https_server.url.host, https_server.url.port, None, timeout ) try: assert stream.is_connection_dropped() is False assert stream.stream_writer.get_extra_info("cipher", default=None) is None stream = await backend.start_tls(stream, https_server.url.host, ctx, timeout) assert stream.is_connection_dropped() is False assert stream.stream_writer.get_extra_info("cipher", default=None) is not None await stream.write(b"GET / HTTP/1.1\r\n\r\n") assert (await stream.read(8192, timeout)).startswith(b"HTTP/1.1 200 OK\r\n") finally: await stream.close()
import sys import pytest import trio from httpx import AsyncioBackend, HTTPVersionConfig, SSLConfig, TimeoutConfig from httpx.concurrency.trio import TrioBackend @pytest.mark.parametrize( "backend, get_cipher", [ pytest.param( AsyncioBackend(), lambda stream: stream.stream_writer.get_extra_info("cipher", default=None), marks=pytest.mark.asyncio, ), pytest.param( TrioBackend(), lambda stream: (stream.stream.cipher() if isinstance(stream.stream, trio.SSLStream) else None), marks=pytest.mark.trio, ), ], ) async def test_start_tls_on_socket_stream(https_server, backend, get_cipher): """ See that the concurrency backend can make a connection without TLS then start TLS on an existing connection.
def __init__(self, data_to_send=b"", backend=None): self.backend = AsyncioBackend() if backend is None else backend self.data_to_send = data_to_send self.received_data = [] self.stream = MockRawSocketStream(self)
def __init__(self, app, backend=None): self.app = app self.backend = AsyncioBackend() if backend is None else backend self.server = None
async def restart(server): await backend.run_in_threadpool(AsyncioBackend().run, server.restart)
response += await stream.read(8192, timeout) # We know we're at the end of the response when we've received the body plus # the terminating CRLFs. if should_contain in response and response.endswith(b"\r\n\r\n"): ended = True break assert ended return response @pytest.mark.parametrize( "backend, get_cipher", [ pytest.param( AsyncioBackend(), get_asyncio_cipher, marks=pytest.mark.asyncio), pytest.param(TrioBackend(), get_trio_cipher, marks=pytest.mark.trio), ], ) async def test_start_tls_on_tcp_socket_stream(https_server, backend, get_cipher): ctx = SSLConfig().load_ssl_context_no_verify() timeout = TimeoutConfig(5) stream = await backend.open_tcp_stream(https_server.url.host, https_server.url.port, None, timeout) try: assert stream.is_connection_dropped() is False assert get_cipher(stream) is None