Skip to content

Commit 83f4f88

Browse files
azmeukcsernazs
authored andcommitted
Added https client code samples
1 parent 1e0732e commit 83f4f88

1 file changed

Lines changed: 35 additions & 9 deletions

File tree

doc/howto.rst

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -607,20 +607,46 @@ To run an https server, `trustme` can be used to do the heavy lifting:
607607
608608
609609
@pytest.fixture(scope="session")
610-
def localhost_cert(ca):
611-
return ca.issue_cert("localhost")
610+
def httpserver_ssl_context(ca):
611+
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
612+
localhost_cert = ca.issue_cert("localhost")
613+
localhost_cert.configure_cert(context)
614+
return context
612615
613616
614617
@pytest.fixture(scope="session")
615-
def httpserver_ssl_context(localhost_cert):
616-
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
618+
def httpclient_ssl_context(ca):
619+
with ca.cert_pem.tempfile() as ca_temp_path:
620+
return ssl.create_default_context(cafile=ca_temp_path)
617621
618-
crt = localhost_cert.cert_chain_pems[0]
619-
key = localhost_cert.private_key_pem
620-
with crt.tempfile() as crt_file, key.tempfile() as key_file:
621-
context.load_cert_chain(crt_file, key_file)
622622
623-
return context
623+
@pytest.mark.asyncio
624+
async def test_aiohttp(httpserver, httpclient_ssl_context):
625+
import aiohttp
626+
627+
httpserver.expect_request("/").respond_with_data("hello world!")
628+
connector = aiohttp.TCPConnector(ssl=httpclient_ssl_context)
629+
async with aiohttp.ClientSession(connector=connector) as session:
630+
async with session.get(httpserver.url_for("/")) as result:
631+
assert (await result.text()) == "hello world!"
632+
633+
634+
def test_requests(httpserver, ca):
635+
import requests
636+
637+
httpserver.expect_request("/").respond_with_data("hello world!")
638+
with ca.cert_pem.tempfile() as ca_temp_path:
639+
result = requests.get(httpserver.url_for("/"), verify=ca_temp_path)
640+
assert result.text == "hello world!"
641+
642+
643+
def test_httpx(httpserver, httpclient_ssl_context):
644+
import httpx
645+
646+
httpserver.expect_request("/").respond_with_data("hello world!")
647+
result = httpx.get(httpserver.url_for("/"), verify=httpclient_ssl_context)
648+
assert result.text == "hello world!"
649+
624650
625651
Using httpserver on a dual-stack (IPv4 and IPv6) system
626652
-------------------------------------------------------

0 commit comments

Comments
 (0)