conftest.py
This file
Path
tests/conftest.py
111 lines · 2.9 KiB · Python
Last changed by
Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
History of this fileTake it
1import asyncio23import pytest4import trustme5from uvicorn.config import Config6from uvicorn.main import Server789async def app(scope, receive, send):10 assert scope["type"] == "http"11 if scope["path"] == "/slow_response":12 await slow_response(scope, receive, send)13 elif scope["path"].startswith("/status"):14 await status_code(scope, receive, send)15 elif scope["path"].startswith("/echo_body"):16 await echo_body(scope, receive, send)17 else:18 await hello_world(scope, receive, send)192021async def hello_world(scope, receive, send):22 await send(23 {24 "type": "http.response.start",25 "status": 200,26 "headers": [[b"content-type", b"text/plain"]],27 }28 )29 await send({"type": "http.response.body", "body": b"Hello, world!"})303132async def slow_response(scope, receive, send):33 await asyncio.sleep(0.01)34 await send(35 {36 "type": "http.response.start",37 "status": 200,38 "headers": [[b"content-type", b"text/plain"]],39 }40 )41 await send({"type": "http.response.body", "body": b"Hello, world!"})424344async def status_code(scope, receive, send):45 status_code = int(scope["path"].replace("/status/", ""))46 await send(47 {48 "type": "http.response.start",49 "status": status_code,50 "headers": [[b"content-type", b"text/plain"]],51 }52 )53 await send({"type": "http.response.body", "body": b"Hello, world!"})545556async def echo_body(scope, receive, send):57 body = b""58 more_body = True5960 while more_body:61 message = await receive()62 body += message.get("body", b"")63 more_body = message.get("more_body", False)6465 await send(66 {67 "type": "http.response.start",68 "status": 200,69 "headers": [[b"content-type", b"text/plain"]],70 }71 )72 await send({"type": "http.response.body", "body": body})737475@pytest.fixture76def cert_and_key_paths():77 ca = trustme.CA()78 ca.issue_cert("example.org")79 with ca.cert_pem.tempfile() as cert_temp_path, ca.private_key_pem.tempfile() as key_temp_path:80 yield cert_temp_path, key_temp_path818283@pytest.fixture84async def server():85 config = Config(app=app, lifespan="off")86 server = Server(config=config)87 task = asyncio.ensure_future(server.serve())88 try:89 while not server.started:90 await asyncio.sleep(0.0001)91 yield server92 finally:93 server.should_exit = True94 await task959697@pytest.fixture98async def https_server(cert_and_key_paths):99 cert_path, key_path = cert_and_key_paths100 config = Config(101 app=app, lifespan="off", ssl_certfile=cert_path, ssl_keyfile=key_path, port=8001102 )103 server = Server(config=config)104 task = asyncio.ensure_future(server.serve())105 try:106 while not server.started:107 await asyncio.sleep(0.0001)108 yield server109 finally:110 server.should_exit = True111 await task