Merge branch 'dummy-session'
[todo-rest-api_edi2-2023.git] / test-api.sh
blob4882eb0da146a11d24a136145a404a331c47c65d
1 #!/bin/bash
3 # --------------------------------------
4 # REST API - Test Frameworks
5 # --------------------------
6 # https://github.com/subeshb1/api-test
7 # https://github.com/apiaryio/dredd
8 # https://github.com/vlucas/frisby
9 # https://github.com/bbyars/mountebank
10 # https://github.com/schemathesis/schemathesis
11 # https://github.com/ladjs/supertest
12 # https://github.com/taverntesting/tavern
13 # https://github.com/Kong/insomnia
14 # --------------------------------------
16 which curl &> /dev/null
18 if (($? != 0)); then
19 echo "> curl not found"
20 exit 1
23 which php &> /dev/null
25 if (($? != 0)); then
26 echo "> php not found"
27 exit 1
30 HOST="${1:-localhost}"
31 PORT="${2:-8000}"
33 # Colors:
34 # @see https://stackoverflow.com/a/5947802
36 # NO COLOR 0
37 # Black 0;30 Dark Gray 1;30
38 # Red 0;31 Light Red 1;31
39 # Green 0;32 Light Green 1;32
40 # Brown/Orange 0;33 Yellow 1;33
41 # Blue 0;34 Light Blue 1;34
42 # Purple 0;35 Light Purple 1;35
43 # Cyan 0;36 Light Cyan 1;36
44 # Light Gray 0;37 White 1;37
46 # .---------- constant part!
47 # vvvv vvvv-- the code from above
48 # RED='\033[0;31m'
49 # NC='\033[0m' # No Color
50 # printf "I ${RED}love${NC} Stack Overflow\n"
52 # --------------------------------------
53 # Display message
55 # Flags:
56 # - 'f': Failure message
57 # - 's': Success message
58 # - (empty): Normal message
60 # @param string $1 Message
61 # @param string $2 Flag
62 # --------------------------------------
63 function msg {
64 local rd='\033[0;31m'
65 local gr='\033[0;32m'
66 local nc='\033[0m'
68 local message="${1}"
69 local flag="${2}"
71 if [[ 's' == "${flag}" ]]; then
72 echo -e "${gr}[✅ SUCCESS] ${message}${nc}"
73 elif [[ 'f' == "${flag}" ]]; then
74 echo -e "${rd}[❌ FAILURE] ${message}${nc}"
75 else
76 echo ">>>>>>>>>>>> ${message}"
80 # --------------------------------------
81 # Call cURL command
83 # Use following global variables to return/export values:
85 # - CURL_EXIT_CODE
86 # - CURL_RESPONSE_HEADER
87 # - CURL_RESPONSE_DATA
89 # @param string $1 Path
90 # @param string $2 Method
91 # @param string $3 Data
92 # --------------------------------------
93 function callRestApi {
94 CURL_EXIT_CODE=1
95 CURL_RESPONSE_HEADER=''
96 CURL_RESPONSE_DATA=''
98 local path="${1}"
99 local method="${2:-GET}"
100 local data="${3}"
102 if [[ ! -z "${data}" ]]; then
103 data="--data ${data}"
106 curl "${HOST}:${PORT}${path}" \
107 --request "${method}" \
108 --header "Content-Type: application/json" \
109 --header "Accept: application/json" \
110 --dump-header curl_response_header.txt \
111 --output curl_response_data.txt \
112 $data &> /dev/null
114 CURL_EXIT_CODE=$?
116 if [ -f curl_response_header.txt ]; then
117 CURL_RESPONSE_HEADER=$(cat curl_response_header.txt)
118 rm curl_response_header.txt
121 if [ -f curl_response_data.txt ]; then
122 CURL_RESPONSE_DATA=$(cat curl_response_data.txt)
123 rm curl_response_data.txt
127 # --------------------------------------
128 # Get the value from a response header
130 # @param string $1 Headers list
131 # @param string $2 Header name
132 # --------------------------------------
133 function getValueFromHeader {
134 local list="${1}"
135 local key="${2}"
136 if [[ "code" == "${key}" ]]; then
137 echo $(echo "${list}" | grep '^HTTP/' | cut -d' ' -f2 | xargs)
138 else
139 echo $(echo "${list}" | grep "^${key}:" | cut -d':' -f2 | xargs)
143 # --------------------------------------
144 # Get a value from a JSON string
146 # @param string $1 JSON
147 # @param string $2 KEY
148 # --------------------------------------
149 function getValueFromJson {
150 local json="${1}"
151 local key="${2}"
152 echo $(php -r '$a=json_decode('"'${json}'"',true);echo $a["'"${key}"'"];')
155 # ====================================================================
157 # --------------------------------------
158 # Test item creation
160 # @param string $1 Item name
161 # --------------------------------------
162 function testCreateItem {
163 local name="${1}"
165 echo '========================================'
166 msg 'Test create item'
168 callRestApi '/items' 'POST' '{"name":"'"${name}"'"}'
170 if (( 0 != "${CURL_EXIT_CODE}" )); then
171 msg 'Failed curl call' f
172 else
173 local code="$(getValueFromHeader "${CURL_RESPONSE_HEADER}" "code")"
175 if (( 200 != $code )); then
176 msg 'Failed to create item' f
177 else
178 local flag=""
180 local rname="$(getValueFromJson "${CURL_RESPONSE_DATA}" "name")"
181 flag=$([[ "${rname}" == "${name}" ]] && echo "s" || echo "f")
182 msg "Iten name (${name}, ${rname})" "${flag}"
184 local done="$(getValueFromJson "${CURL_RESPONSE_DATA}" "done")"
185 done=$([[ "1" == "${done}" ]] && echo 'true' || echo 'false')
186 flag=$([[ "false" == "${done}" ]] && echo "s" || echo "f")
187 msg "Iten done (false, ${done})" "${flag}"
189 ITEM_ID="$(getValueFromJson "${CURL_RESPONSE_DATA}" "id")"
193 echo
196 # --------------------------------------
197 # Test get item
199 # @param string $1 Item id
200 # @param string $2 Item name
201 # @param string $3 Item done
202 # --------------------------------------
203 function testGetItem {
204 local id="${1}"
205 local name="${2}"
206 local done="${3}"
208 echo '========================================'
209 msg 'Test get item'
211 callRestApi "/items/${id}" 'GET'
213 if (( 0 != "${CURL_EXIT_CODE}" )); then
214 msg 'Failed curl call' f
215 else
216 local code="$(getValueFromHeader "${CURL_RESPONSE_HEADER}" "code")"
218 if (( 200 != $code )); then
219 msg 'Failed to get item' f
220 else
221 local rid="$(getValueFromJson "${CURL_RESPONSE_DATA}" "id")"
222 flag=$([[ "${rid}" == "${id}" ]] && echo "s" || echo "f")
223 msg "Iten id (${id}, ${rid})" "${flag}"
225 local rname="$(getValueFromJson "${CURL_RESPONSE_DATA}" "name")"
226 flag=$([[ "${rname}" == "${name}" ]] && echo "s" || echo "f")
227 msg "Iten name (${name}, ${rname})" "${flag}"
229 local rdone="$(getValueFromJson "${CURL_RESPONSE_DATA}" "done")"
230 rdone=$([[ "1" == "${rdone}" ]] && echo 'true' || echo 'false')
231 flag=$([[ "${rdone}" == "${done}" ]] && echo "s" || echo "f")
232 msg "Iten done (${done}, ${rdone})" "${flag}"
236 echo
239 # --------------------------------------
240 # Test update item
242 # @param string $1 Item id
243 # @param string $2 Item name
244 # @param string $3 Item done
245 # --------------------------------------
246 function testUpdateItem {
247 local id="${1}"
248 local name="${2}"
249 local done="${3}"
251 echo '========================================'
252 msg 'Test update item'
254 callRestApi "/items/${id}" 'PUT' '{"done":'"${done}"'}'
256 if (( 0 != "${CURL_EXIT_CODE}" )); then
257 msg 'Failed curl call' f
258 else
259 local code="$(getValueFromHeader "${CURL_RESPONSE_HEADER}" "code")"
261 if (( 200 != $code )); then
262 msg 'Failed to update item' f
263 else
264 local rid="$(getValueFromJson "${CURL_RESPONSE_DATA}" "id")"
265 flag=$([[ "${rid}" == "${id}" ]] && echo "s" || echo "f")
266 msg "Iten id (${id}, ${rid})" "${flag}"
268 local rname="$(getValueFromJson "${CURL_RESPONSE_DATA}" "name")"
269 flag=$([[ "${rname}" == "${name}" ]] && echo "s" || echo "f")
270 msg "Iten name (${name}, ${rname})" "${flag}"
272 local rdone="$(getValueFromJson "${CURL_RESPONSE_DATA}" "done")"
273 rdone=$([[ "1" == "${rdone}" ]] && echo 'true' || echo 'false')
274 flag=$([[ "${rdone}" == "${done}" ]] && echo "s" || echo "f")
275 msg "Iten done (${done}, ${rdone})" "${flag}"
279 echo
282 # --------------------------------------
283 # Test delete item
285 # @param string $1 Item id
286 # --------------------------------------
287 function testDeleteItem {
288 local id="${1}"
290 echo '========================================'
291 msg 'Test delete item'
293 callRestApi "/items/${id}" 'DELETE'
295 if (( 0 != "${CURL_EXIT_CODE}" )); then
296 msg 'Failed curl call' f
297 else
298 local code="$(getValueFromHeader "${CURL_RESPONSE_HEADER}" "code")"
300 if (( 200 != $code )); then
301 msg 'Failed to delete item' f
302 else
303 msg 'Item deleted' s
307 echo
310 # --------------------------------------
311 # Test list and count items
313 # @param string $1 Items amount
314 # --------------------------------------
315 function testListCountItems {
316 local amount="${1}"
318 echo '========================================'
319 msg 'Test list and count items'
321 callRestApi "/items" 'GET'
323 if (( 0 != "${CURL_EXIT_CODE}" )); then
324 msg 'Failed curl call' f
325 else
326 local code="$(getValueFromHeader "${CURL_RESPONSE_HEADER}" "code")"
328 if (( 200 != $code )); then
329 msg 'Failed to get items' f
330 else
331 local ramount=$(php -r '$a=json_decode('"'${CURL_RESPONSE_DATA}'"',true);echo count($a);')
332 flag=$([[ "${amount}" == "${ramount}" ]] && echo "s" || echo "f")
333 msg "Items listed (${amount}, ${ramount})" "${flag}"
337 echo
340 # ====================================================================
342 # Reset (destroy) PHP session before running any test
343 callRestApi "/session" 'DELETE'
345 ITEM_1_ID=''
346 ITEM_1_NAME='milk'
347 testCreateItem "${ITEM_1_NAME}"
348 ITEM_1_ID="${ITEM_ID}"
349 testListCountItems 1
350 testGetItem "${ITEM_1_ID}" "${ITEM_1_NAME}" "false"
351 testUpdateItem "${ITEM_1_ID}" "${ITEM_1_NAME}" "true"
352 testGetItem "${ITEM_1_ID}" "${ITEM_1_NAME}" "true"
354 ITEM_2_ID=''
355 ITEM_2_NAME='coffee'
356 testCreateItem "${ITEM_2_NAME}"
357 ITEM_2_ID="${ITEM_ID}"
358 testListCountItems 2
359 testGetItem "${ITEM_2_ID}" "${ITEM_2_NAME}" "false"
361 ITEM_3_ID=''
362 ITEM_3_NAME='apples'
363 testCreateItem "${ITEM_3_NAME}"
364 ITEM_3_ID="${ITEM_ID}"
365 testListCountItems 3
366 testGetItem "${ITEM_3_ID}" "${ITEM_3_NAME}" "false"
367 testUpdateItem "${ITEM_3_ID}" "${ITEM_3_NAME}" "true"
368 testGetItem "${ITEM_3_ID}" "${ITEM_3_NAME}" "true"
369 testUpdateItem "${ITEM_3_ID}" "${ITEM_3_NAME}" "false"
370 testGetItem "${ITEM_3_ID}" "${ITEM_3_NAME}" "false"
372 testDeleteItem "${ITEM_1_ID}"
373 testListCountItems 2
375 testDeleteItem "${ITEM_2_ID}"
376 testListCountItems 1
378 testDeleteItem "${ITEM_3_ID}"
379 testListCountItems 0