Skip to content

Instantly share code, notes, and snippets.

@RoboSparrow
Last active November 5, 2019 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RoboSparrow/d3b6f7df927b2106a2f0a215862c7a6e to your computer and use it in GitHub Desktop.
Save RoboSparrow/d3b6f7df927b2106a2f0a215862c7a6e to your computer and use it in GitHub Desktop.
Surveysystem: Test /analyse endpoint
#!/bin/bash
## performs two calls to /analyse for a given session and compares responses.
## If both match the test passes.
## If a mismatch occurs or a single response is invalid then the error and the response will be logged into ./error.log
## Tests are repeated 50 times.
# config
# - a valid seesion id, the session has to be finished (all questions answered)
SESSID=<SESSID>
# - htdigest auth logi:n username:password
USER="<DIGEST_USER:DIGEST_PASS>"
# - api endpoint
URI="<ENDPOINT>/surveyapi/analyse?sessionid=$SESSID"
# number of test iterations to run
ITERATIONS=100
# utils
function GREEN {
echo -e "\033[0;32m${1}\033[0m"
}
function RED {
echo -e "\033[0;31m${1}\033[0m"
}
echo \n-------------NEW TEST-------------\n >> ./error.log
# main script
for ((COUNT=0; COUNT<=ITERATIONS; COUNT++)); do
echo "$COUNT) ------------"
echo -- "$COUNT" $(date +"%T") >> ./error.log
ONE=$(curl -s --digest --user $USER $URI)
TWO=$(curl -s --digest --user $USER $URI)
# very simple validation of json, we need to do this only for the first response
if [[ $ONE != '{"category":'* ]]
then
echo -- $(RED "$COUNT: UNEXPECTED RESPONSE!") - mismatching start pattern
echo "$ONE"
echo [INVALID RESPONSE] >> ./error.log
echo "- [ONE] $ONE" >> ./error.log
exit 1
fi
if [[ "${ONE: -1}" != '}' ]]
then
echo -- $(RED "$COUNT: UNEXPECTED RESPONSE!") - no closing bracket
echo "$ONE"
echo [INVALID RESPONSE] >> ./error.log
echo "- [ONE] $ONE" >> ./error.log
exit 1
fi
# main task: diff the two responses
DIFF=$(diff -u <(echo "$ONE") <(echo "$TWO"))
if [ "$DIFF" != "" ]; then
echo -- $(RED "$COUNT: Mismatch!")
echo "$DIFF"
# add invalid response to log
echo "- [ONE] $ONE" >> ./error.log
echo [MISMATCH] >> ./error.log
echo "- [TWO] $TWO" >> ./error.log
exit 1
fi
echo [OK] >> ./error.log
echo $(GREEN "ok")
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment