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 fileTake it
Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
line 1why, one step back1import pytestAdd start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 2why, one step back2import trioAdd ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
lines 3–4why, one step back34from httpx import AsyncioBackend, HTTPVersionConfig, SSLConfig, TimeoutConfigAdd start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 5why, one step back5from httpx.concurrency.trio import TrioBackendAdd ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
lines 6–7why, one step back67Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
lines 8–25why, one step back8@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 ],Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
line 26why, one step back26)Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 27why, one step back27async def test_start_tls_on_socket_stream(https_server, backend, get_cipher):Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
line 28why, one step back28 """Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 29why, one step back29 See that the concurrency backend can make a connection without TLS thenAdd ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
lines 30–34why, one step back30 start TLS on an existing connection.31 """32 ctx = SSLConfig().load_ssl_context_no_verify(HTTPVersionConfig())33 timeout = TimeoutConfig(5)34Stream -> TCPStream (#339)Florimond Mancabefore adoption315a18b
line 35why, one step back35 stream = await backend.open_tcp_stream(Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
lines 36–37why, one step back36 https_server.url.host, https_server.url.port, None, timeout37 )Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
line 38why, one step back38Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 39–40why, one step back39 try:40 assert stream.is_connection_dropped() is FalseAdd start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 41why, one step back41 assert get_cipher(stream) is NoneAdd ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8
line 42why, one step back42Make start_tls a method on streams & return a new stream (#484)Jamie Hewlandbefore adoption644e8fc
line 43why, one step back43 stream = await stream.start_tls(https_server.url.host, ctx, timeout)Serve test server in thread (#292)Florimond Mancabefore adoption994403b
line 44why, one step back44 assert stream.is_connection_dropped() is FalseAdd start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368
line 45why, one step back45 assert get_cipher(stream) is not NoneServe test server in thread (#292)Florimond Mancabefore adoption994403b
lines 46–47why, one step back4647 await stream.write(b"GET / HTTP/1.1\r\n\r\n")asyncio: Wait for the stream to close when closing (#494)Jamie Hewlandbefore adoption88d73de
lines 48–62why, one step back4849 # 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")Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 63–65why, one step back6364 finally:65 await stream.close()