-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathshakedown.sh
151 lines (123 loc) · 3.23 KB
/
shakedown.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env bash
set -u
# default values can be overriden by setting env vars
BASE_URL=${SHAKEDOWN_URL:-""}
CREDENTIALS=${SHAKEDOWN_CREDENTIALS:-""}
CONNECT_TIMEOUT=${SHAKEDOWN_CONNECT_TIMEOUT:-"5"}
MAX_TIME=${SHAKEDOWN_MAX_TIME:-"30"}
_usage() {
echo '
usage: $0 [options...]
Options:
-u <base URL> Base URL to test.
-c <user:password> Credentials for HTTP authentication.
'
exit 1
}
while getopts 'u:c:' OPTION
do
case $OPTION in
u) BASE_URL="$OPTARG";;
c) CREDENTIALS="$OPTARG";;
*) _usage;;
esac
done
echo "Starting shakedown of ${BASE_URL:-"[base URL not set]"}"
STATE=""
FAIL_COUNT=0
PASS_COUNT=0
WORKING_DIR=$(mktemp -d -t shakedown.XXXXXX)
RESPONSE_BODY="${WORKING_DIR}/body"
RESPONSE_HEADERS="${WORKING_DIR}/headers"
AUTH=""
if [ -n "${CREDENTIALS}" ]; then
AUTH="--anyauth --user ${CREDENTIALS}"
fi
CURL="curl -sS ${AUTH} -D ${RESPONSE_HEADERS} --connect-timeout ${CONNECT_TIMEOUT} --max-time ${MAX_TIME}"
CRED=$(tput setaf 1 2> /dev/null)
CGREEN=$(tput setaf 2 2> /dev/null)
CDEFAULT=$(tput sgr0 2> /dev/null)
_pass() {
echo " ${CGREEN}✔ ${1}${CDEFAULT}"
}
_fail() {
STATE="fail"
echo " ${CRED}✘ ${1}${CDEFAULT}"
}
_start_test() {
_finish_test
STATE="pass"
}
_finish_test() {
if [ "$STATE" = "pass" ]; then
PASS_COUNT=$((PASS_COUNT + 1))
elif [ "$STATE" = "fail" ]; then
FAIL_COUNT=$((FAIL_COUNT + 1))
fi
}
_finish() {
_finish_test
rm -rf "${WORKING_DIR}"
echo
MSG="Shakedown complete. ${PASS_COUNT} passed, ${FAIL_COUNT} failed."
[[ ${FAIL_COUNT} -eq 0 ]] && echo "${CGREEN}${MSG}${CDEFAULT}" || echo "${CRED}${MSG} You're busted.${CDEFAULT}"
exit ${FAIL_COUNT}
}
trap _finish EXIT
# start test
# $1 METHOD
# $2 URL
# $3..$n Custom CURL options
shakedown() {
_start_test
METHOD="$1"
URL="$2"
if ! [[ $URL == http* ]]; then
URL="${BASE_URL}${URL}"
fi
echo
echo "${METHOD} ${URL}"
METHOD_OPT="-X ${METHOD}"
if [ "${METHOD}" = "HEAD" ]; then
METHOD_OPT="-I"
fi
${CURL} ${METHOD_OPT} "${@:3}" "${URL}" > ${RESPONSE_BODY}
}
# assertions
header() {
grep -iFq "${1}" "${RESPONSE_HEADERS}" && _pass "header ${1}" || _fail "header ${1}"
}
no_header() {
grep -iFq "${1}" "${RESPONSE_HEADERS}" && _fail "no_header ${1}" || _pass "no_header ${1}"
}
status() {
STATUS_CODE=$(grep -Eo "^HTTP.+ [1-5][0-9][0-9] " ${RESPONSE_HEADERS} | grep -Eo '[1-5][0-9][0-9]' | tail -n1)
[[ "${STATUS_CODE}" = "${1}" ]] && _pass "status ${1}" || _fail "status ${1} (actual: ${STATUS_CODE})"
}
contains() {
MSG="contains \"${1}\""
grep -Fq "${1}" "${RESPONSE_BODY}" && _pass "${MSG}" || _fail "${MSG}"
}
matches() {
MSG="matches \"${1}\""
grep -Eq "${1}" "${RESPONSE_BODY}" && _pass "${MSG}" || _fail "${MSG}"
}
content_type() {
CT_HEADER="$(_get_header 'Content-Type')"
echo "${CT_HEADER}" | grep -Fq "${1}" && _pass "Content-Type: ${1}" || _fail "Content-Type: ${1} (actual: ${CT_HEADER})"
}
header_contains() {
HEADER_NAME=${1}
HEADER="$(_get_header $HEADER_NAME)"
echo "${HEADER}" | grep -Fq "${2}" && _pass "${HEADER_NAME}: ${2}" || _fail "${HEADER_NAME}: ${2} (actual: ${HEADER})"
}
_get_header() {
grep -i -F "${1}" "${RESPONSE_HEADERS}" | tr -d '\r'
}
# debug
print_headers() {
cat "${RESPONSE_HEADERS}"
}
print_body() {
cat "${RESPONSE_BODY}"
}