blob: 6476711fcf61ce5183156932fc367cbfbbecacef [file] [log] [blame]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001#!/bin/bash
2
3# ============LICENSE_START===============================================
4# Copyright (C) 2020 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
21
22# Function to execute curl towards a container (or process) and compare + print result
23# Intended use is for basic test scripts where testing is done with curl and the returned response and payload need to be checked.
BjornMagnussonXA05bfe482021-12-02 08:47:41 +010024# args: GET|PUT|POST|DELETE <url> <target-response-code> [<payload-file>]
BjornMagnussonXA80a92002020-03-19 14:31:06 +010025# All calls made to 'localhost:'<port>.
26# Expects env PORT set to intended port number
27# Expects env RESULT to contain the target response body.
BjornMagnussonXA72667f12020-04-24 09:20:18 +020028# Optional env HTTPX shall contain protocol 'http' or 'https'. If not set, 'http' is used. For 'https' all cert errors are ignored
BjornMagnussonXA05bfe482021-12-02 08:47:41 +010029# RESULT="*" means that returned payload is not checked, may contain any text
BjornMagnussonXA72667f12020-04-24 09:20:18 +020030# RESULT="<text>" means that the returned payload has to match the <text> exactly
BjornMagnussonXA80a92002020-03-19 14:31:06 +010031# RESULT="json:<returned-payload>" means that the returned json payload is compared with the expected result (order of json keys and index is irrelevant)
BjornMagnussonXA72667f12020-04-24 09:20:18 +020032# RESULT="json-array-size:<integer-size>" means that the returned json payload shall contain the number of element given by the <integer-size>
BjornMagnussonXA80a92002020-03-19 14:31:06 +010033# Env BODY contains the response body after the call
34# Any error will stop script execution
35# How to use in a test script: source this file into your bash test script to the make the function available.
BjornMagnussonXA7b36db62020-11-23 10:57:57 +010036# The function may create a dir 'tmp' for temporary files.
BjornMagnussonXA80a92002020-03-19 14:31:06 +010037
38do_curl() {
39 echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
40 if [ $# -lt 3 ]; then
41 echo "Need 3 or more parameters, <http-operation> <url> <response-code> [file]: "$@
42 echo "Exting test script....."
43 exit 1
44 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +020045
46 if [ -z $HTTPX ]; then
47 if [ "$HTTPX" != "http" ] && [ "$HTTPX" != "https" ]; then
48 echo "Env var HTTPX shall be set to 'http' or 'https'"
49 echo "Exting test script....."
50 exit 1
51 fi
52 PROT="http"
53 else
54 PROT=$HTTPX
55 fi
56
BjornMagnussonXA05bfe482021-12-02 08:47:41 +010057 req_content=""
58 if [ -z "$REQ_CONTENT" ]; then
59 if [ $# -gt 3 ]; then
60 req_content="-H Content-Type:application/json" #Assuming json
61 fi
62 else
63 req_content="-H Content-Type:$REQ_CONTENT"
64 fi
65 resp_content=""
66 if [ -z "$RESP_CONTENT" ]; then
67 if [[ "$RESULT" == "json"* ]]; then
68 resp_content="application/json"
69 elif [[ "$RESULT" == "*" ]]; then
70 resp_content=""
71 else
72 resp_content="text/plain"
73 fi
74 else
75 resp_content=$RESP_CONTENT
76 fi
77 curlstr="curl -X "$1" -skw :%{content_type}:%{http_code} ${PROT}://localhost:$PORT$2 -H accept:*/*"
BjornMagnussonXA80a92002020-03-19 14:31:06 +010078 if [ $# -gt 3 ]; then
BjornMagnussonXA05bfe482021-12-02 08:47:41 +010079 curlstr=$curlstr" $req_content --data-binary @"$4
BjornMagnussonXA80a92002020-03-19 14:31:06 +010080 fi
81 echo " CMD:"$curlstr
82 res=$($curlstr)
83 status=${res:${#res}-3}
BjornMagnussonXA05bfe482021-12-02 08:47:41 +010084 reminder=${res:0:${#res}-4}
85 content_type="${reminder##*:}"
86 body="${reminder%:*}"
87
BjornMagnussonXA80a92002020-03-19 14:31:06 +010088 export body
89 if [ $status -ne $3 ]; then
90 echo " Error status:"$status" Expected status: "$3
91 echo " Body: "$body
92 echo "Exting test script....."
93 exit 1
94 else
95 echo " OK, code: "$status" (Expected)"
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +010096 if [[ "$resp_content" == '*' ]]; then
97 :
98 elif [[ "$content_type" == *"$resp_content"* ]]; then
BjornMagnussonXA05bfe482021-12-02 08:47:41 +010099 echo " Content type: "$content_type" (Expected)"
100 else
101 echo " Expected content type: "$resp_content
102 echo " Got: "$content_type
103 echo "Exiting....."
104 exit 1
105 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100106 echo " Body: "$body
107 if [ "$RESULT" == "*" ]; then
108 echo " Body contents not checked"
109 elif [[ "$RESULT" == "json:"* ]]; then
110 result=${RESULT:5:${#RESULT}}
111 #Find dir of the common dir
112 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
113 res=$(python ${DIR}/compare_json.py "$result" "$body")
114 if [ $res -eq 0 ]; then
115 echo " Body as expected"
116 else
117 echo " Expected json body: "$result
118 echo "Exiting....."
119 exit 1
120 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200121 elif [[ "$RESULT" == "json-array-size:"* ]]; then
122 count=${RESULT:16:${#RESULT}}
123 #Find dir of the common dir
124 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100125 mkdir -p tmp
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200126 echo $body > ./tmp/.tmp.json
127 res=$(python ${DIR}/count_json_elements.py ./tmp/.tmp.json)
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200128 if [ $res -eq $count ]; then
129 echo " Body (array size) as expected"
130 else
131 echo " Expected json array size: "$count
132 echo "Exiting....."
133 exit 1
134 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100135 else
136 body="$(echo $body | tr -d '\n' )"
137 if [ "$RESULT" == "$body" ]; then
138 echo " Body as expected"
139 else
140 echo " Expected body: "$RESULT
141 echo "Exiting....."
142 exit 1
143 fi
144 fi
145 fi
146}