Skip to content
Myr

Add start_tls to Trio backend (#467)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Florimond Manca · committed by Seth Michael Larson · parent e5d0ad2 · Browse files · 2 files+51 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+51 added−9 removed

  • tests:+27 added−9 removed
  • other:+24 added−0 removed

2 files+51 added−9 removed

modified:httpx/concurrency/trio.py+24 added
169 unmodified lines
170 raise ConnectTimeout()171172 return TCPStream(stream=stream, timeout=timeout)173174 async def start_tls(175 self,176 stream: BaseTCPStream,177 hostname: str,178 ssl_context: ssl.SSLContext,179 timeout: TimeoutConfig,180 ) -> BaseTCPStream:181 assert isinstance(stream, TCPStream)182183 connect_timeout = _or_inf(timeout.connect_timeout)184 ssl_stream = trio.SSLStream(185 stream.stream, ssl_context=ssl_context, server_hostname=hostname186 )187188 with trio.move_on_after(connect_timeout) as cancel_scope:189 await ssl_stream.do_handshake()190191 if cancel_scope.cancelled_caught:192 raise ConnectTimeout()193194 stream.stream = ssl_stream195196 return stream197198 async def run_in_threadpool(199 self, func: typing.Callable, *args: typing.Any, **kwargs: typing.Any200 ) -> typing.Any:201 return await trio.to_thread.run_sync(
75 unmodified lines
modified:tests/test_concurrency.py+27 added-9 removed
1import sys23import pytest4import trio56from httpx import AsyncioBackend, HTTPVersionConfig, SSLConfig, TimeoutConfig7from httpx.concurrency.trio import TrioBackend898@pytest.mark.xfail(9 sys.version_info < (3, 7),10 reason="Requires Python 3.7+ for AbstractEventLoop.start_tls()",10@pytest.mark.parametrize(11 "backend, get_cipher",12 [13 pytest.param(14 AsyncioBackend(),15 lambda stream: stream.stream_writer.get_extra_info("cipher", default=None),16 marks=pytest.mark.asyncio,17 ),18 pytest.param(19 TrioBackend(),20 lambda stream: (21 stream.stream.cipher()22 if isinstance(stream.stream, trio.SSLStream)23 else None24 ),25 marks=pytest.mark.trio,26 ),27 ],28)12@pytest.mark.asyncio13async def test_start_tls_on_socket_stream(https_server):29async def test_start_tls_on_socket_stream(https_server, backend, get_cipher):30 """15 See that the backend can make a connection without TLS then31 See that the concurrency backend can make a connection without TLS then32 start TLS on an existing connection.33 """18 backend = AsyncioBackend()34 if isinstance(backend, AsyncioBackend) and sys.version_info < (3, 7):35 pytest.xfail(reason="Requires Python 3.7+ for AbstractEventLoop.start_tls()")3637 ctx = SSLConfig().load_ssl_context_no_verify(HTTPVersionConfig())38 timeout = TimeoutConfig(5)3940 stream = await backend.open_tcp_stream(
1 unmodified line
42 )4344 try:45 assert stream.is_connection_dropped() is False28 assert stream.stream_writer.get_extra_info("cipher", default=None) is None46 assert get_cipher(stream) is None4748 stream = await backend.start_tls(stream, https_server.url.host, ctx, timeout)49 assert stream.is_connection_dropped() is False32 assert stream.stream_writer.get_extra_info("cipher", default=None) is not None50 assert get_cipher(stream) is not None5152 await stream.write(b"GET / HTTP/1.1\r\n\r\n")53 assert (await stream.read(8192, timeout)).startswith(b"HTTP/1.1 200 OK\r\n")54
2 unmodified lines