From f4f901ec133e2cc45eac0c9e087de4ac50d6300e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 7 May 2024 14:55:45 +0200 Subject: [PATCH 1/4] Rename integration tests to `integration.bash` --- Makefile | 2 +- README.md | 2 +- tests/{acceptance.sh => integration.bash} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename tests/{acceptance.sh => integration.bash} (100%) diff --git a/Makefile b/Makefile index a94a05a..b5ac1a1 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 test -z "$$(git status --porcelain)" || (echo Directory is dirty && git status && exit 1) deploy: diff --git a/README.md b/README.md index ec85f99..7766583 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ make test > `http://clue.localhost`. You may test other deployments like this: > > ```bash -> tests/acceptance.sh https://clue.example +> tests/integration.bash https://clue.example > ``` Once done, you can clean up like this: diff --git a/tests/acceptance.sh b/tests/integration.bash similarity index 100% rename from tests/acceptance.sh rename to tests/integration.bash From 40dab9dc84518b6ef3ad18fb35fcea897a03fd1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 7 May 2024 15:01:10 +0200 Subject: [PATCH 2/4] Clean up and improve structure of integration tests --- tests/integration.bash | 111 +++++++++++++++++++++++++++++++---------- 1 file changed, 84 insertions(+), 27 deletions(-) diff --git a/tests/integration.bash b/tests/integration.bash index 0292c67..ba95f55 100755 --- a/tests/integration.bash +++ b/tests/integration.bash @@ -3,40 +3,97 @@ #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 +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 } -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" +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)" From 054f1171e5dbc2aabf6588d17f6e997ea8aa9942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 7 May 2024 15:07:13 +0200 Subject: [PATCH 3/4] Ignore trailing slash for integration tests --- Makefile | 2 +- README.md | 6 +++--- tests/integration.bash | 7 +++++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index b5ac1a1..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/integration.bash + 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 7766583..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/integration.bash https://clue.example +> tests/integration.bash http://clue.localhost/ > ``` Once done, you can clean up like this: diff --git a/tests/integration.bash b/tests/integration.bash index ba95f55..879811f 100755 --- a/tests/integration.bash +++ b/tests/integration.bash @@ -1,7 +1,10 @@ #!/bin/bash -#run with base url argument like "http://clue.localhost" or "https://user:pass@clue.example" -base=${1:-http://clue.localhost} +# 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 From c7c8d75343654742540b616678fa3b945ceba241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 7 May 2024 17:04:08 +0200 Subject: [PATCH 4/4] Improve tests to await port being open --- .github/workflows/ci.yml | 1 + tests/await.bash | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100755 tests/await.bash 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/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