Skip to content
Myr

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

1 file+65 added−44 removed

modified:tests/test_concurrency.py+65 added-44 removed
4 unmodified lines
5from httpx.concurrency.trio import TrioBackend6from tests.concurrency import run_concurrently789def get_asyncio_cipher(stream):10 return stream.stream_writer.get_extra_info("cipher", default=None)111213def get_trio_cipher(stream):14 return stream.stream.cipher() if isinstance(stream.stream, trio.SSLStream) else None151617async 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 = False2223 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 break3031 assert ended32 return response333435@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))29async 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 """42async 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)4539 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 )4950 try:51 assert stream.is_connection_dropped() is False52 assert get_cipher(stream) is None
3 unmodified lines
56 assert get_cipher(stream) is not None5758 await stream.write(b"GET / HTTP/1.1\r\n\r\n")5959 # 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")6271 assert ended72 assert read.startswith(b"HTTP/1.1 200 OK\r\n")63 finally:64 await stream.close()656667@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)74async 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)7778 stream = await backend.open_uds_stream(79 https_uds_server.config.uds, https_uds_server.url.host, None, timeout80 )8182 try:83 assert stream.is_connection_dropped() is False84 assert get_cipher(stream) is None8586 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 None8990 await stream.write(b"GET / HTTP/1.1\r\n\r\n")9192 response = await read_response(stream, timeout, should_contain=b"Hello, world")93 assert response.startswith(b"HTTP/1.1 200 OK\r\n")9495 finally:96 await stream.close()9798
14 unmodified lines