conftest.py
This file
Path
tests/conftest.py
89 lines · 2.3 KiB · Python
Last changed by
Merge pull request #57 from yeraydiazdiaz/test-httpsTom Christiebefore adoptionc27c635
History of this fileTake it
Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 1why, one step back1import asyncioMerge pull request #57 from yeraydiazdiaz/test-httpsTom Christiebefore adoptionc27c635
line 2why, one step back2import osMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 3–4why, one step back34import pytestMerge pull request #57 from yeraydiazdiaz/test-httpsTom Christiebefore adoptionc27c635
line 5why, one step back5import trustmeMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 6–11why, one step back6from uvicorn.config import Config7from uvicorn.main import Server8910async def app(scope, receive, send):11 assert scope["type"] == "http"Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 12–13why, one step back12 if scope["path"] == "/slow_response":13 await slow_response(scope, receive, send)Merge pull request #39 from matusf/29-add-raise_for_statusTom Christiebefore adoption5c1cb30
lines 14–15why, one step back14 elif scope["path"].startswith("/status"):15 await status_code(scope, receive, send)Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 16–20why, one step back16 else:17 await hello_world(scope, receive, send)181920async def hello_world(scope, receive, send):Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 21–30why, one step back21 await send(22 {23 "type": "http.response.start",24 "status": 200,25 "headers": [[b"content-type", b"text/plain"]],26 }27 )28 await send({"type": "http.response.body", "body": b"Hello, world!"})2930Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 31–36why, one step back31async def slow_response(scope, receive, send):32 await asyncio.sleep(0.01)33 await send(34 {35 "type": "http.response.start",36 "status": 200,Merge pull request #39 from matusf/29-add-raise_for_statusTom Christiebefore adoption5c1cb30
lines 37–48why, one step back37 "headers": [[b"content-type", b"text/plain"]],38 }39 )40 await send({"type": "http.response.body", "body": b"Hello, world!"})414243async def status_code(scope, receive, send):44 status_code = int(scope["path"].replace("/status/", ""))45 await send(46 {47 "type": "http.response.start",48 "status": status_code,Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 49–54why, one step back49 "headers": [[b"content-type", b"text/plain"]],50 }51 )52 await send({"type": "http.response.body", "body": b"Hello, world!"})5354Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 55–57why, one step back55@pytest.fixture56async def server():57 config = Config(app=app, lifespan="off")Merge pull request #57 from yeraydiazdiaz/test-httpsTom Christiebefore adoptionc27c635
lines 58–80why, one step back58 server = Server(config=config)59 task = asyncio.ensure_future(server.serve())60 try:61 while not server.started:62 await asyncio.sleep(0.0001)63 yield server64 finally:65 server.should_exit = True66 await task676869@pytest.fixture70async def https_server():71 ca = trustme.CA()72 server_cert = ca.issue_cert("example.org")73 with ca.cert_pem.tempfile() as cert_temp_path, ca.private_key_pem.tempfile() as key_temp_path:74 config = Config(75 app=app,76 lifespan="off",77 ssl_certfile=cert_temp_path,78 ssl_keyfile=key_temp_path,79 port=8001,80 )Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 81–87why, one step back81 server = Server(config=config)82 task = asyncio.ensure_future(server.serve())83 try:84 while not server.started:85 await asyncio.sleep(0.0001)86 yield server87 finally:Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
lines 88–89why, one step back88 server.should_exit = True89 await task