#!/bin/sh # Starts the server on a spare port, asks it for three things, and checks # every answer. Run from the repository root after a build. set -u port=$((20000 + $$ % 20000)) ./build/tiny-http "$port" & pid=$! trap 'kill "$pid" 2>/dev/null' EXIT INT TERM url="http://127.0.0.1:$port" for i in 1 2 3 4 5 6 7 8 9 10; do curl -s -o /dev/null "$url/health" && break sleep 0.1 done fail() { echo "FAIL: $*"; exit 1; } page=$(curl -s -i "$url/") || fail "GET / got no answer" echo "$page" | head -n 1 | grep -q '200 OK' || fail "GET / did not answer 200" length=$(echo "$page" | tr -d '\r' | sed -n 's/^Content-Length: //p') body=$(curl -s "$url/" | wc -c) [ "$length" = "$body" ] || fail "Content-Length says $length, body has $body bytes" echo "GET / 200, $body bytes, Content-Length matches" [ "$(curl -s "$url/health")" = "ok" ] || fail "GET /health did not say ok" echo "GET /health 200, ok" code=$(curl -s -o /dev/null -w '%{http_code}' "$url/no/such/page") [ "$code" = 404 ] || fail "GET /no/such/page answered $code, not 404" echo "GET /no/... 404" # Clients that hang up before reading must not take the server down. python3 - "$port" <<'PY' import socket, struct, sys for _ in range(20): s = socket.create_connection(("127.0.0.1", int(sys.argv[1]))) s.sendall(b"GET / HTTP/1.0\r\n\r\n") s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0)) s.close() PY [ "$(curl -s "$url/health")" = "ok" ] || fail "the server died after clients hung up" echo "20 clients hung up early; still serving" echo "smoke: 3 requests answered"