REST API testing script

This commit is contained in:
Marcel Kapfer (mmk2410) 2016-06-21 09:09:33 +02:00
parent d0bf3eae0f
commit b4042cbf15
12 changed files with 317 additions and 36 deletions

147
run_tests.sh Executable file
View File

@ -0,0 +1,147 @@
#!/bin/bash
# Tests for the REST Api
# Copright 2016 (c) Marcel Kapfer (mmk2410)
# Licensed under AGPL
TESTS_AMOUNT=0
TESTS_PASSED=0
ERROR_LOG="./error.log"
CONTENT_TYPE="Content-Type: application/json"
BASEURL="http://localhost:8080/titamaApi/v1"
# Delete old log file if it exists
if [ -f $ERROR_LOG ]; then
rm $ERROR_LOG
fi
# Test for get requests
function test_get {
if [ -z "$1" ]; then
echo "[ERROR] No URL give."
exit 1
elif [ -z "$2" ]; then
echo "[ERROR] No expected value given."
exit 1
fi
((TESTS_AMOUNT++))
echo "Running test $TESTS_AMOUNT: $1"
response=$(curl "$1")
if [ "$2" == "$response" ]; then
echo "Test $TESTS_AMOUNT passed."
((TESTS_PASSED++))
else
echo "Test $TESTS_AMOUNT failed."
echo "Test $TESTS_AMOUNT failed." >> $ERROR_LOG
echo "$response" >> $ERROR_LOG
fi
}
# Test for post requests
function test_post {
if [ -z "$1" ]; then
echo "[ERROR] No URL give."
exit 1
elif [ -z "$2" ]; then
echo "[ERROR] No post data given."
exit 1
elif [ -z "$3" ]; then
echo "[ERROR] No expected value given."
exit 1
fi
((TESTS_AMOUNT++))
echo "Running test $TESTS_AMOUNT: $1"
response=$(curl -d "$2" -H "$CONTENT_TYPE" "$1")
if [ "$3" == "$response" ]; then
echo "Test $TESTS_AMOUNT passed."
((TESTS_PASSED++))
else
echo "Test $TESTS_AMOUNT failed."
echo "Test $TESTS_AMOUNT failed." >> $ERROR_LOG
echo "$response" >> $ERROR_LOG
fi
}
# Test for delete requests
function test_delete {
if [ -z "$1" ]; then
echo "[ERROR] No URL give."
exit 1
elif [ -z "$2" ]; then
echo "[ERROR] No expected value given."
exit 1
fi
((TESTS_AMOUNT++))
echo "Running test $TESTS_AMOUNT: $1"
response=$(curl -X DELETE "$1")
if [ "$2" == "$response" ]; then
echo "Test $TESTS_AMOUNT passed."
((TESTS_PASSED++))
else
echo "Test $TESTS_AMOUNT failed."
echo "Test $TESTS_AMOUNT failed." >> $ERROR_LOG
echo "$response" >> $ERROR_LOG
fi
}
# Test for put requests
function test_put {
if [ -z "$1" ]; then
echo "[ERROR] No URL give."
exit 1
elif [ -z "$2" ]; then
echo "[ERROR] No post data given."
exit 1
elif [ -z "$3" ]; then
echo "[ERROR] No expected value given."
exit 1
fi
((TESTS_AMOUNT++))
echo "Running test $TESTS_AMOUNT: $1"
response=$(curl -d "$2" -H "$CONTENT_TYPE" -X PUT "$1")
if [ "$3" == "$response" ]; then
echo "Test $TESTS_AMOUNT passed."
((TESTS_PASSED++))
else
echo "Test $TESTS_AMOUNT failed."
echo "Test $TESTS_AMOUNT failed." >> $ERROR_LOG
echo "$response" > $ERROR_LOG
fi
}
# Tests
post_course_data='{"title": "FIN Sitzung", "day": "Wed", "time": "18:00"}'
put_course_data='{"title": "FIN Sitzung", "day": "Wed", "time": "18:00", "kind": "meeting"}'
test_get "$BASEURL/courses" "$(cat "./tests/01-get_courses.json")"
test_get "$BASEURL/course/0" "$(cat "./tests/02-get_course-0.json")"
test_get "$BASEURL/course/1" "$(cat "./tests/03-get_course-1.json")"
test_post "$BASEURL/course" "$post_course_data" "$(cat "./tests/04-post_course.json")"
test_get "$BASEURL/courses" "$(cat "./tests/05-get_courses.json")"
test_get "$BASEURL/course/2" "$(cat "./tests/06-get_course-2.json")"
test_delete "$BASEURL/course/1" "$(cat "./tests/07-delete_course-1.json")"
test_get "$BASEURL/courses" "$(cat "./tests/08-get_courses.json")"
test_put "$BASEURL/course/2" "$put_course_data" "$(cat "./tests/09-put_course-2.json")"
test_get "$BASEURL/courses" "$(cat "./tests/10-get_courses.json")"
# Final result
echo "$TESTS_PASSED/$TESTS_AMOUNT tests passed."

36
test.sh
View File

