Skip to content
Myr

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 file

Take 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 back
1import asyncio
23import pytest

Merge pull request #57 from yeraydiazdiaz/test-httpsTom Christiebefore adoptionc27c635

line 4why, one step back
4import trustme

Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29

lines 5–9why, one step back
5from cryptography.hazmat.primitives.serialization import (
6 BestAvailableEncryption,7 Encoding,8 PrivateFormat,9)

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 10–12why, one step back
10from uvicorn.config import Config
11from uvicorn.main import Server12

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 13why, one step back
13from httpx import AsyncioBackend

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

line 14why, one step back
14

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 15–21why, one step back
15
16@pytest.fixture(params=[pytest.param(AsyncioBackend, marks=pytest.mark.asyncio)])17def backend(request):18 backend_cls = request.param19 return backend_cls()2021

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 22–25why, one step back
22async 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 back
26 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 back
28 elif scope["path"].startswith("/echo_body"):
29 await echo_body(scope, receive, send)

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 30–45why, one step back
30 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 back
46 await asyncio.sleep(0.1)

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 47–50why, one step back
47 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 back
51 "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 back
63 "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 back
67
6869async def echo_body(scope, receive, send):

Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885

line 70why, one step back
70 body = b""

Add 100-continue test (#68)Tom Christiebefore adoption4fe6bc0

lines 71–74why, one step back
71 more_body = True
7273 while more_body:74 message = await receive()

Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885

lines 75–76why, one step back
75 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 back
77
78 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 back
86
87

Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29

lines 88–103why, one step back
88class 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 )102103

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

line 104why, one step back
104@pytest.fixture

Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29

lines 105–106why, one step back
105def example_cert():
106 ca = CAWithPKEncryption()

Merge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8

line 107why, one step back
107 ca.issue_cert("example.org")

Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29

line 108why, one step back
108 return ca

Merge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8

lines 109–111why, one step back
109
110111@pytest.fixture

Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29

lines 112–129why, one step back
112def 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.fixture

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 130–131why, one step back
130async def server():
131 config = Config(app=app, lifespan="off")

Merge pull request #57 from yeraydiazdiaz/test-httpsTom Christiebefore adoptionc27c635

lines 132–143why, one step back
132 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.fixture

Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29

line 144why, one step back
144async def https_server(cert_pem_file, cert_private_key_file):

Merge pull request #61 from yeraydiazdiaz/test-configTom Christiebefore adoptionabec2e8

line 145why, one step back
145 config = Config(

Support client cert passwords, new TLS options (#118)Seth Michael Larsonbefore adoptionba83e29

lines 146–150why, one step back
146 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 back
151 )

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 152–160why, one step back
152 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 task

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 161–176why, one step back
161
162163@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