Add test to https server

Add `https_server` test fixture
This commit is contained in:
Yeray Diaz Diaz 2019-05-11 13:24:20 +01:00
parent 21ac95a77a
commit b3af383923
3 changed files with 35 additions and 1 deletions

View File

@ -17,4 +17,5 @@ mypy
pytest
pytest-asyncio
pytest-cov
trustme
uvicorn

View File

@ -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

View File

@ -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/")