#!/usr/bin/env bash # The test suite. A submission must pass every check before it is benchmarked. # # bash test/test.sh # http://127.0.0.1:3000 # bash test/test.sh http://127.0.0.1:8080 # # Run it against a server that was started on a FRESH copy of seed/feed.db: it compares exact # response bytes with test/golden/*.json, then writes one post and a few likes, so a second run # against the same database fails the golden checks. # # Needs: curl, jq, openssl. JWT_SECRET defaults to the challenge secret. set -uo pipefail BASE="${1:-http://127.0.0.1:3000}" GOLDEN="$(cd "$(dirname "$0")" && pwd)/golden" JWT_SECRET="${JWT_SECRET:-twelve-dollar-challenge}" USER_ID=1; USER_NAME=golden_ember_1 # user 1 in the seed TMP="$(mktemp -d)"; trap 'rm -rf "$TMP"' EXIT pass=0; fail=0 ok() { echo " ok $1"; pass=$((pass+1)); } bad() { echo " FAIL $1"; [ -n "${2:-}" ] && echo " $2"; fail=$((fail+1)); } # req [authorization|-] [raw-body] -> STATUS, BODY (one trailing newline stripped), CTYPE req() { local args=(-s -o "$TMP/body" -D "$TMP/headers" -w '%{http_code}' -X "$1" "$BASE$2") [ -n "${3:-}" ] && [ "$3" != "-" ] && args+=(-H "Authorization: $3") [ -n "${4+x}" ] && args+=(-H 'Content-Type: application/json' --data-binary "$4") STATUS="$(curl "${args[@]}")" BODY="$(cat "$TMP/body")" CTYPE="$(grep -i '^content-type:' "$TMP/headers" | head -1 | cut -d' ' -f2- | tr -d '\r')" } # expect