Ben Ashton
2 years ago
7 changed files with 302 additions and 37 deletions
@ -1,33 +1,48 @@
|
||||
#!/bin/bash |
||||
printf '%s\n\n' 'Content-Type: text/html'; |
||||
|
||||
source exprash.sh; |
||||
|
||||
redirectStdout 'log'; |
||||
|
||||
declare -A cats; |
||||
cats[calico]="Calico"; |
||||
cats[sphynx]="Sphynx"; |
||||
cats[ragdoll]="Ragdoll"; |
||||
cats[holland_lop]="Holland Lop"; |
||||
cats[scottish_fold]="Scottish Fold"; |
||||
cats[orange]="Orange"; |
||||
|
||||
get '/' && { |
||||
printf '<h1>Cats</h1>\n'; |
||||
printf '<ul>\n'; |
||||
html='<h1>Cats</h1><ul>'; |
||||
for key in "${!cats[@]}"; do |
||||
printf '<li><a href="cats/%s">%s</a></li>\n' "$key" "${cats[$key]}"; |
||||
html+="<li><a href=\"cats/${key}\">${cats[$key]}</a></li>"; |
||||
done |
||||
printf '</ul>\n'; |
||||
html+='</ul>'; |
||||
|
||||
printf '%s' "$html" | send; |
||||
} |
||||
|
||||
get '/cats/:cat' && { |
||||
key=$(param 'cat'); |
||||
|
||||
if [[ -v "cats[$key]" ]]; then |
||||
printf '<h1>Your Cat: %s</h1>\n' "${cats[$key]}"; |
||||
if [ "$key" == 'holland_lop' ]; then |
||||
next "That's not a cat!"; |
||||
else |
||||
printf '<h1>Your Cat: %s</h1>\n' "${cats[$key]}" | send; |
||||
fi; |
||||
else |
||||
next; |
||||
fi; |
||||
} |
||||
|
||||
use && { |
||||
printf '<h1>Error: Cannot find that cat</h1>\n'; |
||||
get '/cats/*' && { |
||||
printf '<h1>Error: Cannot find that cat</h1>\n' | send; |
||||
} |
||||
|
||||
getError '/cats/*' && { |
||||
printf '<h1>%s</h1>' "$(errorMessage)" | send; |
||||
} |
||||
|
||||
(use || useError) && { |
||||
printf '<h1>404</h1>' | send; |
||||
} |
||||
|
@ -0,0 +1,10 @@
|
||||
#!/bin/bash |
||||
source "$(dirname "$0")/src/exprash.sh"; |
||||
|
||||
source "$(dirname "$0")/tests/utils.sh"; |
||||
|
||||
printf '%s\n' "Routes:"; |
||||
source "$(dirname "$0")/tests/routes.sh"; |
||||
|
||||
printf '\n'; |
||||
testSummary; |
@ -0,0 +1,55 @@
|
||||
it "Should match plain route" $({ |
||||
_exprashResetRouteGlobals |
||||
PATH_INFO='/plain/route' |
||||
all '/plain/route' |
||||
}) |
||||
|
||||
it "Should not match incorrect plain route" $({ |
||||
_exprashResetRouteGlobals |
||||
PATH_INFO='/plain/WRONG' |
||||
! all '/plain/route' |
||||
}) |
||||
|
||||
it "Should extract parameter" $({ |
||||
_exprashResetRouteGlobals |
||||
PATH_INFO='/cats/calico/pet' |
||||
all '/cats/:cat/pet' && hasParam 'cat' && [ "$(param 'cat')" == 'calico' ] |
||||
}) |
||||
|
||||
it "Should match wildcard route" $({ |
||||
_exprashResetRouteGlobals |
||||
PATH_INFO='/cats/calico/pet' |
||||
all '/cats/*/pet' |
||||
}) |
||||
|
||||
it "Should not match incorrect wildcard route" $({ |
||||
_exprashResetRouteGlobals |
||||
PATH_INFO='/cats/calico/' |
||||
! all '/cats/*/pet' |
||||
}) |
||||
|
||||
it "Should match multi-wildcard route" $({ |
||||
_exprashResetRouteGlobals |
||||
PATH_INFO='/cats/calico/pet/donkey' |
||||
all '/cats/**' |
||||
}) |
||||
|
||||
it "Should not match incorrect multi-wildcard route" $({ |
||||
_exprashResetRouteGlobals |
||||
PATH_INFO='/INCORRECT/calico/pet/donkey' |
||||
! all '/cats/**' |
||||
}) |
||||
|
||||
it "Should match get route" $({ |
||||
_exprashResetRouteGlobals |
||||
REQUEST_METHOD='GET' |
||||
PATH_INFO='/simple/route' |
||||
get '/simple/route' |
||||
}) |
||||
|
||||
it "Should not match get route with incorrect method" $({ |
||||
_exprashResetRouteGlobals |
||||
REQUEST_METHOD='POST' |
||||
PATH_INFO='/simple/route' |
||||
! get '/simple/route' |
||||
}) |
@ -0,0 +1,23 @@
|
||||
_testPassCount=0; |
||||
_testTotalCount=0; |
||||
|
||||
function it() { |
||||
local exitCode=$?; |
||||
local message=$1; |
||||
local status; |
||||
|
||||
_testTotalCount=$((_testTotalCount+1)); |
||||
|
||||
if [ "$exitCode" -eq 0 ]; then |
||||
status='✓'; |
||||
_testPassCount=$((_testPassCount+1)); |
||||
else |
||||
status='✗'; |
||||
fi; |
||||
|
||||
printf '%s %s\n' "$status" "$message"; |
||||
} |
||||
|
||||
function testSummary() { |
||||
printf 'Passed: %s of %s tests\n' "$_testPassCount" "$_testTotalCount"; |
||||
} |
Loading…
Reference in new issue