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
19 echo "> curl not found"
23 which php
&> /dev
/null
26 echo "> php not found"
30 HOST
="${1:-localhost}"
34 # @see https://stackoverflow.com/a/5947802
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
49 # NC='\033[0m' # No Color
50 # printf "I ${RED}love${NC} Stack Overflow\n"
52 # --------------------------------------
56 # - 'f': Failure message
57 # - 's': Success message
58 # - (empty): Normal message
60 # @param string $1 Message
61 # @param string $2 Flag
62 # --------------------------------------
71 if [[ 's' == "${flag}" ]]; then
72 echo -e "${gr}[✅ SUCCESS] ${message}${nc}"
73 elif [[ 'f' == "${flag}" ]]; then
74 echo -e "${rd}[❌ FAILURE] ${message}${nc}"
76 echo ">>>>>>>>>>>> ${message}"
80 # --------------------------------------
83 # Use following global variables to return/export values:
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
{
95 CURL_RESPONSE_HEADER
=''
99 local method
="${2:-GET}"
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 \
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
{
136 if [[ "code" == "${key}" ]]; then
137 echo $
(echo "${list}" |
grep '^HTTP/' | cut
-d' ' -f2 |
xargs)
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
{
152 echo $
(php
-r '$a=json_decode('"'${json}'"',true);echo $a["'"${key}"'"];')
155 # ====================================================================
157 # --------------------------------------
160 # @param string $1 Item name
161 # --------------------------------------
162 function testCreateItem
{
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
173 local code
="$(getValueFromHeader "${CURL_RESPONSE_HEADER}" "code
")"
175 if (( 200 != $code )); then
176 msg
'Failed to create item' f
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
")"
196 # --------------------------------------
199 # @param string $1 Item id
200 # @param string $2 Item name
201 # @param string $3 Item done
202 # --------------------------------------
203 function testGetItem
{
208 echo '========================================'
211 callRestApi
"/items/${id}" 'GET'
213 if (( 0 != "${CURL_EXIT_CODE}" )); then
214 msg
'Failed curl call' f
216 local code
="$(getValueFromHeader "${CURL_RESPONSE_HEADER}" "code
")"
218 if (( 200 != $code )); then
219 msg
'Failed to get item' f
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}"
239 # --------------------------------------
242 # @param string $1 Item id
243 # @param string $2 Item name
244 # @param string $3 Item done
245 # --------------------------------------
246 function testUpdateItem
{
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
259 local code="$
(getValueFromHeader
"${CURL_RESPONSE_HEADER}" "code")"
261 if (( 200 != $code )); then
262 msg 'Failed to update item' f
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}"
282 # --------------------------------------
285 # @param string $1 Item id
286 # --------------------------------------
287 function testDeleteItem {
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
298 local code="$
(getValueFromHeader
"${CURL_RESPONSE_HEADER}" "code")"
300 if (( 200 != $code )); then
301 msg 'Failed to delete item' f
310 # --------------------------------------
311 # Test list and count items
313 # @param string $1 Items amount
314 # --------------------------------------
315 function testListCountItems {
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
326 local code="$
(getValueFromHeader
"${CURL_RESPONSE_HEADER}" "code")"
328 if (( 200 != $code )); then
329 msg 'Failed to get items' f
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}"
340 # ====================================================================
342 # Reset (destroy) PHP session before running any test
343 callRestApi "/session
" 'DELETE'
347 testCreateItem "${ITEM_1_NAME}"
348 ITEM_1_ID="${ITEM_ID}"
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
"
356 testCreateItem "${ITEM_2_NAME}"
357 ITEM_2_ID="${ITEM_ID}"
359 testGetItem "${ITEM_2_ID}" "${ITEM_2_NAME}" "false
"
363 testCreateItem "${ITEM_3_NAME}"
364 ITEM_3_ID="${ITEM_ID}"
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}"
375 testDeleteItem "${ITEM_2_ID}"
378 testDeleteItem "${ITEM_3_ID}"