3 # ============LICENSE_START===============================================
4 # Copyright (C) 2022 Nordix Foundation. All rights reserved.
5 # ========================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=================================================
20 # Function to execute curl and compare + print result
22 # Note: Env var PORT must be set to the intended port number
23 # Notre Env var HTTPX must be set to either 'http' or 'https'
25 #args: <http-operation> <url> <response-code> [file]
26 #Expects the env $RESULT to contain the expected RESULT.
27 #If json, the RESULT shall begin with 'json:'.
28 #Any json parameter with unknown value shall be given as "????" to skip checking the value.
29 do_elapsetime_curl() {
31 echo "Need 4 or more parameters, <http-operation> <url> <response-code> <delay-time> [file]: "$@
32 echo "Exiting test script....."
36 #capture total elapsetime via ~%{time_total}
37 curlstr="curl -X "$1" -skw %{http_code}~%{time_total} $HTTPX://localhost:"${PORT}${2}" -H accept:*/*"
39 curlstr=$curlstr" -H Content-Type:application/json --data-binary @"$4
41 echo " CMD (${BASH_LINENO[0]}):"$curlstr
44 #target delay time for a single request
47 #extract time_total and get rid of its decimals points
48 left=$(echo $res | cut -d'~' -f1)
49 right=$(echo $res | cut -d'~' -f2)
50 elapsedtime=$(echo $right | cut -d'.' -f1)
53 status=${res:${#res}-3}
54 body=${res:0:${#res}-3}
56 if [ $status -ne $3 ]; then
57 echo " Error status :"$status" Expected status: "$3
59 echo "Exiting test script....."
61 elif [ $elapsedtime -lt $delay_time ]; then
62 echo " Elapsed time :"$elapsedtime" Expected delay time:"$delay_time "seconds"
63 echo "Exiting test script....."
66 echo " Delay OK :"$elapsedtime" (Expected)"
68 if [ "$RESULT" == "*" ]; then
69 echo " Body contents not checked"
70 elif [[ "$RESULT" == "json:"* ]]; then
71 result=${RESULT:5:${#RESULT}} #Remove 'json:' from the result string
72 res=$(python ../common/compare_json.py "$result" "$body")
73 if [ $res -eq 0 ]; then
74 echo " Expected json body :"$result
75 echo " Body as expected"
77 echo " Expected json body :"$result
82 body="$(echo $body | tr -d '\n' )"
83 if [ "$RESULT" == "$body" ]; then
84 echo " Expected body :"$RESULT
85 echo " Body as expected"
87 echo " Expected body :"$RESULT