-
Notifications
You must be signed in to change notification settings - Fork 1
/
.client.sh
62 lines (51 loc) · 1.35 KB
/
.client.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
#
# Wrapper for cURL command that is run from inside the container network.
#
call_curl() {
exec_idm curl "$@"
}
#
# Get response body from request passed through stdin or as a function argument.
#
get_response_body() {
local input=${1:-}
if [ "$input" = "-" ]; then
input=$(cat)
fi
echo -n "$input" | sed -r '0,/^[\r\n]+/d'
}
#
# Get response status code from request passed through stdin or as a function argument.
#
get_response_status() {
local input=${1:-}
if [ "$input" = "-" ]; then
input=$(cat)
fi
echo -n "$input" | head -n 1 | cut -d' ' -f2
}
#
# Assert response status code value.
#
assert_response_status() {
local expected_status=${1:-200}
local response=$(cat)
local response_status=$(get_response_status "$response")
[ "$response_status" -eq "$expected_status" ] || fail_test "Invalid response code" "$response"
echo -n "$response"
}
#
# Assert JSON response body content.
#
# Usage: assert_response_body <jq_expression>
#
assert_response_body() {
local expression="$1"
local response=$(cat)
local response_body=$(get_response_body "$response")
[ -n "$response_body" ] || fail_test "Unable to test empty response with: $expression"
echo -n "$response_body" | jq -e "$expression" > /dev/null \
|| fail_test "Failed matching response with: $expression" "$response_body"
echo -n "$response"
}