Helper to get amount of votes for a given id
[asis23-votoe-server.git] / test-api.sh
bloba1370457b0d5197ff58bac9dd10f68c9b3ae5acd
1 #!/bin/bash
3 which curl &> /dev/null
5 if (($? != 0)); then
6 echo "> curl not found"
7 exit 1
8 fi
10 which php &> /dev/null
12 if (($? != 0)); then
13 echo "> php not found"
14 exit 1
17 HOST="${1:-localhost}"
18 PORT="${2:-8000}"
20 # Colors:
21 # @see https://stackoverflow.com/a/5947802
23 # NO COLOR 0
24 # Black 0;30 Dark Gray 1;30
25 # Red 0;31 Light Red 1;31
26 # Green 0;32 Light Green 1;32
27 # Brown/Orange 0;33 Yellow 1;33
28 # Blue 0;34 Light Blue 1;34
29 # Purple 0;35 Light Purple 1;35
30 # Cyan 0;36 Light Cyan 1;36
31 # Light Gray 0;37 White 1;37
33 # .---------- constant part!
34 # vvvv vvvv-- the code from above
35 # RED='\033[0;31m'
36 # NC='\033[0m' # No Color
37 # printf "I ${RED}love${NC} Stack Overflow\n"
39 # --------------------------------------
40 # Display message
42 # Flags:
43 # - 'f': Failure message
44 # - 's': Success message
45 # - (empty): Normal message
47 # @param string $1 Message
48 # @param string $2 Flag
49 # --------------------------------------
50 function msg {
51 local rd='\033[0;31m'
52 local gr='\033[0;32m'
53 local nc='\033[0m'
55 local message="${1}"
56 local flag="${2}"
58 if [[ 's' == "${flag}" ]]; then
59 echo -e "${gr}[✅ SUCCESS] ${message}${nc}"
60 elif [[ 'f' == "${flag}" ]]; then
61 echo -e "${rd}[❌ FAILURE] ${message}${nc}"
62 else
63 echo ">>>>>>>>>>>> ${message}"
67 # --------------------------------------
68 # Call cURL command
70 # Use following global variables to return/export values:
72 # - CURL_EXIT_CODE
73 # - CURL_RESPONSE_HEADER
74 # - CURL_RESPONSE_DATA
76 # @param string $1 Path
77 # @param string $2 Method
78 # @param string $3 Data
79 # --------------------------------------
80 function callRestApi {
81 CURL_EXIT_CODE=1
82 CURL_RESPONSE_HEADER=''
83 CURL_RESPONSE_DATA=''
85 local path="${1}"
86 local method="${2:-GET}"
87 local data="${3}"
89 if [[ ! -z "${data}" ]]; then
90 data="--data ${data}"
93 curl "${HOST}:${PORT}${path}" \
94 --request "${method}" \
95 --header "Content-Type: application/json" \
96 --header "Accept: application/json" \
97 --dump-header curl_response_header.txt \
98 --output curl_response_data.txt \
99 $data &> /dev/null
101 CURL_EXIT_CODE=$?
103 if [ -f curl_response_header.txt ]; then
104 CURL_RESPONSE_HEADER=$(cat curl_response_header.txt)
105 rm curl_response_header.txt
108 if [ -f curl_response_data.txt ]; then
109 CURL_RESPONSE_DATA=$(cat curl_response_data.txt)
110 rm curl_response_data.txt
114 # --------------------------------------
115 # Get the value from a response header
117 # @param string $1 Headers list
118 # @param string $2 Header name
119 # --------------------------------------
120 function getValueFromHeader {
121 local list="${1}"
122 local key="${2}"
123 if [[ "code" == "${key}" ]]; then
124 echo $(echo "${list}" | grep '^HTTP/' | cut -d' ' -f2 | xargs)
125 else
126 echo $(echo "${list}" | grep "^${key}:" | cut -d':' -f2 | xargs)
130 # --------------------------------------
131 # Get a value from a JSON string
133 # @param string $1 JSON
134 # @param string $2 KEY
135 # --------------------------------------
136 function getValueFromJson {
137 local json="${1}"
138 local key="${2}"
139 local php=$(cat << PHP
140 \$list = json_decode('${json}', true);
141 echo \$list['${key}'];
144 echo $(php -r "${php}")
147 # --------------------------------------
148 # Count items in JSON list
150 # @param string $1 JSON
151 # --------------------------------------
152 function countItemsInJsonList {
153 local json="${1}"
154 local php=$(cat << PHP
155 \$list = json_decode('${json}', true);
156 echo count(\$list);
159 echo $(php -r "${php}")
162 # --------------------------------------
163 # Get name for a given id from a JSON string
165 # @param string $1 JSON
166 # @param string $2 ID
167 # --------------------------------------
168 function getNameFromId {
169 local json="${1}"
170 local id="${2}"
171 local php=$(cat << PHP
172 \$list = json_decode('${json}', true);
173 \$name = 0;
174 foreach (\$list as \$candidato) {
175 if (${id} == \$candidato['id']) {
176 \$name = \$candidato['name'];
177 break;
180 echo \$name;
183 echo $(php -r "${php}")
186 # --------------------------------------
187 # Get amount of votes for a given id from a JSON string
189 # @param string $1 JSON
190 # @param string $2 ID
191 # --------------------------------------
192 function getVotesFromId {
193 local json="${1}"
194 local id="${2}"
195 local php=$(cat << PHP
196 \$list = json_decode('${json}', true);
197 \$votes = 0;
198 foreach (\$list as \$candidato) {
199 if (${id} == \$candidato['id']) {
200 \$votes = \$candidato['votes'];
201 break;
204 echo \$votes;
207 echo $(php -r "${php}")
210 echo "TODO TEST"