-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.sh
executable file
·74 lines (60 loc) · 1.45 KB
/
server_test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
# Compile and start the server in the background
cd src
make
./server &
SERVER_PID=$!
echo "Server PID: $SERVER_PID"
PORT="8080"
URL="localhost:${PORT}"
numtest=0
passed=0
function test_server()
{
echo "$1"
echo "$2"
#Get 2 arguements
local endpoint=$1
local expected_result=$2
#Send HTTP request and save respone
response=$(curl -s $URL$endpoint)
let "numtest += 1"
#If actual response matches expected test passed
if [[ $response == $expected_result ]]; then
echo "Test ${numtest} passed."
let "passed+=1"
#Otherwise test failed
else
echo "Test ${numtest} failed. Expected: $expected_result, Got: $response"
fi
}
#Shutdown the server
function cleanup {
echo "Stopping the server..."
kill $SERVER_PID
wait $SERVER_PID 2>/dev/null
echo "Server stopped."
}
#If script exits early always end the server session
trap cleanup EXIT
sleep 4 #Allow time for server to start
#tests to be executed
tests=("/" "OK"
"/echo/HelloWorld" "HelloWorld"
"/user-agent" "curl/7.81.0"
"/serverroot/index.html"
"<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<p>This is an example of a simple HTML page with one paragraph.</p>
</body>
</html>"
"/notfound" ""
)
for (( i=0; i<${#tests[@]} ; i+=2 )) ; do
test_server "${tests[i]}" "${tests[i+1]}"
done
echo "Number of tests passed ${passed}"