Skip to content

Commit ce7fe11

Browse files
authored
Merge pull request #1 from subeshb1/organize-display
Update display style and add flags
2 parents 4d4ba56 + f8cff32 commit ce7fe11

File tree

1 file changed

+133
-39
lines changed

1 file changed

+133
-39
lines changed

api-test.sh

Lines changed: 133 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -o pipefail
44
RED=$(tput setaf 1)
55
GREEN=$(tput setaf 2)
66
BOLD=$(tput bold)
7-
RESET=$(tput sgr0)
7+
RESET=$(tput sgr 0)
88

99
ACTION=""
1010

@@ -18,21 +18,52 @@ ACCESS_TOKEN=""
1818
ID_TOKEN=""
1919
URL=""
2020

21+
SHOW_HEADER=0
22+
HEADER_ONLY=0
23+
SILENT=0
24+
API_ERROR=0
25+
2126
echo_v() {
2227
if [ $VERBOSE -eq 1 ]; then
2328
echo $1
2429
fi
2530
}
2631

32+
bytes_to_human() {
33+
b=${1:-0}
34+
d=''
35+
s=0
36+
S=(Bytes {K,M,G,T,E,P,Y,Z}B)
37+
while ((b > 1024)); do
38+
d="$(printf ".%02d" $((b % 1024 * 100 / 1024)))"
39+
b=$((b / 1024))
40+
let s++
41+
done
42+
echo "$b$d ${S[$s]}"
43+
}
44+
2745
run() {
28-
cat $FILE | jq empty
29-
if [ $? -ne 0 ]; then
30-
exit
31-
fi
32-
URL=$(jq -r '.url' $FILE)
33-
ACCESS_TOKEN=$(jq -r '.accessToken' $FILE)
34-
ID_TOKEN=$(jq -r '.idToken' $FILE)
35-
COMMON_HEADER=$(cat $FILE | jq -r -c ". | .header | if . != null then . else {} end | to_entries | map(\"\(.key): \(.value|tostring)\") | join(\"\n\") | if ( . | length) != 0 then \"-H\" + . else \"-H \" end")
46+
for arg in "$@"; do
47+
case $arg in
48+
-i | --include)
49+
SHOW_HEADER=1
50+
shift
51+
;;
52+
-I | --header-only)
53+
HEADER_ONLY=1
54+
shift
55+
;;
56+
-s | --silent)
57+
SILENT=1
58+
shift
59+
;;
60+
-h | --help)
61+
usage run
62+
exit
63+
;;
64+
esac
65+
done
66+
3667
case $1 in
3768
all)
3869
api_factory "$(jq -r '.testCases | keys[]' $FILE)"
@@ -45,21 +76,59 @@ run() {
4576

4677
api_factory() {
4778
for TEST_CASE in $@; do
79+
API_ERROR=0
4880
echo "${BOLD}Running Case:${RESET} $TEST_CASE"
4981
echo_v "${BOLD}Description: ${RESET}$(jq -r ".testCases.$TEST_CASE.description" $FILE)"
50-
echo_v
51-
ROUTE=$(jq -r ".testCases.$TEST_CASE.path" $FILE)
52-
BODY="$(jq -r ".testCases.$TEST_CASE.body" $FILE)"
53-
QUERY_PARAMS=$(cat $FILE | jq -r ".testCases.$TEST_CASE | select(.query != null) | .query | to_entries | map(\"\(.key)=\(.value|tostring)\") | join(\"&\") | \"?\" + . ")
54-
REQUEST_HEADER=$(cat $FILE | jq -r ".testCases.$TEST_CASE | .header | if . != null then . else {} end | to_entries | map(\"\(.key): \(.value|tostring)\") | join(\"\n\") | if ( . | length) != 0 then \"-H\" + . else \"-H \" end")
55-
METHOD="$(jq -r ".testCases.$TEST_CASE.method //\"GET\" | ascii_upcase" $FILE)"
56-
call_api
82+
echo_v "${BOLD}Action: ${RESET}$(jq -r ".testCases.$TEST_CASE.method //\"GET\" | ascii_upcase" $FILE) $(jq -r ".testCases.$TEST_CASE.path" $FILE)"
83+
call_api $TEST_CASE
84+
display_results
5785
echo ""
5886
echo ""
5987
done
6088
}
6189

90+
display_results() {
91+
92+
if [[ $API_ERROR == 1 ]]; then
93+
return
94+
fi
95+
96+
local res=$(jq -r '.http_status + " " + .http_message ' <<<"$RESPONSE_HEADER")
97+
local status=$(jq -r '.http_status' <<<"$RESPONSE_HEADER")
98+
echo "Response:"
99+
echo "${BOLD}$(color_response $status)$res${RESET}"
100+
if [[ $HEADER_ONLY == 1 ]]; then
101+
echo "HEADER:"
102+
echo "$RESPONSE_HEADER" | jq -C
103+
else
104+
if [[ $SHOW_HEADER == 1 ]]; then
105+
echo "HEADER:"
106+
echo "$RESPONSE_HEADER" | jq -C
107+
fi
108+
if [[ $SILENT == 0 ]]; then
109+
echo "BODY:"
110+
echo "$RESPONSE_BODY" | jq -C
111+
fi
112+
113+
fi
114+
echo "META:"
115+
echo "$META" | jq -C
116+
}
117+
118+
color_response() {
119+
case $1 in
120+
2[0-9][0-9]) echo $GREEN ;;
121+
[45][0-9][0-9]) echo $RED ;;
122+
*) ;;
123+
esac
124+
}
125+
62126
call_api() {
127+
ROUTE=$(jq -r ".testCases.$1.path" $FILE)
128+
BODY="$(jq -r ".testCases.$1.body" $FILE)"
129+
QUERY_PARAMS=$(cat $FILE | jq -r ".testCases.$1 | select(.query != null) | .query | to_entries | map(\"\(.key)=\(.value|tostring)\") | join(\"&\") | \"?\" + . ")
130+
REQUEST_HEADER=$(cat $FILE | jq -r ".testCases.$1 | .header | if . != null then . else {} end | to_entries | map(\"\(.key): \(.value|tostring)\") | join(\"\n\") | if ( . | length) != 0 then \"-H\" + . else \"-H \" end")
131+
METHOD="$(jq -r ".testCases.$1.method //\"GET\" | ascii_upcase" $FILE)"
63132
# curl -ivs --request $METHOD "$URL$ROUTE$QUERY_PARAMS" \
64133
# --data "$BODY" \
65134
# "$COMMON_HEADER" \
@@ -69,49 +138,64 @@ call_api() {
69138
--data "$BODY" \
70139
"$COMMON_HEADER" \
71140
"$REQUEST_HEADER" \
72-
-w '\n{ "ResponseTime": "%{time_total}s" }' || echo "AUTO_API_ERROR")
141+
-w '\n{ "ResponseTime": "%{time_total}s", "Size": %{size_download} }' || echo "AUTO_API_ERROR")
73142

