test: add a real HTTP-over-TCP loopback domain test
Implement marqdo-ext-http#3: the explicit @http package now owns a real
loopback domain test instead of only pure encode/parse cases.
- tests/loopback.mq.md performs two real HTTP/1.1 exchanges in one program
through the CLI's generic resource.open("tcp")/read/write/close, building
requests with http.构建请求字节 and parsing responses with
http.解析响应字节; non-ASCII bodies, explicit --allow-net 127.0.0.1 and
--allow-env HTTP_TEST_PORT only.
- scripts/loopback_server.py is a deterministic one-request-per-connection
server that records method/path/body and returns fixed status/bodies.
- scripts/run_cases.py supports kind=loopback (server lifecycle, port env,
grants, request-record assertions) without changing the pure case path.
- tests/cases/loopback.json asserts GET /one -> 200 第一个 and
POST /two -> 201 第二个 plus the request records.
- Evidence: docs/evidence-2026-11-30-loopback-domain-test.md (real CLI rc.6
candidate + Bundle 0.3.0: 2 cases PASS).
No CLI/Host/other-component change, no new Host request, no release.
This commit is contained in:
1 parent
8fe0bee24c
commit
a604405908
5 files changed
+397
-1
No files matched your search
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Deterministic loopback HTTP server for ext-http domain tests.
|
||||
|
||||
Accepts exactly N connections (one request each), reads the request head and a
|
||||
Content-Length body, records method/path/headers/body, answers with a fixed
|
||||
HTTP/1.1 response and closes the connection. Prints the ephemeral port on
|
||||
stdout as one line so the test runner can pass it to the program.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import socket
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
RESPONSES = {
|
||||
"/one": ("第一个", 200, "OK"),
|
||||
"/two": ("第二个", 201, "Created"),
|
||||
}
|
||||
|
||||
|
||||
def read_request(conn: socket.socket) -> dict:
|
||||
data = b""
|
||||
while b"\r\n\r\n" not in data:
|
||||
chunk = conn.recv(4096)
|
||||
if not chunk:
|
||||
break
|
||||
data += chunk
|
||||
head, _, rest = data.partition(b"\r\n\r\n")
|
||||
lines = head.decode("utf-8", errors="replace").split("\r\n")
|
||||
method, path, _ = (lines[0].split(" ") + ["", ""])[:3]
|
||||
headers = {}
|
||||
for line in lines[1:]:
|
||||
if ":" in line:
|
||||
name, value = line.split(":", 1)
|
||||
headers[name.strip().lower()] = value.strip()
|
||||
length = int(headers.get("content-length", "0"))
|
||||
body = rest
|
||||
while len(body) < length:
|
||||
chunk = conn.recv(4096)
|
||||
if not chunk:
|
||||
break
|
||||
body += chunk
|
||||
return {
|
||||
"method": method,
|
||||
"path": path,
|
||||
"headers": headers,
|
||||
"body": body.decode("utf-8", errors="replace"),
|
||||
}
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if len(sys.argv) != 3:
|
||||
print("usage: loopback_server.py RECORD_PATH REQUEST_COUNT", file=sys.stderr)
|
||||
return 2
|
||||
record_path = Path(sys.argv[1])
|
||||
count = int(sys.argv[2])
|
||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
server.bind(("127.0.0.1", 0))
|
||||
server.listen(4)
|
||||
port = server.getsockname()[1]
|
||||
print(port, flush=True)
|
||||
records = []
|
||||
try:
|
||||
for _ in range(count):
|
||||
conn, _ = server.accept()
|
||||
with conn:
|
||||
request = read_request(conn)
|
||||
records.append(request)
|
||||
body, status, reason = RESPONSES.get(
|
||||
request["path"], ("unknown", 404, "Not Found")
|
||||
)
|
||||
payload = body.encode("utf-8")
|
||||
response = (
|
||||
f"HTTP/1.1 {status} {reason}\r\n"
|
||||
"Content-Type: text/plain; charset=utf-8\r\n"
|
||||
f"Content-Length: {len(payload)}\r\n"
|
||||
"Connection: close\r\n\r\n"
|
||||
).encode("utf-8") + payload
|
||||
conn.sendall(response)
|
||||
finally:
|
||||
server.close()
|
||||
record_path.write_text(json.dumps(records, ensure_ascii=False, indent=2))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user