conftest.py
This file
Path
tests/conftest.py
176 lines · 4.5 KiB · Python
Last changed by
Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
History of this fileTake it
The walk stopped after 200 commits: lines older than e1ecb36 point at it.
Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 1–3why, one step back1import asyncio23import pytestMerge pull request #57 from yeraydiazdiaz/test-httpsTom Christiebefore adoptionc27c635
line 4why, one step back4import trustmeSupport client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
lines 5–9why, one step back5from cryptography.hazmat.primitives.serialization import (6 BestAvailableEncryption,7 Encoding,8 PrivateFormat,9)Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 10–12why, one step back10from uvicorn.config import Config11from uvicorn.main import Server12Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 13why, one step back13from httpx import AsyncioBackendCarefulness with encoding everywhereTom Christiebefore adoptione1ecb36
line 14why, one step back14Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 15–21why, one step back1516@pytest.fixture(params=[pytest.param(AsyncioBackend, marks=pytest.mark.asyncio)])17def backend(request):18 backend_cls = request.param19 return backend_cls()2021Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 22–25why, one step back22async def app(scope, receive, send):23 assert scope["type"] == "http"24 if scope["path"] == "/slow_response":25 await slow_response(scope, receive, send)Merge pull request #39 from matusf/29-add-raise_for_statusTom Christiebefore adoption5c1cb30
lines 26–27why, one step back26 elif scope["path"].startswith("/status"):27 await status_code(scope, receive, send)Add 100-continue test (#68)Tom Christiebefore adoption4fe6bc0
lines 28–29why, one step back28 elif scope["path"].startswith("/echo_body"):29 await echo_body(scope, receive, send)Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 30–45why, one step back30 else:31 await hello_world(scope, receive, send)323334async def hello_world(scope, receive, send):35 await send(36 {37 "type": "http.response.start",38 "status": 200,39 "headers": [[b"content-type", b"text/plain"]],40 }41 )42 await send({"type": "http.response.body", "body": b"Hello, world!"})434445async def slow_response(scope, receive, send):Version 0.6.4 (#105)Tom Christiebefore adoption5326cd1
line 46why, one step back46 await asyncio.sleep(0.1)Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 47–50why, one step back47 await send(48 {49 "type": "http.response.start",50 "status": 200,Merge pull request #39 from matusf/29-add-raise_for_statusTom Christiebefore adoption5c1cb30
lines 51–62why, one step back51 "headers": [[b"content-type", b"text/plain"]],52 }53 )54 await send({"type": "http.response.body", "body": b"Hello, world!"})555657async def status_code(scope, receive, send):58 status_code = int(scope["path"].replace("/status/", ""))59 await send(60 {61 "type": "http.response.start",62 "status": status_code,Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 63–66why, one step back63 "headers": [[b"content-type", b"text/plain"]],64 }65 )66 await send({"type": "http.response.body", "body": b"Hello, world!"})Add 100-continue test (#68)Tom Christiebefore adoption4fe6bc0
lines 67–69why, one step back676869async def echo_body(scope, receive, send):Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 70why, one step back70 body = b""Add 100-continue test (#68)Tom Christiebefore adoption4fe6bc0
lines 71–74why, one step back71 more_body = True7273 while more_body:74 message = await receive()Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
lines 75–76why, one step back75 body += message.get("body", b"")76 more_body = message.get("more_body", False)Add 100-continue test (#68)Tom Christiebefore adoption4fe6bc0
lines 77–85why, one step back7778 await send(79 {80 "type": "http.response.start",81 "status": 200,82 "headers": [[b"content-type", b"text/plain"]],83 }84 )85 await send({"type": "http.response.body", "body": body})Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 86–87why, one step back8687Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
lines 88–103why, one step back88class CAWithPKEncryption(trustme.CA):89 """Implementation of trustme.CA() that can emit90 private keys that are encrypted with a password.91 """9293 @property94 def encrypted_private_key_pem(self):95 return trustme.Blob(96 self._private_key.private_bytes(97 Encoding.PEM,98 PrivateFormat.TraditionalOpenSSL,99 BestAvailableEncryption(password=b"password"),100 )101 )102103Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
line 104why, one step back104@pytest.fixtureSupport client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
lines 105–106why, one step back105def example_cert():106 ca = CAWithPKEncryption()Merge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8
line 107why, one step back107 ca.issue_cert("example.org")Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
line 108why, one step back108 return caMerge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8
lines 109–111why, one step back109110111@pytest.fixtureSupport client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
lines 112–129why, one step back112def cert_pem_file(example_cert):113 with example_cert.cert_pem.tempfile() as tmp:114 yield tmp115116117@pytest.fixture118def cert_private_key_file(example_cert):119 with example_cert.private_key_pem.tempfile() as tmp:120 yield tmp121122123@pytest.fixture124def cert_encrypted_private_key_file(example_cert):125 with example_cert.encrypted_private_key_pem.tempfile() as tmp:126 yield tmp127128129@pytest.fixtureCarefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 130–131why, one step back130async def server():131 config = Config(app=app, lifespan="off")Merge pull request #57 from yeraydiazdiaz/test-httpsTom Christiebefore adoptionc27c635
lines 132–143why, one step back132 server = Server(config=config)133 task = asyncio.ensure_future(server.serve())134 try:135 while not server.started:136 await asyncio.sleep(0.0001)137 yield server138 finally:139 server.should_exit = True140 await task141142143@pytest.fixtureSupport client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
line 144why, one step back144async def https_server(cert_pem_file, cert_private_key_file):Merge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8
line 145why, one step back145 config = Config(Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
lines 146–150why, one step back146 app=app,147 lifespan="off",148 ssl_certfile=cert_pem_file,149 ssl_keyfile=cert_private_key_file,150 port=8001,Merge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8
line 151why, one step back151 )Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 152–160why, one step back152 server = Server(config=config)153 task = asyncio.ensure_future(server.serve())154 try:155 while not server.started:156 await asyncio.sleep(0.0001)157 yield server158 finally:159 server.should_exit = True160 await taskParametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 161–176why, one step back161162163@pytest.fixture164def restart(backend):165 async def asyncio_restart(server):166 await server.shutdown()167 await server.startup()168169 if isinstance(backend, AsyncioBackend):170 return asyncio_restart171172 # The uvicorn server runs under asyncio, so we will need to figure out173 # how to restart it under a different I/O library.174 # This will most likely require running `asyncio_restart` in the threadpool,175 # but that might not be sufficient.176 raise NotImplementedError