LINE1 10 20 30 40 50 60 70123456789set -euo pipefail1011BASE="${1:-${DIAL_URL:-https://dial.silenlocatelli.ch}}"12BASE="${BASE%/}"1314read -rs -p "dial API key: " KEY15echo16if [[ -z "$KEY" ]]; then17 echo "no key entered" >&218 exit 119fi2021topic="selftest-$(date +%s)-$RANDOM"22payload="ping-$(date +%s)-$RANDOM"23failures=02425pass() { printf 'PASS %s\n' "$1"; }26fail() { printf 'FAIL %s\n' "$1"; failures=$((failures + 1)); }272829out=$(curl -sS --max-time 20 -w '\n%{http_code}' -X POST \30 -H "Authorization: Bearer $KEY" --data "$payload" \31 "$BASE/publish/$topic" || true)32code=$(printf '%s\n' "$out" | tail -n1)33body=$(printf '%s\n' "$out" | sed '$d')34id=$(printf '%s' "$body" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')35if [[ "$code" == 201 && -n "$id" ]]; then36 pass "publish with key (id $id)"37else38 fail "publish with key: HTTP $code $body"39fi404142if [[ -n "$id" ]]; then43 out=$(curl -sS --max-time 20 -w '\n%{http_code}' \44 -H "Authorization: Bearer $KEY" \45 "$BASE/subscribe/$topic?after=$((id - 1))" || true)46 code=$(printf '%s\n' "$out" | tail -n1)47 body=$(printf '%s\n' "$out" | sed '$d')48 if [[ "$code" == 200 && "$body" == *"\"payload\":\"$payload\""* ]]; then49 pass "subscribe returns the published message"50 else51 fail "subscribe: HTTP $code $body"52 fi53fi545556code=$(curl -sS --max-time 20 -o /dev/null -w '%{http_code}' -X POST \57 --data "unauthenticated" "$BASE/publish/$topic" || true)58if [[ "$code" == 401 ]]; then59 pass "publish without key is rejected (401)"60else61 fail "publish without key returned HTTP $code (expected 401)"62fi6364echo65if (( failures == 0 )); then66 echo "all checks passed against $BASE"67else68 echo "$failures check(s) failed against $BASE"69fi70exit $(( failures > 0 ))