forked from mtusi2026/mtusi2026lab4
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
import http.client
|
|
import json
|
|
import sys
|
|
import time
|
|
|
|
status = int(sys.argv[1])
|
|
path = {502: "/bad", 503: "/none", 504: "/slow"}[status]
|
|
if status != 503:
|
|
for _ in range(60):
|
|
conn = http.client.HTTPConnection("lb", 8080, timeout=2)
|
|
conn.request("GET", "/health/ready")
|
|
response = conn.getresponse()
|
|
response.read()
|
|
conn.close()
|
|
if response.status == 200:
|
|
break
|
|
time.sleep(1)
|
|
else:
|
|
raise RuntimeError("Backend never became ready")
|
|
else:
|
|
time.sleep(5)
|
|
for value in ['a"b', "a\\b", "a\tb", "x" * 200]:
|
|
conn = http.client.HTTPConnection("lb", 8080, timeout=20)
|
|
start = time.monotonic()
|
|
conn.request("GET", path, headers={"X-Request-Id": value})
|
|
response = conn.getresponse()
|
|
payload = json.loads(response.read())
|
|
assert response.status == status, response.status
|
|
assert response.getheader("X-Request-Id") == value
|
|
assert payload["error"]["request_id"] == value
|
|
print(
|
|
json.dumps(
|
|
{
|
|
"status": status,
|
|
"request_id": value,
|
|
"json_valid": True,
|
|
"echo_matches": True,
|
|
"seconds": round(time.monotonic() - start, 3),
|
|
}
|
|
),
|
|
flush=True,
|
|
)
|
|
conn.close()
|