import asyncio import json import pytest from uvicorn.config import Config from uvicorn.main import Server async def app(scope, receive, send): assert scope['type'] == 'http' await send({ 'type': 'http.response.start', 'status': 200, 'headers': [ [b'content-type', b'text/plain'], ] }) await send({ 'type': 'http.response.body', 'body': b'Hello, world!', }) @pytest.fixture async def server(): config = Config(app=app, lifespan="off") server = Server(config=config) task = asyncio.ensure_future(server.serve()) try: while not server.started: await asyncio.sleep(0.0001) yield server finally: task.cancel()