debian-forge/test/mod/test_testutil_net.py
Michael Vogt e535877798 test: add new https_serve_directory() and test certs
This commit adds a new `https_serve_directory()` test helper
and some custom self-signed and worthless certs that are used
during testing. They are not dynamically generated to avoid the
extra compuation time during tests (but they could be).

Generated via:
```
$ openssl req -new -newkey rsa:2048  -nodes -x509  \
   -subj "/C=DE/ST=Berlin/L=Berlin/O=Org/CN=localhost"   \
   -keyout "key1.pem" -out "cert1.pem"
```

This will allow us to test `https` download URLs as well in e.g.
the curl source.
2024-07-29 16:51:48 +02:00

39 lines
1.3 KiB
Python

import os.path
import pathlib
import subprocess
from osbuild.testutil import make_fake_tree
from osbuild.testutil.net import http_serve_directory, https_serve_directory
def test_http_serve_directory_smoke(tmp_path):
make_fake_tree(tmp_path, {
"file1": "file1 content",
"dir1/file2": "file2 content",
})
with http_serve_directory(tmp_path) as httpd:
output = subprocess.check_output(
["curl", f"http://localhost:{httpd.server_port}/file1"])
assert output == b"file1 content"
output = subprocess.check_output(
["curl", f"http://localhost:{httpd.server_port}/dir1/file2"])
assert output == b"file2 content"
def test_https_serve_directory_smoke(tmp_path):
make_fake_tree(tmp_path, {
"file1": "file1 content",
})
cert_dir = pathlib.Path(__file__).parent.parent / "data/certs"
cacertfile = cert_dir / "cert1.pem"
assert cacertfile.exists()
keyfile = cert_dir / "key1.pem"
assert keyfile.exists()
with https_serve_directory(tmp_path, cacertfile, keyfile) as httpd:
output = subprocess.check_output(
["curl",
"--cacert", os.fspath(cacertfile),
f"https://localhost:{httpd.server_port}/file1"],
)
assert output == b"file1 content"