5 echo "Usage:$0 <URL-to-test>"
9 echo "Error: No test URL given." >&2
16 CURL="curl --silent --show-error"
22 echo $1 | sed -e "s/./$2/g"
38 printf "%*s" $(( 52 - ${#1} )) | tr ' ' '.'
39 (( tests_total++ )) || true
42 # Result of test depends on the exit status of last command
48 (( tests_failed++ )) || true
52 # If we got an argument, append it after test result
60 # Print report of all previous test results
67 if [ "$tests_failed" == "" ]; then
68 echo "All $tests_total tests passed."
72 echo "$tests_failed of $tests_total tests failed."
78 # Does a string contain a regular expression pattern
82 # contains "All's well that ends well" "s.well"
88 # POST to a URL endpoint with optional JSON data
92 # curl_post <ENDPOINT> [data] [CURL_OPTIONS]
95 $CURL ${3:-} -X POST ${2:+-H 'Content-Type: application/json' -d "$2"} $URL/$1
98 # PUT to a URL endpoint with optional JSON data
102 # curl_post <ENDPOINT> [data] [CURL_OPTIONS]
105 $CURL ${3:-} -X PUT ${2:+-H 'Content-Type: application/json' -d "$2"} $URL/$1
108 # GET from a URL endpoint
112 # curl_get <ENDPOINT> [CURL_OPTIONS]
118 # Create a new game of the specified engine type
125 curl_post new/$1 | jq -r .
128 TEST_SECTION "LMNO (super-site for games)"
130 TEST_SUBSECTION "Testing home page"
131 home_page=$($CURL $URL)
133 TEST "Contains 'Join Game'"
134 contains "$home_page" "Join Game"
137 TEST "Contains 'Host a new game'"
138 contains "$home_page" "Host a new game"
141 TEST_SUBSECTION "Creating some new games"
144 empires_game_id=$(new_game empires)
145 test "$empires_game_id" != ""
146 TEST_END $empires_game_id
149 tictactoe_game_id=$(new_game tictactoe)
150 test "$tictactoe_game_id" != ""
151 TEST_END $tictactoe_game_id
153 TEST_SUBSECTION "Test redirects"
155 TEST "Redirect of /GAMEID at top level"
156 redirect=$(curl_get $empires_game_id)
157 test "$redirect" = "Moved Permanently. Redirecting to /empires/$empires_game_id/"
160 TEST "Redirect of lowercase /gameid at top level"
161 empires_game_id_lower=$(tr '[:upper:]' '[:lower:]' <<< $empires_game_id)
162 redirect=$(curl_get $empires_game_id_lower)
163 test "$redirect" = "Moved Permanently. Redirecting to /$empires_game_id/"
166 TEST "Redirect of lowercase /empires/gameid"
167 redirect=$(curl_get empires/$empires_game_id_lower)
168 test "$redirect" = "Moved Permanently. Redirecting to /empires/$empires_game_id/"
171 TEST_SECTION "Empires game"
173 empires_game_path=empires/$empires_game_id
175 TEST_SUBSECTION "Empires game /register"
179 curl_post $empires_game_path/register "{\"name\": \"$1\", \"character\": \"$2\"}"
182 empires_players_string()
184 curl_get $empires_game_path/players | jq -r .[].name | tr '\n' ','
187 empires_characters_string()
189 curl_get $empires_game_path/characters | jq -r .[] | tr '\n' ','
192 TEST "Registering a player returns an ID"
193 carl_id=$(empires_register Carl "Bugs Bunny" | jq -r .)
194 test "$carl_id" = "1"
197 TEST "Registering several more players"
198 empires_register Richard "Bob Hope" > /dev/null
199 empires_register Kevin "Elvis Presley" > /dev/null
200 empires_register Stacy Phineas > /dev/null
201 empires_register David "Red Power Ranger" > /dev/null
202 empires_register Nancy "Audrey Hepburn" > /dev/null
203 bogus_id=$(empires_register Bogus "Mr. Bogus")
206 TEST 'Verify complete players list (with "Bogus")'
207 players=$(empires_players_string)
208 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,Bogus,"
211 TEST 'Verify complete players list (with "Mr. Bogus")'
212 characters=$(empires_characters_string)
213 test "$characters" = "Bugs Bunny,Bob Hope,Elvis Presley,Phineas,Red Power Ranger,Audrey Hepburn,Mr. Bogus,"
216 TEST_SUBSECTION "Empires game /deregister"
220 curl_post $empires_game_path/deregister/$1
223 TEST "Removing the bogus player"
224 empires_deregister $bogus_id
227 TEST 'Verify modified players list (w/o "Bogus")"'
228 players=$(empires_players_string)
229 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,"
232 TEST 'Verify modified characters list (w/o "Mr. Bogus")'
233 characters=$(empires_characters_string)
234 test "$characters" = "Bugs Bunny,Bob Hope,Elvis Presley,Phineas,Red Power Ranger,Audrey Hepburn,"
237 TEST_SUBSECTION "Empires game /capture"
241 curl_post $empires_game_path/capture/$1/$2
244 empires_empires_string()
246 # Get empires as a compact string (much more compact than JSON)
247 curl_get $empires_game_path/empires | jq -c '.[] | [.id,.captures]' | tr '\n' ','
250 TEST "Verify empires before any captures"
251 empires=$(empires_empires_string)
252 test "$empires" = "[1,[]],[2,[]],[3,[]],[4,[]],[5,[]],[6,[]],"
255 TEST "Perform some captures"
262 TEST "Verify empires after captures"
263 empires=$(empires_empires_string)
264 test "$empires" = "[1,[2]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
267 TEST_SUBSECTION "Empires game /liberate"
271 curl_post $empires_game_path/liberate/$1
274 TEST "Liberate a player"
278 TEST "Verify empires after liberate"
279 empires=$(empires_empires_string)
280 test "$empires" = "[1,[]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
283 TEST_SUBSECTION "Empires game /reset"
287 curl_post $empires_game_path/reset
290 TEST "Reset the game"
294 TEST "Verify players is now empty"
295 players=$(empires_players_string)
299 TEST_SECTION "Tic Tac Toe game"
301 tictactoe_game_path=tictactoe/$tictactoe_game_id
305 curl_post $tictactoe_game_path/move "{ \"move\": $1 }" "-b .test-cookie"
310 curl_put /profile "{ \"nickname\": \"$1\" }" "-c .test-cookie"
313 tictactoe_player_info()
315 curl_get $tictactoe_game_path/events "-m 0.1 -b .test-cookie" 2>&1 \
316 | grep player-info -A 1 \
320 tictactoe_player_name()
322 curl_put $tictactoe_game_path/player "{ \"name\": \"$1\" }" "-b .test-cookie"
325 tictactoe_player_team()
327 curl_put $tictactoe_game_path/player "{ \"team\": \"$1\" }" "-b .test-cookie"
330 TEST_SUBSECTION "Tic Tac Toe player-info"
332 TEST "Hit LMNO /profile to set name to 'curl'"
336 TEST "Verify player-info event reports 'curl' name"
337 result=$(tictactoe_player_info)
338 test "$result" = 'data: {"id":1,"name":"curl","team":""}'
341 TEST_SUBSECTION "Tic Tac Toe /player"
343 TEST "Change name to 'newname'"
344 tictactoe_player_name newname
345 result=$(tictactoe_player_info)
346 test "$result" = 'data: {"id":1,"name":"newname","team":""}'
349 TEST "Change team to 'X'"
350 tictactoe_player_team X
351 result=$(tictactoe_player_info)
352 test "$result" = 'data: {"id":1,"name":"newname","team":"X"}'
355 TEST "Change team to 'O'"
356 tictactoe_player_team O
357 result=$(tictactoe_player_info)
358 test "$result" = 'data: {"id":1,"name":"newname","team":"O"}'
361 TEST "Verify cannot change team to 'Z'"
362 tictactoe_player_team Z
363 result=$(tictactoe_player_info)
364 test "$result" = 'data: {"id":1,"name":"newname","team":"O"}'
367 TEST "Leave current team"
368 tictactoe_player_team ""
369 result=$(tictactoe_player_info)
370 test "$result" = 'data: {"id":1,"name":"newname","team":""}'
373 TEST_SUBSECTION "Tic Tac Toe /move"
375 TEST "First move doesn't require a team"
376 result=$(tictactoe_move 0)
377 test "$result" = '{"legal":true}'
380 TEST "Second move does require a team"
381 result=$(tictactoe_move 4)
382 test "$result" = '{"legal":false,"message":"It'"'"'s not your turn to move"}'
385 TEST "Illegal to move when it's not your turn"
386 tictactoe_player_team X
387 result=$(tictactoe_move 4)
388 test "$result" = '{"legal":false,"message":"It'"'"'s not your turn to move"}'
391 TEST "Legal move to center square"
392 tictactoe_player_team O
393 result=$(tictactoe_move 4)
394 test "$result" = '{"legal":true}'
397 TEST "Move to center square again is now illegal"
398 tictactoe_player_team X
399 result=$(tictactoe_move 4)
400 test "$result" = '{"legal":false,"message":"Square is already occupied"}'