@ -1,36 +0,0 @@
#!/bin/bash
baseurl="http://localhost:8080/titamaApi/v1"
header="Content-Type: application/json"
post_course_data='{"title": "FIN Sitzung", "day": "Wed", "time": "18:00"}'
put_course_data='{"title": "FIN Sitzung", "day": "Wed", "time": "18:00", "kind": "meeting"}'
printf "Testing GET %s/courses...\n" "$baseurl"
curl "$baseurl/courses"
printf "\n\nTesting GET %s/course/0...\n" "$baseurl"
curl "$baseurl/course/0"
printf "\n\nTesting GET %s/course/1...\n" "$baseurl"
curl "$baseurl/course/1"
printf "\n\nTesting POST %s/course...\n" "$baseurl"
curl -d "$post_course_data" -H "$header" $baseurl/course
printf "\n\nTesting GET %s/courses...\n" "$baseurl"
curl "$baseurl/courses"
printf "\n\nTesting GET %s/course/2...\n" "$baseurl"
curl "$baseurl/course/2"
printf "\n\nTesting DELETE %s/course/1...\n" "$baseurl"
curl -X DELETE "$baseurl/course/1"
printf "\n\nTesting GET %s/courses...\n" "$baseurl"
curl "$baseurl/courses"
printf "\n\nTesting PUT %s/course/2...\n" "$baseurl"
curl -d "$put_course_data" -H "$header" -X PUT $baseurl/course/2
printf "\n\nTesting GET %s/courses...\n" "$baseurl"
curl "$baseurl/courses"

22
tests/01-get_courses.json Normal file
View File

@ -0,0 +1,22 @@
[
{
"title": "UlmAPI",
"time": "18:00",
"day": "Monday",
"kind": "Lab",
"place": "O27/343",
"prof": "",
"turnin": "",
"id": 0
},
{
"title": "CCC Ulm",
"time": "20:00",
"day": "Monday",
"kind": "Meeting",
"place": "Cafe Einstein",
"prof": "",
"turnin": "",
"id": 1
}
]

View File

@ -0,0 +1,10 @@
{
"title": "UlmAPI",
"time": "18:00",
"day": "Monday",
"kind": "Lab",
"place": "O27/343",
"prof": "",
"turnin": "",
"id": 0
}

View File

@ -0,0 +1,10 @@
{
"title": "CCC Ulm",
"time": "20:00",
"day": "Monday",
"kind": "Meeting",
"place": "Cafe Einstein",
"prof": "",
"turnin": "",
"id": 1
}

10
tests/04-post_course.json Normal file
View File

@ -0,0 +1,10 @@
{
"title": "FIN Sitzung",
"time": "18:00",
"day": "Wed",
"kind": "",
"place": "",
"prof": "",
"turnin": "",
"id": 2
}

32
tests/05-get_courses.json Normal file
View File

@ -0,0 +1,32 @@
[
{
"title": "UlmAPI",
"time": "18:00",
"day": "Monday",
"kind": "Lab",
"place": "O27/343",
"prof": "",
"turnin": "",
"id": 0
},
{
"title": "CCC Ulm",
"time": "20:00",
"day": "Monday",
"kind": "Meeting",
"place": "Cafe Einstein",
"prof": "",
"turnin": "",
"id": 1
},
{
"title": "FIN Sitzung",
"time": "18:00",
"day": "Wed",
"kind": "",
"place": "",
"prof": "",
"turnin": "",
"id": 2
}
]

View File

@ -0,0 +1,10 @@
{
"title": "FIN Sitzung",
"time": "18:00",
"day": "Wed",
"kind": "",
"place": "",
"prof": "",
"turnin": "",
"id": 2
}

View File

@ -0,0 +1,22 @@
[
{
"title": "UlmAPI",
"time": "18:00",
"day": "Monday",
"kind": "Lab",
"place": "O27/343",
"prof": "",
"turnin": "",
"id": 0
},
{
"title": "FIN Sitzung",
"time": "18:00",
"day": "Wed",
"kind": "",
"place": "",
"prof": "",
"turnin": "",
"id": 2
}
]

22
tests/08-get_courses.json Normal file
View File

@ -0,0 +1,22 @@
[
{
"title": "UlmAPI",
"time": "18:00",
"day": "Monday",
"kind": "Lab",
"place": "O27/343",
"prof": "",
"turnin": "",
"id": 0
},
{
"title": "FIN Sitzung",
"time": "18:00",
"day": "Wed",
"kind": "",
"place": "",
"prof": "",
"turnin": "",
"id": 2
}
]

View File

@ -0,0 +1,10 @@
{
"title": "FIN Sitzung",
"time": "18:00",
"day": "Wed",
"kind": "meeting",
"place": "",
"prof": "",
"turnin": "",
"id": 2
}

22
tests/10-get_courses.json Normal file
View File

@ -0,0 +1,22 @@
[
{
"title": "UlmAPI",
"time": "18:00",
"day": "Monday",
"kind": "Lab",
"place": "O27/343",
"prof": "",
"turnin": "",
"id": 0
},
{
"title": "FIN Sitzung",
"time": "18:00",
"day": "Wed",
"kind": "meeting",
"place": "",
"prof": "",
"turnin": "",
"id": 2
}
]