Skip to content

Commit abec2e8

Browse files
Merge pull request #61 from yeraydiazdiaz/test-config
Test config
2 parents 8f4abe0 + 8d53257 commit abec2e8

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

scripts/test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ fi
99
set -x
1010

1111
PYTHONPATH=. ${PREFIX}pytest --ignore venv --cov tests --cov ${PACKAGE} --cov-report= ${@}
12-
${PREFIX}coverage report
12+
${PREFIX}coverage report -m

tests/conftest.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ async def status_code(scope, receive, send):
5151
await send({"type": "http.response.body", "body": b"Hello, world!"})
5252

5353

54+
@pytest.fixture
55+
def 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_path
60+
61+
5462
@pytest.fixture
5563
async def server():
5664
config = Config(app=app, lifespan="off")
@@ -66,17 +74,11 @@ async def server():
6674

6775

6876
@pytest.fixture
69-
async def https_server():
70-
ca = trustme.CA()
71-
server_cert = ca.issue_cert("example.org")
72-
with ca.cert_pem.tempfile() as cert_temp_path, ca.private_key_pem.tempfile() as key_temp_path:
73-
config = Config(
74-
app=app,
75-
lifespan="off",
76-
ssl_certfile=cert_temp_path,
77-
ssl_keyfile=key_temp_path,
78-
port=8001,
79-
)
77+
async def https_server(cert_and_key_paths):
78+
cert_path, key_path = cert_and_key_paths
79+
config = Config(
80+
app=app, lifespan="off", ssl_certfile=cert_path, ssl_keyfile=key_path, port=8001
81+
)
8082
server = Server(config=config)
8183
task = asyncio.ensure_future(server.serve())
8284
try:

tests/test_config.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import ssl
23

34
import pytest
@@ -12,6 +13,44 @@ async def test_load_ssl_config():
1213
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
1314

1415

16+
@pytest.mark.asyncio
17+
async def test_load_ssl_config_verify_non_existing_path():
18+
ssl_config = httpcore.SSLConfig(verify="/path/to/nowhere")
19+
with pytest.raises(IOError):
20+
await ssl_config.load_ssl_context()
21+
22+
23+
@pytest.mark.asyncio
24+
async def test_load_ssl_config_verify_existing_file():
25+
ssl_config = httpcore.SSLConfig(verify=httpcore.config.DEFAULT_CA_BUNDLE_PATH)
26+
context = await ssl_config.load_ssl_context()
27+
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
28+
29+
30+
@pytest.mark.asyncio
31+
async def test_load_ssl_config_verify_directory():
32+
path = os.path.dirname(httpcore.config.DEFAULT_CA_BUNDLE_PATH)
33+
ssl_config = httpcore.SSLConfig(verify=path)
34+
context = await ssl_config.load_ssl_context()
35+
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
36+
37+
38+
@pytest.mark.asyncio
39+
async def test_load_ssl_config_cert_and_key(cert_and_key_paths):
40+
cert_path, key_path = cert_and_key_paths
41+
ssl_config = httpcore.SSLConfig(cert=(cert_path, key_path))
42+
context = await ssl_config.load_ssl_context()
43+
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
44+
45+
46+
@pytest.mark.asyncio
47+
async def test_load_ssl_config_cert_without_key_raises(cert_and_key_paths):
48+
cert_path, _ = cert_and_key_paths
49+
ssl_config = httpcore.SSLConfig(cert=cert_path)
50+
with pytest.raises(ssl.SSLError):
51+
await ssl_config.load_ssl_context()
52+
53+
1554
@pytest.mark.asyncio
1655
async def test_load_ssl_config_no_verify(verify=False):
1756
ssl_config = httpcore.SSLConfig(verify=False)

0 commit comments

Comments
 (0)