74143
if [[ $raw_output == *"AUTO_API_ERROR"* ]]; then
75144
echo "Problem connecting to $URL"
145+
API_ERROR=1
76146
return 1
77147
fi
78148
local header="$(awk -v bl=1 'bl{bl=0; h=($0 ~ /HTTP\//)} /^\r?$/{bl=1} {if(h)print $0 }' <<<"$raw_output")"
79149
local json=$(jq -c -R -r '. as $line | try fromjson' <<<"$raw_output")
80-
BODY=$(sed -n 1p <<<"$json")
150+
RESPONSE_BODY=$(sed -n 1p <<<"$json")
81151
META=$(sed 1d <<<"$json")
152+
META=$(jq -r ".Size = \"$(bytes_to_human $(jq -r '.Size' <<<"$META"))\"" <<<"$META")
82153
parse_header "$header"
83-
echo "HEADER:"
84-
echo "$HEADER" | jq -C
85-
echo "BODY:"
86-
echo "$BODY" | jq -C
87-
echo "META:"
88-
echo "$META" | jq -C
89154
}
90155

91156
function parse_header() {
92157
local RESPONSE=($(echo "$header" | tr '\r' ' ' | sed -n 1p))
93158
local header=$(echo "$header" | sed '1d;$d' | sed 's/: /" : "/' | sed 's/^/"/' | tr '\r' ' ' | sed 's/ $/",/' | sed '1 s/^/{/' | sed '$ s/,$/}/' | jq)
94-
# echo "$HEADER"
95-
HEADER=$(echo "$header" "{ \"http_version\": \"${RESPONSE[0]}\",
159+
RESPONSE_HEADER=$(echo "$header" "{ \"http_version\": \"${RESPONSE[0]}\",
96160
\"http_status\": \"${RESPONSE[1]}\",
97161
\"http_message\": \"${RESPONSE[@]:2}\",
98162
\"http_response\": \"${RESPONSE[@]:0}\" }" | jq -s add)
99163
}
100164

101-
# Show usage and exit
165+
# Show usage
102166
function usage() {
103-
echo "USAGE: $COMMAND_NAME [-hv] [-f file_name] [CMD] [ARGS]"
104-
echo ""
105-
echo "OPTIONS:"
106-
echo " -h (--help) print this message"
107-
echo " -h (--help) print this message"
108-
echo " -v (--verbose) verbose logging"
109-
echo " -f (--file) file to test"
110-
echo ""
111-
echo "COMMANDS:"
112-
echo " run Run test cases specified in the test file."
113-
echo " Example: 'api-test -f test.json run test_case_1 test_case_2', 'api-test -f test.json run all'"
114-
exit
167+
case $1 in
168+
run)
169+
echo "USAGE: $COMMAND_NAME [-v] -f file_name run [-hiIs] [ARGS]"
170+
echo ""
171+
echo "OPTIONS:"
172+
echo " -h (--help) print this message"
173+
echo " -i (--include) include header"
174+
echo " -I (--header-only) header only"
175+
echo " -s (--silent) silent mode"
176+
echo ""
177+
echo "ARGS:"
178+
echo " all Run all test case."
179+
echo " <test_case_name> Run provided test case."
180+
echo ""
181+
echo "EXAMPLE:"
182+
echo "'api-test -f test.json run test_case_1 test_case_2', 'api-test -f test.json run all'"
183+
exit
184+
;;
185+
*)
186+
echo "USAGE: $COMMAND_NAME [-hv] -f file_name [CMD] [ARGS]"
187+
echo ""
188+
echo "OPTIONS:"
189+
echo " -h (--help) print this message"
190+
echo " -v (--verbose) verbose logging"
191+
echo " -f (--file) file to test"
192+
echo ""
193+
echo "COMMANDS:"
194+
echo " run Run test cases specified in the test file."
195+
echo " Example: 'api-test -f test.json run test_case_1 test_case_2', 'api-test -f test.json run all'"
196+
exit
197+
;;
198+
esac
115199
}
116200

117201
for arg in "$@"; do
@@ -144,6 +228,16 @@ if [ ! -f "$FILE" ]; then
144228
exit 1
145229
fi
146230

231+
cat $FILE | jq empty
232+
if [ $? -ne 0 ]; then
233+
echo "Empty file"
234+
exit
235+
fi
236+
URL=$(jq -r '.url' $FILE)
237+
ACCESS_TOKEN=$(jq -r '.accessToken' $FILE)
238+
ID_TOKEN=$(jq -r '.idToken' $FILE)
239+
COMMON_HEADER=$(cat $FILE | jq -r -c ". | .header | if . != null then . else {} end | to_entries | map(\"\(.key): \(.value|tostring)\") | join(\"\n\") | if ( . | length) != 0 then \"-H\" + . else \"-H \" end")
240+
147241
case $ACTION in
148242
run)
149243
run $@

0 commit comments

Comments
 (0)