Skip to content
Myr

conftest.py

This file

Path

tests/conftest.py

64 lines · 1.7 KiB · Python

Last changed by

Merge pull request #39 from matusf/29-add-raise_for_statusTom Christiebefore adoption5c1cb30

History of this file

Take it

1import asyncio23import pytest4from uvicorn.config import Config5from uvicorn.main import Server678async def app(scope, receive, send):9    assert scope["type"] == "http"10    if scope["path"] == "/slow_response":11        await slow_response(scope, receive, send)12    elif scope["path"].startswith("/status"):13        await status_code(scope, receive, send)14    else:15        await hello_world(scope, receive, send)161718async def hello_world(scope, receive, send):19    await send(20        {21            "type": "http.response.start",22            "status": 200,23            "headers": [[b"content-type", b"text/plain"]],24        }25    )26    await send({"type": "http.response.body", "body": b"Hello, world!"})272829async def slow_response(scope, receive, send):30    await asyncio.sleep(0.01)31    await send(32        {33            "type": "http.response.start",34            "status": 200,35            "headers": [[b"content-type", b"text/plain"]],36        }37    )38    await send({"type": "http.response.body", "body": b"Hello, world!"})394041async def status_code(scope, receive, send):42    status_code = int(scope["path"].replace("/status/", ""))43    await send(44        {45            "type": "http.response.start",46            "status": status_code,47            "headers": [[b"content-type", b"text/plain"]],48        }49    )50    await send({"type": "http.response.body", "body": b"Hello, world!"})515253@pytest.fixture54async def server():55    config = Config(app=app, lifespan="off")56    server = Server(config=config)57    task = asyncio.ensure_future(server.serve())58    try:59        while not server.started:60            await asyncio.sleep(0.0001)61        yield server62    finally:63        server.should_exit = True64        await task