cat / nz
clone

push a directory, get a url: sh + one Cloudflare Worker + KV

main
nz / test.sh
test.sh 3.5K executable
1#!/bin/sh2# End-to-end test: worker.js under `wrangler dev`, driven by ./nz (or NZ="pwsh ./nz.ps1"). Needs wrangler3# (WRANGLER=path, default `npx wrangler`). Routes make wrangler dev rewrite the4# Host header, so this runs from a routeless copy of the config.5set -u6cd "$(dirname "$0")"7NZ=${NZ:-./nz}8port=${PORT:-8787} t=$(mktemp -d) fails=09trap 'kill $pid 2>/dev/null; rm -rf "$t"' EXIT1011sed '/"routes"/,/\]/d' wrangler.jsonc | sed "s|\"main\": \"worker.js\"|\"main\": \"$PWD/worker.js\"|" > "$t/wrangler.jsonc"12${WRANGLER:-npx wrangler} dev -c "$t/wrangler.jsonc" --port "$port" --persist-to "$t/state" \13 --var "DOMAIN:localhost:$port" --var TOKEN:test > "$t/log" 2>&1 &14pid=$!15for _ in $(seq 60); do curl -s -o /dev/null "localhost:$port" && break; sleep 1; done1617export NZ_HOME=$t/home NZ_SERVER=http://localhost:$port NZ_TOKEN=test18is() { if [ "$2" = "$3" ]; then echo "ok $1"; else echo "FAIL $1: got '$2' want '$3'"; fails=$((fails + 1)); fi; }19get() { curl -s "$@"; }20code() { curl -s -o /dev/null -w '%{http_code}' "$@"; }21idof() { echo "$1" | sed 's|^.*//||; s|\..*$||'; }2223s=$t/site24mkdir -p "$s/sub/.git" "$s/node_modules"25echo '<h1>v1</h1>' > "$s/index.html"; echo 'body{}' > "$s/style.css"; echo sub > "$s/sub/index.html"26echo x > "$s/sub/.git/x"; echo x > "$s/node_modules/y"; echo x > "$s/.env"2728u=$($NZ push "$s" 2>/dev/null)29is 'push prints a site url' "$(echo "$u" | grep -c "^http://[a-z0-9]\{5\}\.localhost:$port/$")" 130is 'serves index' "$(get "$u")" '<h1>v1</h1>'31is 'serves assets with a type' "$(curl -s -o /dev/null -w '%{content_type}' "${u}style.css")" 'text/css; charset=utf-8'32is 'directory redirects to slash' "$(code "${u}sub")" 30833is 'serves nested index' "$(get "${u}sub/")" sub34is 'skips dot dirs' "$(code "${u}sub/.git/x")" 40435is 'skips dotfiles' "$(code "${u}.env")" 40436is 'skips node_modules' "$(code "${u}node_modules/y")" 40437is 'default ttl is 14d' "$($NZ update "$(idof "$u")" 2>&1 >/dev/null | grep -c expires)" 13839echo '<h1>v2</h1>' > "$s/index.html"40is 'push again reuses the url' "$($NZ push "$s" --ttl 1h 2>/dev/null)" "$u"41is 'and replaces content' "$(get "$u")" '<h1>v2</h1>'42is '--new makes another url' "$($NZ push "$s" --new 2>/dev/null | grep -c "$u")" 04344echo '<b>report</b>' > "$t/report.html"45r=$($NZ push "$t/report.html" --password meow 2>/dev/null)46is 'password locks the site' "$(code "$r")" 40147is 'wrong password is refused' "$(code -X POST -d password=nope "$r")" 40148cookie=$(curl -s -D - -o /dev/null -X POST -d password=meow "$r" | sed -n 's/^set-cookie: \(__Host-nz=[^;]*\).*/\1/ip')49is 'right password unlocks' "$(get -H "cookie: $cookie" "$r")" '<b>report</b>'50$NZ update "$(idof "$r")" --password purr 2>/dev/null >&251is 'new password revokes cookies' "$(code -H "cookie: $cookie" "$r")" 40152is 'update keeps content' "$(get -H "cookie: $(curl -s -D - -o /dev/null -X POST -d password=purr "$r" | sed -n 's/^set-cookie: \(__Host-nz=[^;]*\).*/\1/ip')" "$r")" '<b>report</b>'53is 'no-password + never' "$($NZ update "$(idof "$r")" --no-password --ttl never 2>&1 >/dev/null)" 'never expires'54is 'is public again' "$(get "$r")" '<b>report</b>'5556is 'bad ttl is refused' "$($NZ push "$s" --ttl 3x 2>&1)" 'nz: ttl must look like 30m, 12h, 14d, 2w or never'57is 'bad token is refused' "$(NZ_TOKEN=nope $NZ push "$s" 2>&1)" 'nz: unauthorized'58is 'unknown id' "$($NZ update zzzzz 2>&1)" 'nz: zzzzz not found (expired?)'59is 'not a tar' "$(curl -s -X POST -H 'authorization: Bearer test' --data-binary hello "$NZ_SERVER/")" 'body must be a ustar archive with at least one file'6061[ $fails = 0 ] && echo "all passed" || { echo "$fails failed"; exit 1; }