Skip to content

Commit 37e8075

Browse files
committed
Add option to check that missing endpoints give right errors.
1 parent 59cce30 commit 37e8075

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

contest-api/check-api.sh

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ team-members
3030
awards
3131
'
3232

33+
ENDPOINTS_TO_FAIL='
34+
404:doesnt-exist
35+
404:doesnt-exist/42
36+
404:submissions/999999
37+
'
38+
3339
ENDPOINTS_CHECK_CONSISTENT="$ENDPOINTS"
3440
for endpoint in $ENDPOINTS_OPTIONAL scoreboard ; do
3541
ENDPOINTS_CHECK_CONSISTENT="${ENDPOINTS_CHECK_CONSISTENT/$endpoint/}"
@@ -84,6 +90,7 @@ Options:
8490
-C Check internal consistency between REST endpoints and event feed.
8591
-c OPTS Options to pass to curl to request API data (default: $CURL_OPTIONS)
8692
-d Turn on shell script debugging.
93+
-e Check correct HTTP error codes for non-existent endpoints.
8794
-h Snow this help output.
8895
-j PROG Specify the path to the 'validate-json' binary.
8996
-n Require that all collection endpoints are non-empty.
@@ -102,12 +109,13 @@ CURL_OPTIONS='-n -s'
102109
URL_ARGS=''
103110

104111
# Parse command-line options:
105-
while getopts 'a:Cc:dhj:npt:q' OPT ; do
112+
while getopts 'a:Cc:dehj:npt:q' OPT ; do
106113
case "$OPT" in
107114
a) URL_ARGS="$OPTARG" ;;
108115
C) CHECK_CONSISTENCY=1 ;;
109116
c) CURL_OPTIONS="$OPTARG" ;;
110117
d) export DEBUG=1 ;;
118+
e) CHECK_ERRORS=1 ;;
111119
h) usage ; exit 0 ;;
112120
j) VALIDATE_JSON="$OPTARG" ;;
113121
n) NONEMPTY=1 ;;
@@ -148,6 +156,8 @@ query_endpoint()
148156
local OUTPUT="$1"
149157
local URL="$2"
150158
local OPTIONAL="$3"
159+
local EXPECTED_HTTPCODE="$4"
160+
151161
local HTTPCODE EXITCODE
152162

153163
local CURLOPTS="$CURL_OPTIONS"
@@ -163,6 +173,15 @@ query_endpoint()
163173

164174
HTTPCODE=$(curl $CURLOPTS -w "%{http_code}\n" -o "$OUTPUT" "${URL}${ARGS:+?$ARGS}")
165175
EXITCODE="$?"
176+
177+
if [ -n "$EXPECTED_HTTPCODE" ]; then
178+
if [ "$HTTPCODE" -ne "$EXPECTED_HTTPCODE" ]; then
179+
verbose "Warning: curl returned HTTP status $HTTPCODE != $EXPECTED_HTTPCODE for '$URL'."
180+
return 1;
181+
fi
182+
return 0
183+
fi
184+
166185
if [ $EXITCODE -eq 28 ]; then # timeout
167186
if [ -z "$TIMEOUT" ]; then
168187
verbose "Warning: curl request timed out for '$URL'."
@@ -289,6 +308,19 @@ for CONTEST in $CONTESTS ; do
289308

290309
done
291310

311+
if [ -n "$CHECK_ERRORS" ]; then
312+
verbose "Validating errors on missing endpoints..."
313+
for i in $ENDPOINTS_TO_FAIL ; do
314+
CODE=${i%%:*}
315+
ENDPOINT=${i#*:}
316+
URL="$CONTEST_URL/$ENDPOINT"
317+
verbose '%20s: ' "$ENDPOINT"
318+
if query_endpoint /dev/null "$URL" '' "$CODE" ; then
319+
verbose 'OK (returned %s)\n' "$CODE"
320+
fi
321+
done
322+
fi
323+
292324
[ -n "$DEBUG" ] || rm -rf $TMP
293325

294326
exit $EXITCODE

0 commit comments

Comments
 (0)