conftest.py
This file
Path
tests/conftest.py
88 lines · 2.3 KiB · Python
Last changed by
Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
History of this fileTake it
Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 1–3why, one step back1import asyncio23import pytestMerge pull request #57 from yeraydiazdiaz/test-httpsTom Christiebefore adoptionc27c635
line 4why, one step back4import trustmeMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 5–10why, one step back5from uvicorn.config import Config6from uvicorn.main import Server789async def app(scope, receive, send):10 assert scope["type"] == "http"Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 11–12why, one step back11 if scope["path"] == "/slow_response":12 await slow_response(scope, receive, send)Merge pull request #39 from matusf/29-add-raise_for_statusTom Christiebefore adoption5c1cb30
lines 13–14why, one step back13 elif scope["path"].startswith("/status"):14 await status_code(scope, receive, send)Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 15–19why, one step back15 else:16 await hello_world(scope, receive, send)171819async def hello_world(scope, receive, send):Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 20–29why, one step back20 await send(21 {22 "type": "http.response.start",23 "status": 200,24 "headers": [[b"content-type", b"text/plain"]],25 }26 )27 await send({"type": "http.response.body", "body": b"Hello, world!"})2829Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 30–35why, one step back30async def slow_response(scope, receive, send):31 await asyncio.sleep(0.01)32 await send(33 {34 "type": "http.response.start",35 "status": 200,Merge pull request #39 from matusf/29-add-raise_for_statusTom Christiebefore adoption5c1cb30
lines 36–47why, one step back36 "headers": [[b"content-type", b"text/plain"]],37 }38 )39 await send({"type": "http.response.body", "body": b"Hello, world!"})404142async def status_code(scope, receive, send):43 status_code = int(scope["path"].replace("/status/", ""))44 await send(45 {46 "type": "http.response.start",47 "status": status_code,Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 48–53why, one step back48 "headers": [[b"content-type", b"text/plain"]],49 }50 )51 await send({"type": "http.response.body", "body": b"Hello, world!"})5253Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 54–56why, one step back54@pytest.fixture55async def server():56 config = Config(app=app, lifespan="off")Merge pull request #57 from yeraydiazdiaz/test-httpsTom Christiebefore adoptionc27c635
lines 57–79why, one step back57 server = Server(config=config)58 task = asyncio.ensure_future(server.serve())59 try:60 while not server.started:61 await asyncio.sleep(0.0001)62 yield server63 finally:64 server.should_exit = True65 await task666768@pytest.fixture69async def https_server():70 ca = trustme.CA()71 server_cert = ca.issue_cert("example.org")72 with ca.cert_pem.tempfile() as cert_temp_path, ca.private_key_pem.tempfile() as key_temp_path:73 config = Config(74 app=app,75 lifespan="off",76 ssl_certfile=cert_temp_path,77 ssl_keyfile=key_temp_path,78 port=8001,79 )Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 80–86why, one step back80 server = Server(config=config)81 task = asyncio.ensure_future(server.serve())82 try:83 while not server.started:84 await asyncio.sleep(0.0001)85 yield server86 finally:Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
lines 87–88why, one step back87 server.should_exit = True88 await task