From b3af383923bbf6c7ca2dd72630b3e32f9d32f490 Mon Sep 17 00:00:00 2001 From: Yeray Diaz Diaz Date: Sat, 11 May 2019 13:24:20 +0100 Subject: [PATCH] Add test to https server Add `https_server` test fixture --- requirements.txt | 1 + tests/conftest.py | 25 +++++++++++++++++++++++++ tests/dispatch/test_connections.py | 10 +++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 59fed558..520c7739 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,4 +17,5 @@ mypy pytest pytest-asyncio pytest-cov +trustme uvicorn diff --git a/tests/conftest.py b/tests/conftest.py index aab371cf..cc84fbb4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,8 @@ import asyncio +import os import pytest +import trustme from uvicorn.config import Config from uvicorn.main import Server @@ -62,3 +64,26 @@ async def server(): finally: server.should_exit = True await task + + +@pytest.fixture +async def https_server(): + ca = trustme.CA() + server_cert = ca.issue_cert("example.org") + with ca.cert_pem.tempfile() as cert_temp_path, ca.private_key_pem.tempfile() as key_temp_path: + config = Config( + app=app, + lifespan="off", + ssl_certfile=cert_temp_path, + ssl_keyfile=key_temp_path, + port=8001, + ) + server = Server(config=config) + task = asyncio.ensure_future(server.serve()) + try: + while not server.started: + await asyncio.sleep(0.0001) + yield server + finally: + server.should_exit = True + await task diff --git a/tests/dispatch/test_connections.py b/tests/dispatch/test_connections.py index a2bac09c..2edf3ada 100644 --- a/tests/dispatch/test_connections.py +++ b/tests/dispatch/test_connections.py @@ -1,6 +1,6 @@ import pytest -from httpcore import HTTPConnection, Request +from httpcore import HTTPConnection, Request, SSLConfig @pytest.mark.asyncio @@ -13,6 +13,14 @@ async def test_get(server): assert response.content == b"Hello, world!" +@pytest.mark.asyncio +async def test_https_get(https_server): + http = HTTPConnection(origin="https://127.0.0.1:8001/", ssl=SSLConfig(verify=False)) + response = await http.request("GET", "https://127.0.0.1:8001/") + assert response.status_code == 200 + assert response.content == b"Hello, world!" + + @pytest.mark.asyncio async def test_post(server): conn = HTTPConnection(origin="http://127.0.0.1:8000/") -- 2.47.3