Skip to content
Myr

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 file

Take it

Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8

line 1why, one step back
1import pytest

Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368

line 2why, one step back
2import trio

Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8

lines 3–4why, one step back
3
4from httpx import AsyncioBackend, HTTPVersionConfig, SSLConfig, TimeoutConfig

Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368

line 5why, one step back
5from httpx.concurrency.trio import TrioBackend

Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8

lines 6–7why, one step back
6
7

Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368

lines 8–25why, one step back
8@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 back
26)

Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368

line 27why, one step back
27async 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 back
28 """

Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368

line 29why, one step back
29 See that the concurrency backend can make a connection without TLS then

Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8

lines 30–34why, one step back
30 start TLS on an existing connection.
31 """32 ctx = SSLConfig().load_ssl_context_no_verify(HTTPVersionConfig())33 timeout = TimeoutConfig(5)34

Stream -> TCPStream (#339)Florimond Mancabefore adoption315a18b

line 35why, one step back
35 stream = await backend.open_tcp_stream(

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

lines 36–37why, one step back
36 https_server.url.host, https_server.url.port, None, timeout
37 )

Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8

line 38why, one step back
38

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 39–40why, one step back
39 try:
40 assert stream.is_connection_dropped() is False

Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368

line 41why, one step back
41 assert get_cipher(stream) is None

Add ConcurrencyBackend.start_tls() (#263)Seth Michael Larsonbefore adoption1872ae8

line 42why, one step back
42

Make start_tls a method on streams & return a new stream (#484)Jamie Hewlandbefore adoption644e8fc

line 43why, one step back
43 stream = await stream.start_tls(https_server.url.host, ctx, timeout)

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

line 44why, one step back
44 assert stream.is_connection_dropped() is False

Add start_tls to Trio backend (#467)Florimond Mancabefore adoption38a1368

line 45why, one step back
45 assert get_cipher(stream) is not None

Serve test server in thread (#292)Florimond Mancabefore adoption994403b

lines 46–47why, one step back
46
47 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 back
48
49 # 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 back
63
64 finally:65 await stream.close()