Create an external web server building RESTful API
[sim/a1-interface.git] / near-rt-ric-simulator / test / common / elapse_time_curl.sh
1 #!/bin/bash
2
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
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
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=================================================
18 #
19
20 # Function to execute curl and compare + print result
21
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'
24
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() {
30     if [ $# -lt 4 ]; then
31         echo "Need 4 or more parameters, <http-operation> <url> <response-code> <delay-time> [file]: "$@
32         echo "Exiting test script....."
33         exit 1
34     fi
35
36     #capture total elapsetime via ~%{time_total}
37     curlstr="curl -X "$1" -skw %{http_code}~%{time_total} $HTTPX://localhost:"${PORT}${2}" -H accept:*/*"
38     if [ $# -gt 4 ]; then
39         curlstr=$curlstr" -H Content-Type:application/json --data-binary @"$4
40     fi
41     echo "  CMD (${BASH_LINENO[0]}):"$curlstr
42     res=$($curlstr)
43
44     #target delay time for a single request
45     delay_time=$4
46
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)
51
52     res=$left
53     status=${res:${#res}-3}
54     body=${res:0:${#res}-3}
55
56     if [ $status -ne $3 ]; then
57         echo "  Error status :"$status" Expected status: "$3
58         echo "  Body         :"$body
59         echo "Exiting test script....."
60         exit 1
61     elif [ $elapsedtime -lt $delay_time ]; then
62         echo "  Elapsed time :"$elapsedtime" Expected delay time:"$delay_time "seconds"
63         echo "Exiting test script....."
64         exit 1
65     else
66         echo "  Delay OK           :"$elapsedtime"     (Expected)"
67         echo "  Body               :"$body
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"
76             else
77                 echo "  Expected json body :"$result
78                 echo "Exiting....."
79                 exit 1
80             fi
81         else
82             body="$(echo $body | tr -d '\n' )"
83             if [ "$RESULT" == "$body" ]; then
84                 echo "  Expected body      :"$RESULT
85                 echo "  Body as expected"
86             else
87                 echo "  Expected body      :"$RESULT
88                 echo "Exiting....."
89                 exit 1
90             fi
91         fi
92     fi
93 }