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 fileTake it
Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 1–9why, one step back1import asyncio23import pytest4from uvicorn.config import Config5from uvicorn.main import Server678async def app(scope, receive, send):9 assert scope["type"] == "http"Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 10–11why, one step back10 if scope["path"] == "/slow_response":11 await slow_response(scope, receive, send)Merge pull request #39 from matusf/29-add-raise_for_statusTom Christiebefore adoption5c1cb30
lines 12–13why, one step back12 elif scope["path"].startswith("/status"):13 await status_code(scope, receive, send)Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 14–18why, one step back14 else:15 await hello_world(scope, receive, send)161718async def hello_world(scope, receive, send):Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 19–28why, one step back19 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!"})2728Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 29–34why, one step back29async def slow_response(scope, receive, send):30 await asyncio.sleep(0.01)31 await send(32 {33 "type": "http.response.start",34 "status": 200,Merge pull request #39 from matusf/29-add-raise_for_statusTom Christiebefore adoption5c1cb30
lines 35–46why, one step back35 "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,Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 47–52why, one step back47 "headers": [[b"content-type", b"text/plain"]],48 }49 )50 await send({"type": "http.response.body", "body": b"Hello, world!"})5152Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 53–62why, one step back53@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:Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
lines 63–64why, one step back63 server.should_exit = True64 await task