Skip to content

Commit daba205

Browse files
committed
Add header parsing method
1 parent 3f96e2f commit daba205

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

auto_test.sh

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,34 @@ api_factory() {
5151
ROUTE=$(jq -r ".testCases.$TEST_CASE.path" $FILE)
5252
BODY="$(jq -r ".testCases.$TEST_CASE.body" $FILE)"
5353
QUERY_PARAMS=$(cat $FILE | jq -r ".testCases.$TEST_CASE | select(.query != null) | .query | to_entries | map(\"\(.key)=\(.value|tostring)\") | join(\"&\") | \"?\" + . ")
54+
METHOD="$(jq -r ".testCases.$TEST_CASE.method //\"GET\"" $FILE)"
5455
call_api
5556
done
5657
}
5758

5859
call_api() {
59-
curl -is --request POST $URL$ROUTE$QUERY_PARAMS \
60-
--header "Authorization: Bearer $ID_TOKEN:$ACCESS_TOKEN" \
61-
--data "$BODY" -w '\nTotal Time: %{time_total}s' | jq -C -R -r '. as $line | try fromjson catch $line'
62-
echo ""
63-
echo ""
60+
# curl -is --request POST $URL$ROUTE$QUERY_PARAMS \
61+
# --header "Authorization: Bearer $ID_TOKEN:$ACCESS_TOKEN" \
62+
# --data "$BODY" -w '\n\n\n\n%{json}'
63+
local raw_output=$(curl -is --request $METHOD $URL$ROUTE$QUERY_PARAMS \
64+
--header "Authorization: Bearer $ACCESS_TOKEN : $ID_TOKEN" \
65+
--data "$BODY" -w '\n{ "ResponseTime": "%{time_total}s" }')
66+
local header="$(awk -v bl=1 'bl{bl=0; h=($0 ~ /HTTP\//)} /^\r?$/{bl=1} {if(h)print $0 }' <<<"$raw_output")"
67+
local json=$(jq -c -R -r '. as $line | try fromjson' <<<"$raw_output")
68+
BODY=$(sed -n 1p <<<"$json")
69+
META=$(sed 1d <<<"$json")
70+
parse_header "$header"
71+
echo "$HEADER"
72+
}
73+
74+
function parse_header() {
75+
local RESPONSE=($(echo "$header" | tr '\r' ' ' | sed -n 1p))
76+
local header=$(echo "$header" | sed '1d;$d' | sed 's/: /" : "/' | sed 's/^/"/' | tr '\r' ' ' | sed 's/ $/",/' | sed '1 s/^/{/' | sed '$ s/,$/}/' | jq)
77+
# echo "$HEADER"
78+
HEADER=$(echo "$header" "{ \"http_version\": \"${RESPONSE[0]}\",
79+
\"http_status\": \"${RESPONSE[1]}\",
80+
\"http_message\": \"${RESPONSE[@]:2}\",
81+
\"http_response\": \"${RESPONSE[@]:0}\" }" | jq -s add)
6482
}
6583

6684
# Show usage and exit

0 commit comments

Comments
 (0)