conftest.py
This file
Path
tests/conftest.py
152 lines · 3.8 KiB · Python
Last changed by
Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
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–17why, one step back10from uvicorn.config import Config11from uvicorn.main import Server121314async def app(scope, receive, send):15 assert scope["type"] == "http"16 if scope["path"] == "/slow_response":17 await slow_response(scope, receive, send)Merge pull request #39 from matusf/29-add-raise_for_statusTom Christiebefore adoption5c1cb30
lines 18–19why, one step back18 elif scope["path"].startswith("/status"):19 await status_code(scope, receive, send)Add 100-continue test (#68)Tom Christiebefore adoption4fe6bc0
lines 20–21why, one step back20 elif scope["path"].startswith("/echo_body"):21 await echo_body(scope, receive, send)Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 22–37why, one step back22 else:23 await hello_world(scope, receive, send)242526async def hello_world(scope, receive, send):27 await send(28 {29 "type": "http.response.start",30 "status": 200,31 "headers": [[b"content-type", b"text/plain"]],32 }33 )34 await send({"type": "http.response.body", "body": b"Hello, world!"})353637async def slow_response(scope, receive, send):Version 0.6.4 (#105)Tom Christiebefore adoption5326cd1
line 38why, one step back38 await asyncio.sleep(0.1)Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 39–42why, one step back39 await send(40 {41 "type": "http.response.start",42 "status": 200,Merge pull request #39 from matusf/29-add-raise_for_statusTom Christiebefore adoption5c1cb30
lines 43–54why, one step back43 "headers": [[b"content-type", b"text/plain"]],44 }45 )46 await send({"type": "http.response.body", "body": b"Hello, world!"})474849async def status_code(scope, receive, send):50 status_code = int(scope["path"].replace("/status/", ""))51 await send(52 {53 "type": "http.response.start",54 "status": status_code,Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 55–58why, one step back55 "headers": [[b"content-type", b"text/plain"]],56 }57 )58 await send({"type": "http.response.body", "body": b"Hello, world!"})Add 100-continue test (#68)Tom Christiebefore adoption4fe6bc0
lines 59–61why, one step back596061async def echo_body(scope, receive, send):Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 62why, one step back62 body = b""Add 100-continue test (#68)Tom Christiebefore adoption4fe6bc0
lines 63–66why, one step back63 more_body = True6465 while more_body:66 message = await receive()Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
lines 67–68why, one step back67 body += message.get("body", b"")68 more_body = message.get("more_body", False)Add 100-continue test (#68)Tom Christiebefore adoption4fe6bc0
lines 69–77why, one step back6970 await send(71 {72 "type": "http.response.start",73 "status": 200,74 "headers": [[b"content-type", b"text/plain"]],75 }76 )77 await send({"type": "http.response.body", "body": body})Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 78–79why, one step back7879Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
lines 80–95why, one step back80class CAWithPKEncryption(trustme.CA):81 """Implementation of trustme.CA() that can emit82 private keys that are encrypted with a password.83 """8485 @property86 def encrypted_private_key_pem(self):87 return trustme.Blob(88 self._private_key.private_bytes(89 Encoding.PEM,90 PrivateFormat.TraditionalOpenSSL,91 BestAvailableEncryption(password=b"password"),92 )93 )9495Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
line 96why, one step back96@pytest.fixtureSupport client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
lines 97–98why, one step back97def example_cert():98 ca = CAWithPKEncryption()Merge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8
line 99why, one step back99 ca.issue_cert("example.org")Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
line 100why, one step back100 return caMerge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8
lines 101–103why, one step back101102103@pytest.fixtureSupport client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
lines 104–121why, one step back104def cert_pem_file(example_cert):105 with example_cert.cert_pem.tempfile() as tmp:106 yield tmp107108109@pytest.fixture110def cert_private_key_file(example_cert):111 with example_cert.private_key_pem.tempfile() as tmp:112 yield tmp113114115@pytest.fixture116def cert_encrypted_private_key_file(example_cert):117 with example_cert.encrypted_private_key_pem.tempfile() as tmp:118 yield tmp119120121@pytest.fixtureCarefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 122–123why, one step back122async def server():123 config = Config(app=app, lifespan="off")Merge pull request #57 from yeraydiazdiaz/test-httpsTom Christiebefore adoptionc27c635
lines 124–135why, one step back124 server = Server(config=config)125 task = asyncio.ensure_future(server.serve())126 try:127 while not server.started:128 await asyncio.sleep(0.0001)129 yield server130 finally:131 server.should_exit = True132 await task133134135@pytest.fixtureSupport client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
line 136why, one step back136async def https_server(cert_pem_file, cert_private_key_file):Merge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8
line 137why, one step back137 config = Config(Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29
lines 138–142why, one step back138 app=app,139 lifespan="off",140 ssl_certfile=cert_pem_file,141 ssl_keyfile=cert_private_key_file,142 port=8001,Merge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8
line 143why, one step back143 )Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 144–152why, one step back144 server = Server(config=config)145 task = asyncio.ensure_future(server.serve())146 try:147 while not server.started:148 await asyncio.sleep(0.0001)149 yield server150 finally:151 server.should_exit = True152 await task