conftest.py
This file
Path
tests/conftest.py
152 lines · 3.8 KiB · Python
Last changed by
Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
History of this fileTake it
1import asyncio23import pytest4import trustme5from cryptography.hazmat.primitives.serialization import (6 BestAvailableEncryption,7 Encoding,8 PrivateFormat,9)10from uvicorn.config import Config11from uvicorn.main import Server121314async def app(scope, receive, send):15 assert scope["type"] == "http"16 if scope["path"] == "/slow_response":17 await slow_response(scope, receive, send)18 elif scope["path"].startswith("/status"):19 await status_code(scope, receive, send)20 elif scope["path"].startswith("/echo_body"):21 await echo_body(scope, receive, send)22 else:23 await hello_world(scope, receive, send)242526async def hello_world(scope, receive, send):27 await send(28 {29 "type": "http.response.start",30 "status": 200,31 "headers": [[b"content-type", b"text/plain"]],32 }33 )34 await send({"type": "http.response.body", "body": b"Hello, world!"})353637async def slow_response(scope, receive, send):38 await asyncio.sleep(0.1)39 await send(40 {41 "type": "http.response.start",42 "status": 200,43 "headers": [[b"content-type", b"text/plain"]],44 }45 )46 await send({"type": "http.response.body", "body": b"Hello, world!"})474849async def status_code(scope, receive, send):50 status_code = int(scope["path"].replace("/status/", ""))51 await send(52 {53 "type": "http.response.start",54 "status": status_code,55 "headers": [[b"content-type", b"text/plain"]],56 }57 )58 await send({"type": "http.response.body", "body": b"Hello, world!"})596061async def echo_body(scope, receive, send):62 body = b""63 more_body = True6465 while more_body:66 message = await receive()67 body += message.get("body", b"")68 more_body = message.get("more_body", False)6970 await send(71 {72 "type": "http.response.start",73 "status": 200,74 "headers": [[b"content-type", b"text/plain"]],75 }76 )77 await send({"type": "http.response.body", "body": body})787980class CAWithPKEncryption(trustme.CA):81 """Implementation of trustme.CA() that can emit82 private keys that are encrypted with a password.83 """8485 @property86 def encrypted_private_key_pem(self):87 return trustme.Blob(88 self._private_key.private_bytes(89 Encoding.PEM,90 PrivateFormat.TraditionalOpenSSL,91 BestAvailableEncryption(password=b"password"),92 )93 )949596@pytest.fixture97def example_cert():98 ca = CAWithPKEncryption()99 ca.issue_cert("example.org")100 return ca101102103@pytest.fixture104def cert_pem_file(example_cert):105 with example_cert.cert_pem.tempfile() as tmp:106 yield tmp107108109@pytest.fixture110def cert_private_key_file(example_cert):111 with example_cert.private_key_pem.tempfile() as tmp:112 yield tmp113114115@pytest.fixture116def cert_encrypted_private_key_file(example_cert):117 with example_cert.encrypted_private_key_pem.tempfile() as tmp:118 yield tmp119120121@pytest.fixture122async def server():123 config = Config(app=app, lifespan="off")124 server = Server(config=config)125 task = asyncio.ensure_future(server.serve())126 try:127 while not server.started:128 await asyncio.sleep(0.0001)129 yield server130 finally:131 server.should_exit = True132 await task133134135@pytest.fixture136async def https_server(cert_pem_file, cert_private_key_file):137 config = Config(138 app=app,139 lifespan="off",140 ssl_certfile=cert_pem_file,141 ssl_keyfile=cert_private_key_file,142 port=8001,143 )144 server = Server(config=config)145 task = asyncio.ensure_future(server.serve())146 try:147 while not server.started:148 await asyncio.sleep(0.0001)149 yield server150 finally:151 server.should_exit = True152 await task