conftest.py
This file
Path
tests/conftest.py
111 lines · 2.9 KiB · Python
Last changed by
Add 100-continue test (#68)Tom Christiebefore adoption4fe6bc0
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)Add 100-continue test (#68)Tom Christiebefore adoption4fe6bc0
lines 15–16why, one step back15 elif scope["path"].startswith("/echo_body"):16 await echo_body(scope, receive, send)Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 17–21why, one step back17 else:18 await hello_world(scope, receive, send)192021async def hello_world(scope, receive, send):Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 22–31why, one step back22 await send(23 {24 "type": "http.response.start",25 "status": 200,26 "headers": [[b"content-type", b"text/plain"]],27 }28 )29 await send({"type": "http.response.body", "body": b"Hello, world!"})3031Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 32–37why, one step back32async def slow_response(scope, receive, send):33 await asyncio.sleep(0.01)34 await send(35 {36 "type": "http.response.start",37 "status": 200,Merge pull request #39 from matusf/29-add-raise_for_statusTom Christiebefore adoption5c1cb30
lines 38–49why, one step back38 "headers": [[b"content-type", b"text/plain"]],39 }40 )41 await send({"type": "http.response.body", "body": b"Hello, world!"})424344async def status_code(scope, receive, send):45 status_code = int(scope["path"].replace("/status/", ""))46 await send(47 {48 "type": "http.response.start",49 "status": status_code,Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 50–53why, one step back50 "headers": [[b"content-type", b"text/plain"]],51 }52 )53 await send({"type": "http.response.body", "body": b"Hello, world!"})Add 100-continue test (#68)Tom Christiebefore adoption4fe6bc0
lines 54–72why, one step back545556async def echo_body(scope, receive, send):57 body = b''58 more_body = True5960 while more_body:61 message = await receive()62 body += message.get('body', b'')63 more_body = message.get('more_body', False)6465 await send(66 {67 "type": "http.response.start",68 "status": 200,69 "headers": [[b"content-type", b"text/plain"]],70 }71 )72 await send({"type": "http.response.body", "body": body})Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
lines 73–74why, one step back7374Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 75why, one step back75@pytest.fixtureMerge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8
lines 76–83why, one step back76def cert_and_key_paths():77 ca = trustme.CA()78 ca.issue_cert("example.org")79 with ca.cert_pem.tempfile() as cert_temp_path, ca.private_key_pem.tempfile() as key_temp_path:80 yield cert_temp_path, key_temp_path818283@pytest.fixtureMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 84–85why, one step back84async def server():85 config = Config(app=app, lifespan="off")Merge pull request #57 from yeraydiazdiaz/test-httpsTom Christiebefore adoptionc27c635
lines 86–97why, one step back86 server = Server(config=config)87 task = asyncio.ensure_future(server.serve())88 try:89 while not server.started:90 await asyncio.sleep(0.0001)91 yield server92 finally:93 server.should_exit = True94 await task959697@pytest.fixtureMerge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8
lines 98–102why, one step back98async def https_server(cert_and_key_paths):99 cert_path, key_path = cert_and_key_paths100 config = Config(101 app=app, lifespan="off", ssl_certfile=cert_path, ssl_keyfile=key_path, port=8001102 )Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 103–109why, one step back103 server = Server(config=config)104 task = asyncio.ensure_future(server.serve())105 try:106 while not server.started:107 await asyncio.sleep(0.0001)108 yield server109 finally:Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
lines 110–111why, one step back110 server.should_exit = True111 await task