Skip to content

Commit

Permalink
Merge pull request #136 from clue-labs/tests
Browse files Browse the repository at this point in the history
Clean up and improve structure of integration tests
  • Loading branch information
SimonFrings authored May 8, 2024
2 parents 62797fd + c7c8d75 commit b1332c0
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 46 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "[email protected]"
- run: git config --global url."https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/".insteadOf "https://github.com/"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
42 changes: 0 additions & 42 deletions tests/acceptance.sh

This file was deleted.

16 changes: 16 additions & 0 deletions tests/await.bash
Original file line number Diff line number Diff line change
@@ -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
102 changes: 102 additions & 0 deletions tests/integration.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/bash

# run with base url argument like "http://clue.localhost" or "https://user:[email protected]"
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 protected] --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 protected] --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 protected] --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 protected] --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)"

0 comments on commit b1332c0

Please sign in to comment.