Skip to content

Commit f2d4d34

Browse files
committed
Add size filed and organize display
1 parent 22233e3 commit f2d4d34

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

api-test.sh

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ echo_v() {
2828
fi
2929
}
3030

31+
bytes_to_human() {
32+
b=${1:-0}
33+
d=''
34+
s=0
35+
S=(Bytes {K,M,G,T,E,P,Y,Z}B)
36+
while ((b > 1024)); do
37+
d="$(printf ".%02d" $((b % 1024 * 100 / 1024)))"
38+
b=$((b / 1024))
39+
let s++
40+
done
41+
echo "$b$d ${S[$s]}"
42+
}
43+
3144
run() {
3245
for arg in "$@"; do
3346
case $arg in
@@ -60,19 +73,16 @@ api_factory() {
6073
for TEST_CASE in $@; do
6174
echo "${BOLD}Running Case:${RESET} $TEST_CASE"
6275
echo_v "${BOLD}Description: ${RESET}$(jq -r ".testCases.$TEST_CASE.description" $FILE)"
63-
ROUTE=$(jq -r ".testCases.$TEST_CASE.path" $FILE)
64-
BODY="$(jq -r ".testCases.$TEST_CASE.body" $FILE)"
65-
QUERY_PARAMS=$(cat $FILE | jq -r ".testCases.$TEST_CASE | select(.query != null) | .query | to_entries | map(\"\(.key)=\(.value|tostring)\") | join(\"&\") | \"?\" + . ")
66-
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")
67-
METHOD="$(jq -r ".testCases.$TEST_CASE.method //\"GET\" | ascii_upcase" $FILE)"
68-
call_api
76+
echo_v "${BOLD}Action: ${RESET}$(jq -r ".testCases.$TEST_CASE.method //\"GET\" | ascii_upcase" $FILE) $(jq -r ".testCases.$TEST_CASE.path" $FILE)"
77+
call_api $TEST_CASE
6978
display_results
7079
done
7180
}
7281

7382
display_results() {
7483
local res=$(jq -r '.http_status + " " + .http_message ' <<<"$HEADER")
7584
local status=$(jq -r '.http_status' <<<"$HEADER")
85+
echo "Response:"
7686
echo "${BOLD}$(color_response $status)$res${RESET}"
7787
if [[ $HEADER_ONLY == 1 ]]; then
7888
echo "HEADER:"
@@ -88,7 +98,6 @@ display_results() {
8898
fi
8999

90100
fi
91-
92101
echo "META:"
93102
echo "$META" | jq -C
94103
echo ""
@@ -104,6 +113,11 @@ color_response() {
104113
}
105114

106115
call_api() {
116+
ROUTE=$(jq -r ".testCases.$1.path" $FILE)
117+
BODY="$(jq -r ".testCases.$1.body" $FILE)"
118+
QUERY_PARAMS=$(cat $FILE | jq -r ".testCases.$1 | select(.query != null) | .query | to_entries | map(\"\(.key)=\(.value|tostring)\") | join(\"&\") | \"?\" + . ")
119+
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")
120+
METHOD="$(jq -r ".testCases.$1.method //\"GET\" | ascii_upcase" $FILE)"
107121
# curl -ivs --request $METHOD "$URL$ROUTE$QUERY_PARAMS" \
108122
# --data "$BODY" \
109123
# "$COMMON_HEADER" \
@@ -113,7 +127,7 @@ call_api() {
113127
--data "$BODY" \
114128
"$COMMON_HEADER" \
115129
"$REQUEST_HEADER" \
116-
-w '\n{ "ResponseTime": "%{time_total}s" }' || echo "AUTO_API_ERROR")
130+
-w '\n{ "ResponseTime": "%{time_total}s", "Size": %{size_download} }' || echo "AUTO_API_ERROR")
117131

118132
if [[ $raw_output == *"AUTO_API_ERROR"* ]]; then
119133
echo "Problem connecting to $URL"
@@ -123,20 +137,20 @@ call_api() {
123137
local json=$(jq -c -R -r '. as $line | try fromjson' <<<"$raw_output")
124138
BODY=$(sed -n 1p <<<"$json")
125139
META=$(sed 1d <<<"$json")
140+
META=$(jq -r ".Size = \"$(bytes_to_human $(jq -r '.Size' <<<"$META"))\"" <<<"$META")
126141
parse_header "$header"
127142
}
128143

129144
function parse_header() {
130145
local RESPONSE=($(echo "$header" | tr '\r' ' ' | sed -n 1p))
131146
local header=$(echo "$header" | sed '1d;$d' | sed 's/: /" : "/' | sed 's/^/"/' | tr '\r' ' ' | sed 's/ $/",/' | sed '1 s/^/{/' | sed '$ s/,$/}/' | jq)
132-
# echo "$HEADER"
133147
HEADER=$(echo "$header" "{ \"http_version\": \"${RESPONSE[0]}\",
134148
\"http_status\": \"${RESPONSE[1]}\",
135149
\"http_message\": \"${RESPONSE[@]:2}\",
136150
\"http_response\": \"${RESPONSE[@]:0}\" }" | jq -s add)
137151
}
138152

139-
# Show usage and exit
153+
# Show usage
140154
function usage() {
141155
echo "USAGE: $COMMAND_NAME [-hv] [-f file_name] [CMD] [ARGS]"
142156
echo ""

0 commit comments

Comments
 (0)