httpd.sh (1001B)
1 #!/bin/sh 2 3 set -eu 4 5 PASS=0 6 FAIL=0 7 8 HTTP_URL=${HTTP_URL:-http://127.0.0.1:8000} 9 UPLOAD="$HTTP_URL/default/upload.cgi" 10 11 TMPDIR=${TMPDIR:-/tmp} 12 13 fixture() { 14 file="$TMPDIR/upload-test.txt" 15 16 printf '%s\n' "hello upload test" > "$file" 17 18 printf '%s\n' "$file" 19 } 20 21 check() { 22 name=$1 23 expected=$2 24 shift 2 25 26 printf '%-35s' "$name" 27 28 output=$("$@" 2>&1 || true) 29 30 if printf '%s\n' "$output" | grep -q "$expected"; then 31 printf '%s\n' "ok" 32 PASS=$((PASS + 1)) 33 else 34 printf '%s\n' "failed" 35 printf ' expected: %s\n' "$expected" 36 printf '%s\n' "$output" 37 FAIL=$((FAIL + 1)) 38 fi 39 } 40 41 test_upload() { 42 file=$1 43 44 curl -s -i \ 45 -F "file=@$file" \ 46 "$UPLOAD" 47 } 48 49 main() { 50 file=$(fixture) 51 52 check \ 53 "upload through httpd" \ 54 "303 See Other" \ 55 test_upload \ 56 "$file" 57 58 check \ 59 "redirect location" \ 60 "Location: list.cgi" \ 61 test_upload \ 62 "$file" 63 64 rm -f "$file" 65 66 printf '\n%s passed, %s failed\n' "$PASS" "$FAIL" 67 68 [ "$FAIL" -eq 0 ] 69 } 70 71 main "$@"