Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 51 additions & 9 deletions scripts/validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -226,21 +226,38 @@ fail_with_link() {
FAIL=$((FAIL + 1))
}

dump_debug() {
local trace="$1"
local response="$2"
if [ -n "$trace" ] && [ -s "$trace" ]; then
echo " ─── wire trace ───"
sed 's/^/ /' "$trace"
fi
if [ -n "$response" ]; then
echo " ─── response ───"
printf '%s\n' "$response" | sed 's/^/ /'
fi
[ -n "$trace" ] && rm -f "$trace"
}

check() {
local label="$1"
local expected_body="$2"
local docs_url="$3"
shift 3
local response
response=$(curl -s --max-time 30 -D- "$@" || true)
local response trace
trace=$(mktemp)
response=$(curl -s --max-time 30 -D- --trace-ascii "$trace" "$@" || true)
local body
body=$(echo "$response" | tail -1)

if [ "$body" = "$expected_body" ]; then
echo " PASS [$label]"
PASS=$((PASS + 1))
rm -f "$trace"
else
fail_with_link "[$label]: expected body '$expected_body', got '$body'" "$docs_url"
dump_debug "$trace" "$response"
fi
}

Expand All @@ -249,14 +266,22 @@ check_status() {
local expected_status="$2"
local docs_url="$3"
shift 3
local http_code
http_code=$(curl -s --max-time 30 -o /dev/null -w '%{http_code}' "$@" || true)
local http_code trace body_file
trace=$(mktemp)
body_file=$(mktemp)
http_code=$(curl -s --max-time 30 -o "$body_file" -D "$body_file.hdr" -w '%{http_code}' --trace-ascii "$trace" "$@" || true)

if [ "$http_code" = "$expected_status" ]; then
echo " PASS [$label] (HTTP $http_code)"
PASS=$((PASS + 1))
rm -f "$trace" "$body_file" "$body_file.hdr"
else
fail_with_link "[$label]: expected HTTP $expected_status, got HTTP $http_code" "$docs_url"
local response=""
[ -s "$body_file.hdr" ] && response=$(cat "$body_file.hdr")
[ -s "$body_file" ] && response="${response}$(cat "$body_file")"
dump_debug "$trace" "$response"
rm -f "$body_file" "$body_file.hdr"
fi
}

Expand All @@ -272,15 +297,20 @@ check_fragmented() {
local expected_body="$2"
local docs_url="$3"
shift 3
local body
body=$(PORT="$PORT" python3 -c '
local body trace
trace=$(mktemp)
body=$(PORT="$PORT" TRACE="$trace" python3 -c '
import os, socket, sys, time
port = int(os.environ["PORT"])
trace_path = os.environ.get("TRACE", "")
frags = sys.argv[1:]
s = socket.create_connection(("localhost", port), timeout=5)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) # no Nagle coalescing
sent = b""
for i, f in enumerate(frags):
s.sendall(f.encode("latin-1"))
data = f.encode("latin-1")
s.sendall(data)
sent += data
if i < len(frags) - 1:
time.sleep(0.03)
buf = b""
Expand All @@ -289,6 +319,13 @@ while True:
if not chunk: break
buf += chunk
s.close()
if trace_path:
with open(trace_path, "w") as tf:
tf.write("=> Send (" + str(len(sent)) + " bytes across " + str(len(frags)) + " fragment(s))\n")
tf.write(sent.decode("latin-1", errors="replace"))
tf.write("\n<= Recv (" + str(len(buf)) + " bytes)\n")
tf.write(buf.decode("latin-1", errors="replace"))
tf.write("\n")
resp = buf.decode("latin-1", errors="replace")
try:
head, raw = resp.split("\r\n\r\n", 1)
Expand Down Expand Up @@ -333,8 +370,10 @@ sys.stdout.write(body.strip())
if [ "$body" = "$expected_body" ]; then
echo " PASS [$label]"
PASS=$((PASS + 1))
rm -f "$trace"
else
fail_with_link "[$label]: expected body '$expected_body', got '$body'" "$docs_url"
dump_debug "$trace" ""
fi
}

Expand All @@ -344,8 +383,9 @@ check_header() {
local expected_value="$3"
local docs_url="$4"
shift 4
local headers
headers=$(curl -s --max-time 30 -D- -o /dev/null "$@" || true)
local headers trace
trace=$(mktemp)
headers=$(curl -s --max-time 30 -D- -o /dev/null --trace-ascii "$trace" "$@" || true)
local value
value=$(echo "$headers" | grep -i "^${header_name}:" | sed 's/^[^:]*: *//' | tr -d '\r' || true)

Expand All @@ -356,8 +396,10 @@ check_header() {
if [ "$value" = "$expected_value" ] || [[ "$value" == "$expected_value;"* ]] || [ "$norm_value" = "$norm_expected" ] || [[ "$norm_value" == "$norm_expected;"* ]]; then
echo " PASS [$label] ($header_name: $value)"
PASS=$((PASS + 1))
rm -f "$trace"
else
fail_with_link "[$label]: expected $header_name '$expected_value', got '$value'" "$docs_url"
dump_debug "$trace" "$headers"
fi
}

Expand Down