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
Provenance
- Commit
38a1368- Tree
80d3820- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
Seth Michael Larson <sethmichaellarson@gmail.com>- Parent
e5d0ad2
2 files changed+51 added−9 removed
modified:httpx/concurrency/trio.py+24 added
169 unmodified lines170 raise ConnectTimeout()171 172 return TCPStream(stream=stream, timeout=timeout)173 174+ 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)182+183+ connect_timeout = _or_inf(timeout.connect_timeout)184+ ssl_stream = trio.SSLStream(185+ stream.stream, ssl_context=ssl_context, server_hostname=hostname186+ )187+188+ with trio.move_on_after(connect_timeout) as cancel_scope:189+ await ssl_stream.do_handshake()190+191+ if cancel_scope.cancelled_caught:192+ raise ConnectTimeout()193+194+ stream.stream = ssl_stream195+196+ return stream197+198 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 linesmodified:tests/test_concurrency.py+27 added-9 removed
1 import sys2 3 import pytest4+import trio5 6 from httpx import AsyncioBackend, HTTPVersionConfig, SSLConfig, TimeoutConfig7+from httpx.concurrency.trio import TrioBackend8 9 8-@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.asyncio13-async def test_start_tls_on_socket_stream(https_server):29+async 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()")36+37 ctx = SSLConfig().load_ssl_context_no_verify(HTTPVersionConfig())38 timeout = TimeoutConfig(5)39 40 stream = await backend.open_tcp_stream(1 unmodified line42 )43 44 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 None47 48 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 None51 52 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