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]
95 $CURL -X POST ${2:+-H 'Content-Type: application/json' -d "$2"} $URL/$1
98 # Get form a URL endpoint
102 # curl_get <ENDPOINT>
108 # Create a new game of the specified engine type
115 curl_post new/$1 | jq -r .
118 TEST_SECTION "LMNO (super-site for games)"
120 TEST_SUBSECTION "Testing home page"
121 home_page=$($CURL $URL)
123 TEST "Contains 'Join Game'"
124 contains "$home_page" "Join Game"
127 TEST "Contains 'Host a new game'"
128 contains "$home_page" "Host a new game"
131 TEST_SUBSECTION "Creating some new games"
134 empires_game_id=$(new_game empires)
135 test "$empires_game_id" != ""
136 TEST_END $empires_game_id
139 tictactoe_game_id=$(new_game tictactoe)
140 test "$tictactoe_game_id" != ""
141 TEST_END $tictactoe_game_id
143 TEST_SUBSECTION "Test redirects"
145 TEST "Redirect of /GAMEID at top level"
146 redirect=$(curl_get $empires_game_id)
147 test "$redirect" = "Moved Permanently. Redirecting to /empires/$empires_game_id/"
150 TEST "Redirect of lowercase /gameid at top level"
151 empires_game_id_lower=$(tr '[:upper:]' '[:lower:]' <<< $empires_game_id)
152 redirect=$(curl_get $empires_game_id_lower)
153 test "$redirect" = "Moved Permanently. Redirecting to /$empires_game_id/"
156 TEST "Redirect of lowercase /empires/gameid"
157 redirect=$(curl_get empires/$empires_game_id_lower)
158 test "$redirect" = "Moved Permanently. Redirecting to /empires/$empires_game_id/"
161 TEST_SECTION "Empires game"
163 empires_game_path=empires/$empires_game_id
165 TEST_SUBSECTION "Empires game /register"
169 curl_post $empires_game_path/register "{\"name\": \"$1\", \"character\": \"$2\"}"
172 empires_players_string()
174 curl_get $empires_game_path/players | jq -r .[].name | tr '\n' ','
177 empires_characters_string()
179 curl_get $empires_game_path/characters | jq -r .[] | tr '\n' ','
182 TEST "Registering a player returns an ID"
183 carl_id=$(empires_register Carl "Bugs Bunny" | jq -r .)
184 test "$carl_id" = "1"
187 TEST "Registering several more players"
188 empires_register Richard "Bob Hope" > /dev/null
189 empires_register Kevin "Elvis Presley" > /dev/null
190 empires_register Stacy Phineas > /dev/null
191 empires_register David "Red Power Ranger" > /dev/null
192 empires_register Nancy "Audrey Hepburn" > /dev/null
193 bogus_id=$(empires_register Bogus "Mr. Bogus")
196 TEST 'Verify complete players list (with "Bogus")'
197 players=$(empires_players_string)
198 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,Bogus,"
201 TEST 'Verify complete players list (with "Mr. Bogus")'
202 characters=$(empires_characters_string)
203 test "$characters" = "Bugs Bunny,Bob Hope,Elvis Presley,Phineas,Red Power Ranger,Audrey Hepburn,Mr. Bogus,"
206 TEST_SUBSECTION "Empires game /deregister"
210 curl_post $empires_game_path/deregister/$1
213 TEST "Removing the bogus player"
214 empires_deregister $bogus_id
217 TEST 'Verify modified players list (w/o "Bogus")"'
218 players=$(empires_players_string)
219 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,"
222 TEST 'Verify modified characters list (w/o "Mr. Bogus")'
223 characters=$(empires_characters_string)
224 test "$characters" = "Bugs Bunny,Bob Hope,Elvis Presley,Phineas,Red Power Ranger,Audrey Hepburn,"
227 TEST_SUBSECTION "Empires game /capture"
231 curl_post $empires_game_path/capture/$1/$2
234 empires_empires_string()
236 # Get empires as a compact string (much more compact than JSON)
237 curl_get $empires_game_path/empires | jq -c '.[] | [.id,.captures]' | tr '\n' ','
240 TEST "Verify empires before any captures"
241 empires=$(empires_empires_string)
242 test "$empires" = "[1,[]],[2,[]],[3,[]],[4,[]],[5,[]],[6,[]],"
245 TEST "Perform some captures"
252 TEST "Verify empires after captures"
253 empires=$(empires_empires_string)
254 test "$empires" = "[1,[2]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
257 TEST_SUBSECTION "Empires game /liberate"
261 curl_post $empires_game_path/liberate/$1
264 TEST "Liberate a player"
268 TEST "Verify empires after liberate"
269 empires=$(empires_empires_string)
270 test "$empires" = "[1,[]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
273 TEST_SUBSECTION "Empires game /reset"
277 curl_post $empires_game_path/reset
280 TEST "Reset the game"
284 TEST "Verify players is now empty"
285 players=$(empires_players_string)
289 TEST_SECTION "Tic Tac Toe game"
291 tictactoe_game_path=tictactoe/$tictactoe_game_id
295 curl_post $tictactoe_game_path/move "{ \"move\": $1 }"
298 TEST_SUBSECTION "Tic Tac Toe /move"
300 TEST "Move to the center square"
301 result=$(tictactoe_move 4)
302 test "$result" = "true"
305 TEST "Move to center square again is now illegal"
306 result=$(tictactoe_move 4)
307 test "$result" = "false"