conftest.py
This file
Path
tests/conftest.py
90 lines · 2.4 KiB · Python
Last changed by
Merge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8
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
line 54why, one step back54@pytest.fixtureMerge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8
lines 55–62why, one step back55def cert_and_key_paths():56 ca = trustme.CA()57 ca.issue_cert("example.org")58 with ca.cert_pem.tempfile() as cert_temp_path, ca.private_key_pem.tempfile() as key_temp_path:59 yield cert_temp_path, key_temp_path606162@pytest.fixtureMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 63–64why, one step back63async def server():64 config = Config(app=app, lifespan="off")Merge pull request #57 from yeraydiazdiaz/test-httpsTom Christiebefore adoptionc27c635
lines 65–76why, one step back65 server = Server(config=config)66 task = asyncio.ensure_future(server.serve())67 try:68 while not server.started:69 await asyncio.sleep(0.0001)70 yield server71 finally:72 server.should_exit = True73 await task747576@pytest.fixtureMerge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8
lines 77–81why, one step back77async def https_server(cert_and_key_paths):78 cert_path, key_path = cert_and_key_paths79 config = Config(80 app=app, lifespan="off", ssl_certfile=cert_path, ssl_keyfile=key_path, port=800181 )Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 82–88why, one step back82 server = Server(config=config)83 task = asyncio.ensure_future(server.serve())84 try:85 while not server.started:86 await asyncio.sleep(0.0001)87 yield server88 finally:Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
lines 89–90why, one step back89 server.should_exit = True90 await task