diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5310f05..d73b148 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,6 +20,7 @@ jobs: coverage: none - run: make - run: make served + - run: bash tests/await.bash http://clue.localhost/ - run: make test - run: git config --global user.name "GitHub Actions" && git config --global user.email "actions@github.com" - run: git config --global url."https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/".insteadOf "https://github.com/" diff --git a/Makefile b/Makefile index a94a05a..f0c5c1e 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ served: build @echo Container running. Use \"docker rm -f {containerId}\" to stop container. test: - bash tests/acceptance.sh + bash tests/integration.bash http://clue.localhost/ test -z "$$(git status --porcelain)" || (echo Directory is dirty && git status && exit 1) deploy: diff --git a/README.md b/README.md index ec85f99..83127f5 100644 --- a/README.md +++ b/README.md @@ -34,11 +34,11 @@ like this: make test ``` -> This test assumes you're running the above web server container on -> `http://clue.localhost`. You may test other deployments like this: +> If you don't want to test this against the local container, you can optionally +> pass in a different base URL like this: > > ```bash -> tests/acceptance.sh https://clue.example +> tests/integration.bash http://clue.localhost/ > ``` Once done, you can clean up like this: diff --git a/tests/acceptance.sh b/tests/acceptance.sh deleted file mode 100755 index 0292c67..0000000 --- a/tests/acceptance.sh +++ /dev/null @@ -1,42 +0,0 @@ -#!/bin/bash - -#run with base url argument like "http://clue.localhost" or "https://user:pass@clue.example" -base=${1:-http://clue.localhost} -redir=$(echo $base | sed "s,://.*@,://,g") -echo -n "Testing $redir" - -n=0 -match() { - n=$[$n+1] - echo "$out" | grep "$@" >/dev/null && echo -n . || \ - (echo ""; echo "Error in test $n: Unable to \"grep $@\" this output:"; echo "$out"; exit 1) || exit 1 -} - -out=$(curl -v $base/ 2>&1) && match "HTTP/.* 200" -out=$(curl -v $base/index.html 2>&1) && match -i "Location: $redir" -out=$(curl -v $base/index 2>&1) && match "HTTP/.* 404" - -out=$(curl -v $base/blog 2>&1) && match "HTTP/.* 200" -out=$(curl -v $base/blog.html 2>&1) && match -i "Location: $redir/blog" -out=$(curl -v $base/blog/ 2>&1) && match -i "Location: $redir/blog" - -out=$(curl -v $base/2019 2>&1) && match -i "Location: $redir/blog#2019" -out=$(curl -v $base/2019/ 2>&1) && match -i "Location: $redir/blog#2019" -out=$(curl -v $base/2000 2>&1) && match "HTTP/.* 404" -out=$(curl -v $base/2000/ 2>&1) && match "HTTP/.* 404" - -out=$(curl -v $base/2018/hello-world 2>&1) && match "HTTP/.* 200" -out=$(curl -v $base/2018/hello-world/ 2>&1) && match -i "Location: $redir/2018/hello-world" - -out=$(curl -v $base/contact 2>&1) && match "HTTP/.* 200" -out=$(curl -v $base/contact -X POST 2>&1) && match "HTTP/.* 400" -out=$(curl -v $base/contact --data name=A --data email=alice@example.com --data company=ACME --data budget=None --data message="Let's get in touch!" 2>&1) && match "HTTP/.* 400" # name length -out=$(curl -v $base/contact --data name=Alice --data email=alice --data company=ACME --data budget=None --data message="Let's get in touch!" 2>&1) && match "HTTP/.* 400" # email invalid -out=$(curl -v $base/contact --data name=Alice --data email=alice@example.com --data company=ACME --data budget=A --data message="Let's get in touch!" 2>&1) && match "HTTP/.* 400" # budget invalid -# out=$(curl -v $base/contact --data name=Alice --data email=alice@example.com --data company= --data budget=Yes --data message="Let's get in touch!!" 2>&1) && match "HTTP/.* 302" # valid without company -# out=$(curl -v $base/contact --data name=Alice --data email=alice@example.com --data company=ACME --data budget=Yes --data message="Let's get in touch!!" 2>&1) && match "HTTP/.* 302" # valid with company -out=$(curl -v $base/contact.html 2>&1) && match -i "Location: $redir/contact" -out=$(curl -v $base/contact.php 2>&1) && match -i "Location: $redir/contact" -out=$(curl -v $base/contact/ 2>&1) && match -i "Location: $redir/contact" - -echo "OK ($n)" diff --git a/tests/await.bash b/tests/await.bash new file mode 100755 index 0000000..7b098b6 --- /dev/null +++ b/tests/await.bash @@ -0,0 +1,16 @@ +#!/bin/bash + +# @copyright Copyright (c) 2024 Christian Lück, taken from https://github.com/clue/framework-x/pull/3 with permission + +base=${1:-http://clue.localhost/} +base=${base%/} + +for i in {1..600} +do + out=$(curl -v -X PROBE $base/ 2>&1) && exit 0 || echo -n . + sleep 0.1 +done + +echo +echo "$out" +exit 1 diff --git a/tests/integration.bash b/tests/integration.bash new file mode 100755 index 0000000..879811f --- /dev/null +++ b/tests/integration.bash @@ -0,0 +1,102 @@ +#!/bin/bash + +# run with base url argument like "http://clue.localhost" or "https://user:pass@clue.example" +base=${1:-http://clue.localhost/} +base=${base%/} + +# base url with any userinfo removed +redir=$(echo $base | sed "s,://.*@,://,g") + +n=0 +curl() { + out=$($(which curl) "$@" 2>&1); +} +match() { + n=$[$n+1] + echo "$out" | grep "$@" >/dev/null && echo -n . || \ + (echo ""; echo "Error in test $n: Unable to \"grep $@\" this output:"; echo "$out"; exit 1) || exit 1 +} + +curl -v $base/ +match "HTTP/.* 200" +match -iP "Content-Type: text/html[;\r\n]" + +curl -v $base/index.html +match "HTTP/.* 302" +match -iP "Location: $redir/[\r\n]" + +curl -v $base/index.html/ +match "HTTP/.* 404" + +curl -v $base/index +match "HTTP/.* 404" + +curl -v $base/blog +match "HTTP/.* 200" +match -iP "Content-Type: text/html[;\r\n]" + +curl -v $base/blog.html +match "HTTP/.* 302" +match -iP "Location: $redir/blog[\r\n]" + +curl -v $base/blog/ +match "HTTP/.* 302" +match -iP "Location: $redir/blog[\r\n]" + +curl -v $base/2019 +match "HTTP/.* 302" +match -iP "Location: $redir/blog#2019[\r\n]" + +curl -v $base/2019/ +match "HTTP/.* 302" +match -iP "Location: $redir/blog#2019[\r\n]" + +curl -v $base/2000 +match "HTTP/.* 404" + +curl -v $base/2000/ +match "HTTP/.* 404" + +curl -v $base/2018/hello-world +match "HTTP/.* 200" +match -iP "Content-Type: text/html[;\r\n]" + +curl -v $base/2018/hello-world/ +match "HTTP/.* 302" +match -iP "Location: $redir/2018/hello-world[\r\n]" + +curl -v $base/contact +match "HTTP/.* 200" +match -iP "Content-Type: text/html[;\r\n]" + +curl -v $base/contact -X POST +match "HTTP/.* 400" + +curl -v $base/contact --data name=A --data email=alice@example.com --data company=ACME --data budget=None --data message="Let's get in touch!" +match "HTTP/.* 400" # name length + +curl -v $base/contact --data name=Alice --data email=alice --data company=ACME --data budget=None --data message="Let's get in touch!" +match "HTTP/.* 400" # email invalid + +curl -v $base/contact --data name=Alice --data email=alice@example.com --data company=ACME --data budget=A --data message="Let's get in touch!" +match "HTTP/.* 400" # budget invalid + +# curl -v $base/contact --data name=Alice --data email=alice@example.com --data company= --data budget=Yes --data message="Let's get in touch!!" +# match "HTTP/.* 302" # valid without company + +# curl -v $base/contact --data name=Alice --data email=alice@example.com --data company=ACME --data budget=Yes --data message="Let's get in touch!!" +# match "HTTP/.* 302" # valid with company + +curl -v $base/contact.html +match "HTTP/.* 302" +match -iP "Location: $redir/contact[\r\n]" + +curl -v $base/contact.php +match "HTTP/.* 302" +match -iP "Location: $redir/contact[\r\n]" + +curl -v $base/contact/ +match "HTTP/.* 302" +match -iP "Location: $redir/contact[\r\n]" + +echo "OK ($n)"