53 lines
2.1 KiB
Python
53 lines
2.1 KiB
Python
"""在验收容器的回环接口验证已脱离开发包的 HTTP/TLS 产物。"""
|
|
|
|
import http.client
|
|
import json
|
|
from pathlib import Path
|
|
import socket
|
|
import ssl
|
|
import subprocess
|
|
import time
|
|
|
|
|
|
root = Path("/work/http")
|
|
(root / "config").mkdir(exist_ok=True)
|
|
certificates = Path("/work/tls")
|
|
for encrypted in (False, True):
|
|
with socket.socket() as listener:
|
|
listener.bind(("127.0.0.1", 0))
|
|
port = listener.getsockname()[1]
|
|
settings = {"http": {"host": "127.0.0.1", "port": port},
|
|
"sites": {"public": {"path": "", "hosts": ["localhost"]}}}
|
|
if encrypted:
|
|
settings["http"]["tls"] = {"certificate": str(certificates / "server.pem"),
|
|
"key": str(certificates / "server-key.pem")}
|
|
(root / "config/setting.json").write_text(json.dumps(settings))
|
|
with (root / f"server-{encrypted}.log").open("w") as log:
|
|
server = subprocess.Popen([str(root / "app")], stdout=log, stderr=log)
|
|
try:
|
|
deadline = time.monotonic() + 10
|
|
while True:
|
|
assert server.poll() is None, (root / f"server-{encrypted}.log").read_text()
|
|
try:
|
|
with socket.create_connection(("127.0.0.1", port), timeout=1):
|
|
break
|
|
except ConnectionRefusedError:
|
|
assert time.monotonic() < deadline, "HTTP server startup timed out"
|
|
time.sleep(0.05)
|
|
if encrypted:
|
|
context = ssl.create_default_context(cafile=str(certificates / "root.pem"))
|
|
client = http.client.HTTPSConnection("localhost", port, context=context, timeout=5)
|
|
else:
|
|
client = http.client.HTTPConnection("localhost", port, timeout=5)
|
|
try:
|
|
client.request("GET", "/demo/greeting/greet?name=Dever")
|
|
response = client.getresponse()
|
|
assert response.status == 200, response.status
|
|
assert json.load(response)["data"] == "Hello, Dever"
|
|
finally:
|
|
client.close()
|
|
finally:
|
|
server.terminate()
|
|
server.wait(timeout=10)
|
|
print("HTTP_TLS_PASS")
|