From dd8e62bdaba0f887938c79affcafc51b5c39676f Mon Sep 17 00:00:00 2001 From: Tijmen van Nesselrooij Date: Sun, 16 Jun 2019 21:33:03 +0200 Subject: [PATCH] Integration test added --- test/integrationtest.sh | 83 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 test/integrationtest.sh diff --git a/test/integrationtest.sh b/test/integrationtest.sh new file mode 100755 index 0000000..65e731a --- /dev/null +++ b/test/integrationtest.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +serverBinary="server.out" +serverWorkingDirectory="../bin" +serverWwwRoot="$serverWorkingDirectory/www" + +function KillServer() +{ + echo "Stopping server..." + killall "$serverBinary" +} + +function StartServer() +{ + echo "Starting server..." + oldPwd=$PWD + until pgrep "$serverBinary"; do + cd "$serverWorkingDirectory" && ./$serverBinary & + sleep 2 + done + cd "$oldPwd" +} + +function TestGetHeader() +{ + curlHeaderOutputFile="./temp" + if curl -I $1 --output $curlHeaderOutputFile 2> /dev/null; then + + regexPattern="Content\-Type: $2" + if egrep -o "$regexPattern" $curlHeaderOutputFile; then + echo "SUCCESS GET headers for $1 has correct MIME type" + else + echo "FAILURE GET headers for $1 contains bad MIME type" + echo "expected Content-Type: $2" + echo "actual $(egrep -o \"$regexPattern\" $curlHeaderOutputFile)" + fi + + rm $curlHeaderOutputFile + else + echo "FAILURE Cannot GET headers for $1" + fi +} + +function TestGetContent() +{ + downloadedFile="./temp" + if curl "$1" --output "$downloadedFile" 2> /dev/null; then + expectedMd5="$(md5sum $2 | awk '{print $1}')" + actualMd5="$(md5sum $downloadedFile | awk '{print $1}')" + + if [ "$expectedMd5" == "$actualMd5" ]; then + echo "SUCCESS GET request content matched for $1" + else + echo "FAILURE file MD5 differs for $1" + echo "expected $truthMd5" + echo "actual $actualMd5" + fi + + rm $downloadedFile + else + echo "FAILURE Cannot GET content for $1" + fi +} + +function TestGET() +{ + truthFile="$serverWwwRoot/$1" + getUrl="http://localhost:8080/$1" + + TestGetContent $getUrl $truthFile + TestGetHeader $getUrl $2 + echo "" +} + +StartServer +TestGET "index.html" 'text/html' +TestGET "robedude.png" 'image/png' +TestGET "hope.jpg" 'image/jpeg' +TestGET "nevada.mp3" 'audio/mpeg3' +TestGET "index.css" 'text/css' +TestGET "test.js" 'application/javascript' +TestGET "subdir/index.html" 'text/html' +KillServer \ No newline at end of file