#!/usr/bin/env bash
# Smoke test against a running `wrangler dev`. Usage: ./test.sh [base_url]
B=${1:-http://127.0.0.1:8787}
ADMIN=${ADMIN_TOKEN:-test-admin-token}
PASS=0; FAIL=0
check() { # name expected actual
  if [ "$2" = "$3" ]; then PASS=$((PASS+1)); echo "ok   $1"; else FAIL=$((FAIL+1)); echo "FAIL $1 (want $2, got $3)"; fi
}
code() { curl -s -o /tmp/resp.$$ -w '%{http_code}' "$@"; }
ok='{"name":" Ada ","email":"ada@example.org","message":"Hello","turnstileToken":"XXXX.DUMMY.TOKEN.XXXX"}'

c=$(code -X POST "$B/api/contact" -H 'Content-Type: application/json' -H 'CF-Connecting-IP: 203.0.113.7' -d "$ok"); check "valid POST -> 201" 201 "$c"
ID=$(sed -E 's/.*"id":([0-9]+).*/\1/' /tmp/resp.$$); echo "     id=$ID"
c=$(code -X POST "$B/api/contact" -d 'not json'); check "non-JSON -> 400" 400 "$c"; cat /tmp/resp.$$; echo
c=$(code -X POST "$B/api/contact" -d '[1]'); check "JSON array -> 400" 400 "$c"
c=$(code -X POST "$B/api/contact" -d '{"name":"  ","email":"bad","message":"","turnstileToken":""}'); check "invalid fields -> 400" 400 "$c"; cat /tmp/resp.$$; echo
c=$(code -X POST "$B/api/contact" -d '{"name":"a","email":"a@b.co","message":"m"}'); check "missing token -> 400" 400 "$c"
c=$(code -X POST "$B/api/contact" -d "{\"name\":\"$(printf 'a%.0s' {1..101})\",\"email\":\"a@b.co\",\"message\":\"m\",\"turnstileToken\":\"t\"}"); check "name 101 -> 400" 400 "$c"
c=$(code -X POST "$B/api/contact" -d "{\"name\":\"$(printf 'a%.0s' {1..100})\",\"email\":\"a@b.co\",\"message\":\"m\",\"turnstileToken\":\"t\"}"); check "name 100 -> 201" 201 "$c"
c=$(code -X POST "$B/api/contact" -d "{\"name\":\"n\",\"email\":\"a@b.co\",\"message\":\"$(printf 'a%.0s' {1..5001})\",\"turnstileToken\":\"t\"}"); check "message 5001 -> 400" 400 "$c"
big=$(head -c 11000 /dev/zero | tr '\0' 'a')
c=$(code -X POST "$B/api/contact" -d "{\"name\":\"n\",\"email\":\"a@b.co\",\"message\":\"$big\",\"turnstileToken\":\"t\"}"); check ">10KB -> 413" 413 "$c"
c=$(printf '{"name":"n","email":"a@b.co","message":"%s","turnstileToken":"t"}' "$big" | code -X POST "$B/api/contact" -H 'Transfer-Encoding: chunked' --data-binary @-); check ">10KB chunked -> 413" 413 "$c"
c=$(code -X GET "$B/api/contact"); check "GET collection -> 405" 405 "$c"
allow=$(curl -s -D - -o /dev/null -X PUT "$B/api/contact" | grep -i '^allow:' | tr -d '\r'); check "405 has Allow" "allow: post, options" "${allow,,}"
c=$(code "$B/nope"); check "unknown path -> 404" 404 "$c"
c=$(code -X OPTIONS "$B/api/contact" -H 'Origin: https://example.org' -H 'Access-Control-Request-Method: POST'); check "preflight -> 204" 204 "$c"
curl -s -D - -o /dev/null -X OPTIONS "$B/api/contact" -H 'Origin: https://example.org' -H 'Access-Control-Request-Method: POST' | grep -i '^access-control'
c=$(code "$B/api/contact/$ID"); check "GET no auth -> 401" 401 "$c"
c=$(code "$B/api/contact/$ID" -H 'Authorization: Bearer wrong'); check "GET wrong auth -> 401" 401 "$c"
c=$(code "$B/api/contact/999999" -H "Authorization: Bearer $ADMIN"); check "GET unknown id -> 404" 404 "$c"
c=$(code "$B/api/contact/abc" -H "Authorization: Bearer $ADMIN"); check "GET non-numeric id -> 404" 404 "$c"
c=$(code "$B/api/contact/$ID" -H "Authorization: Bearer $ADMIN"); check "GET with auth -> 200" 200 "$c"; cat /tmp/resp.$$; echo
ct=$(curl -s -D - -o /dev/null "$B/nope" | grep -i '^content-type' | tr -d '\r'); echo "     $ct"
rm -f /tmp/resp.$$
echo "passed=$PASS failed=$FAIL"; [ $FAIL -eq 0 ]
