Refactor start_tls tests (#567)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Refactor start_tls tests * Clean up read_response()
authored by Florimond Manca · committed by GitHub · parent 364378a · Browse files · 1 file·+65 added−44 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+65 added−44 removed
- tests:+65 added−44 removed
Provenance
- Commit
3218c35- Tree
e12c0aa- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
364378a
1 file changed+65 added−44 removed
modified:tests/test_concurrency.py+65 added-44 removed
4 unmodified lines5 from httpx.concurrency.trio import TrioBackend6 from tests.concurrency import run_concurrently7 8 9+def get_asyncio_cipher(stream):10+ return stream.stream_writer.get_extra_info("cipher", default=None)11+12+13+def get_trio_cipher(stream):14+ return stream.stream.cipher() if isinstance(stream.stream, trio.SSLStream) else None15+16+17+async def read_response(stream, timeout: float, should_contain: bytes) -> bytes:18+ # stream.read() only gives us *up to* as much data as we ask for. In order to19+ # cleanly close the stream, we must read until the end of the HTTP response.20+ response = b""21+ ended = False22+23+ for _ in range(5): # Try read some (not too large) number of times...24+ response += await stream.read(8192, timeout)25+ # We know we're at the end of the response when we've received the body plus26+ # the terminating CRLFs.27+ if should_contain in response and response.endswith(b"\r\n\r\n"):28+ ended = True29+ break30+31+ assert ended32+ return response33+34+35 @pytest.mark.parametrize(36 "backend, get_cipher",37 [12- pytest.param(13- AsyncioBackend(),14- lambda stream: stream.stream_writer.get_extra_info("cipher", default=None),15- marks=pytest.mark.asyncio,16- ),17- pytest.param(18- TrioBackend(),19- lambda stream: (20- stream.stream.cipher()21- if isinstance(stream.stream, trio.SSLStream)22- else None23- ),24- marks=pytest.mark.trio,25- ),38+ pytest.param(AsyncioBackend(), get_asyncio_cipher, marks=pytest.mark.asyncio),39+ pytest.param(TrioBackend(), get_trio_cipher, marks=pytest.mark.trio),40 ],41 )28-@pytest.mark.parametrize("use_uds", (False, True))29-async def test_start_tls_on_socket_stream(30- https_server, https_uds_server, backend, get_cipher, use_uds31-):32- """33- See that the concurrency backend can make a connection without TLS then34- start TLS on an existing connection.35- """42+async def test_start_tls_on_tcp_socket_stream(https_server, backend, get_cipher):43 ctx = SSLConfig().load_ssl_context_no_verify(HTTPVersionConfig())44 timeout = TimeoutConfig(5)45 39- if use_uds:40- assert https_uds_server.config.uds is not None41- stream = await backend.open_uds_stream(42- https_uds_server.config.uds, https_uds_server.url.host, None, timeout43- )44- else:45- stream = await backend.open_tcp_stream(46- https_server.url.host, https_server.url.port, None, timeout47- )46+ stream = await backend.open_tcp_stream(47+ https_server.url.host, https_server.url.port, None, timeout48+ )49 50 try:51 assert stream.is_connection_dropped() is False52 assert get_cipher(stream) is None3 unmodified lines56 assert get_cipher(stream) is not None57 58 await stream.write(b"GET / HTTP/1.1\r\n\r\n")59 59- # stream.read() only gives us *up to* as much data as we ask for. In order to60- # cleanly close the stream, we must read until the end of the HTTP response.61- read = b""62- ended = False63- for _ in range(5): # Try read some (not too large) number of times...64- read += await stream.read(8192, timeout)65- # We know we're at the end of the response when we've received the body plus66- # the terminating CRLFs.67- if b"Hello, world!" in read and read.endswith(b"\r\n\r\n"):68- ended = True69- break60+ response = await read_response(stream, timeout, should_contain=b"Hello, world")61+ assert response.startswith(b"HTTP/1.1 200 OK\r\n")62 71- assert ended72- assert read.startswith(b"HTTP/1.1 200 OK\r\n")63+ finally:64+ await stream.close()65 66+67+@pytest.mark.parametrize(68+ "backend, get_cipher",69+ [70+ pytest.param(AsyncioBackend(), get_asyncio_cipher, marks=pytest.mark.asyncio),71+ pytest.param(TrioBackend(), get_trio_cipher, marks=pytest.mark.trio),72+ ],73+)74+async def test_start_tls_on_uds_socket_stream(https_uds_server, backend, get_cipher):75+ ctx = SSLConfig().load_ssl_context_no_verify(HTTPVersionConfig())76+ timeout = TimeoutConfig(5)77+78+ stream = await backend.open_uds_stream(79+ https_uds_server.config.uds, https_uds_server.url.host, None, timeout80+ )81+82+ try:83+ assert stream.is_connection_dropped() is False84+ assert get_cipher(stream) is None85+86+ stream = await stream.start_tls(https_uds_server.url.host, ctx, timeout)87+ assert stream.is_connection_dropped() is False88+ assert get_cipher(stream) is not None89+90+ await stream.write(b"GET / HTTP/1.1\r\n\r\n")91+92+ response = await read_response(stream, timeout, should_contain=b"Hello, world")93+ assert response.startswith(b"HTTP/1.1 200 OK\r\n")94+95 finally:96 await stream.close()97 98 14 unmodified lines