1#!/bin/sh -e 2 3set -e 4 5TMPFILE="out.$$" 6URLFILE="urls.$$" 7# TRACEFILE="/dev/tty" 8 9# create temp files 10# shellcheck disable=SC2064 11rm -f $TMPFILE $URLFILE && touch $TMPFILE && 12trap "rm ./$TMPFILE ./$URLFILE" 0 || exit 2 13echo "/over/the/rainbow /over/the/rainbow" > $URLFILE 14 15# build up the command line 16# for mawk, add -Wi or -W interactive to avoid buffered read on fifo 17overrides="-v urlfile=$URLFILE -f add-urls.awk -v tracefile=$TRACEFILE" 18command="awk -f ./http-redirect.awk $overrides" 19 20awk -f ./test.awk -v "resultfile=$TMPFILE" -v "tracefile=$TRACEFILE" 21 22# remember the CR in your expect 23 24test="1 test absolute URI" 25expect='^location: https://somewhere.example.com/over/the/rainbow.$' 26$command << HERE > $TMPFILE 27GET http://somewhere.example.com/over/the/rainbow HTTP/1.1 28Host: elsewhere.example 29 30HERE 31 32if grep -is "$expect" $TMPFILE 33then 34 echo "PASS $test" 35else 36 echo "FAIL $test" 37 echo "Expected to find >'$expect'< in :" 38 cat $TMPFILE 39 false 40fi 41 42test="2 Test no absolute-path in URI" 43expect='^location: https://somewhere.example/.$' 44$command << HERE > $TMPFILE 45GET http://somewhere.example HTTP/1.1 46Host: elsewhere.example 47 48HERE 49 50if grep -is "$expect" $TMPFILE 51then 52 echo "PASS $test" 53else 54 echo "FAIL $test" 55 echo "Expected to find >'$expect'< in :" 56 cat $TMPFILE 57 false 58fi 59 60 61test="3 test generic 1.1 client" 62expect='^location: https://elsewhere.example/over/the/rainbow.$' 63$command << HERE > $TMPFILE 64GET /over/the/rainbow HTTP/1.1 65Host: elsewhere.example 66 67HERE 68 69if grep -is "$expect" $TMPFILE 70then 71 echo "PASS $test" 72else 73 echo "FAIL $test" 74 echo "Expected to find >'$expect'< in :" 75 cat $TMPFILE 76 false 77fi 78 79 80 81test="4 test generic 1.1 client" 82expect='^location: https://somewhere.com/over/the/rainbow.$' 83$command << HERE > $TMPFILE 84GET /over/the/rainbow HTTP/1.1 85not-host: elsewhere.example 86x-host: elsewhere.example.com 87 ( comment ) 88host: somewhere.com 89host2: else.where.example.com 90 91HERE 92 93 94if grep -is "$expect" $TMPFILE 95then 96 echo "PASS $test" 97else 98 echo "FAIL $test" 99 echo "Expected to find >'$expect'< in :" 100 cat $TMPFILE 101 false 102fi 103 104 105 106echo all tests passed 107