---------------------------- HTTP Server Test Plan Script ---------------------------- -Ian! D. Allen - idallen@idallen.ca Run the test script with input from /dev/null to avoid prompts between tests. Remember to capture stderr as well as stdout in the same output pipe/file. Run the output through "cat -v" to make extra CR characters visible (though the script should do that for you already). $ ./test_plan.sh &1 | cat -v >test_out.txt #!/bin/sh -u # # Syntax: $0 (no arguments) # # This script runs tests on an HTTP server in the current directory. # It creates a fake HTTP client using "nc" that feeds test HTTP request # lines to the server. # # The script waits for RETURN before every test, if stdin is a terminal. # (To run tests without prompting, redirect stdin from /dev/null ) # # - Ian! D. Allen idallen@idallen.ca # set standard shell PATH and file creation mask # PATH=/bin:/usr/bin ; export PATH umask 022 # We expect the server to be named as follows: # HTTPSERVER='java PigLatinHTTP' # how to run the HTTP server PORT=55555 # an unused port WEBDIR=/tmp # directory from which to serve files # The command line used to start the server (done once, at test start): # RUNSERVER=" $HTTPSERVER $PORT $WEBDIR " # The command line used to run the fake HTTP client: # RUNCLIENT=" nc localhost $PORT " # Global variable used to count each test # testno=0 # A temp file used as the HTTP request being sent (input to the client): # A temp file used to collect client output. # http=/tmp/$USER.http.$$ clientout=/tmp/$USER.out.$$ rm -f $http $clientout # Create a data file in $WEBDIR with two names: one text, one binary: # The HTTP server content guessing routine will produce text/plain for # the text file, which we will use to make it PigLatin. # (These files have to be in the directory given by $WEBDIR, above.) # basename=$USER-$$ name=$WEBDIR/$basename rm -f $name.txt $name.bin $name.000 cat >$name.txt <&2 " " echo 1>&2 "*** SCRIPT EXIT $0" rm -f $name.txt $name.bin $name.000 pgrep -u $USER -f -l -s 0 java # show all running java # pkill -u $USER -f -s 0 java # kill off all running java (danger) trap 0 # remove exit trap exit } # Start our HTTP server in the background. # Number all the server output to make it easier to see on the screen. # Use cat -v to make control characters (e.g. ^M) visible. # Sleep a bit, to allow time for server to actually get going. # Make sure the server actually started (kill -0). # $RUNSERVER 2>&1 | cat -v -n & serverpid=$! sleep 2 # wait a bit - java is slow to start... if ! kill -0 $serverpid 2>/dev/null ; then echo 1>&2 "$0: Unable to start a new server: $RUNSERVER" echo 1>&2 "$0: Is the server already running?" pgrep -u $USER -f -l -s 0 java exit 1 fi # This is how we actually run a test. # - pause and ask to run this test only if stdin is a tty # - start the server in background # - run the client against the server, reading from the file # DoTest () { echo " " let testno=testno+1 # If stdin is a terminal, prompt to continue with this test. # (To run tests without prompting, redirect stdin from /dev/null ) # if [ -t 0 ] ; then echo 1>&2 "==> TEST $testno. $*" echo 1>&2 -n "==> Press RETURN to execute test $testno ..." read junk echo " " fi echo "==============================================================" echo "Test $testno: $*" echo "==============================================================" # Run the fake HTTP client using input from the prepared file. # Save responses received by the client (including stderr) in a temp file. # $RUNCLIENT <$http >$clientout 2>&1 # Sleep a bit to give the server time to finish its own debug output. # Increase this if needed to keep the client and server output separate. # Output the client response lines prefaced with a prefix. # sleep 1 cat -v $clientout | sed -e 's/^/RESPONSE: /' } ########################################################################## # Tests of the HTTP server go below this line. # Each test uses a different GET line. # Our toy HTTP server only reads one line from a client. ########################################################################## echo "HEAD /$basename.bin HTTP/1.0" >$http DoTest "HEAD a file with a non-text name: $basename.bin" echo "GET /$basename.bin HTTP/1.0" >$http DoTest "GET a file with a non-text name: $basename.bin" echo "HEAD /$basename.txt HTTP/1.0" >$http DoTest "HEAD a file with a text name: $basename.txt" echo "GET /$basename.txt HTTP/1.0" >$http DoTest "GET a file with a text name: $basename.txt" echo "GET /$basename.txt HTTP/1.0" >$http DoTest "GET with extra spaces (should work)" echo "GET//$basename.txt HTTP/1.0" >$http DoTest "GET invalid syntax: missing leading blank (should fail as invalid)" echo "GET /$basename.txtHTTP/1.0" >$http DoTest "GET invalid syntax: missing trailing blank (should fail as invalid)" echo "get /$basename.bin HTTP/1.0" >$http DoTest "GET in wrong case (should fail as invalid)" echo "GET /$basename.bin http/1.0" >$http DoTest "HTTP in wrong case (should fail as invalid)" echo "GET /$basename.bin HTTP/2.0" >$http DoTest "GET with unknown version (should fail as invalid)" echo "GET /$basename.000 HTTP/1.0" >$http DoTest "GET file with no read permissions (should fail - forbidden)" echo "GET /$basename.XXX HTTP/1.0" >$http DoTest "GET wrong file name (should fail - not found)" # more tests here ... echo " "