Use Busted and LuaJIT-cURL for tests

This commit is contained in:
Catalin Constantin Mititiuc 2025-06-10 18:11:22 -07:00
parent 11c56aff4d
commit 9f019dbac5
5 changed files with 58 additions and 11 deletions

View File

@ -1,4 +1,4 @@
FROM openresty/openresty:1.27.1.2-0-bookworm-buildpack FROM openresty/openresty:bookworm-buildpack
WORKDIR /opt/app WORKDIR /opt/app
@ -12,6 +12,7 @@ RUN apt-get update && apt-get install -y \
RUN luarocks install sitegen RUN luarocks install sitegen
RUN luarocks install busted RUN luarocks install busted
RUN luarocks install luajit-curl
RUN luarocks install inotify INOTIFY_INCDIR=/usr/include/x86_64-linux-gnu/ RUN luarocks install inotify INOTIFY_INCDIR=/usr/include/x86_64-linux-gnu/
CMD ["sh", "-c", "openresty -p `pwd` -g 'daemon off;'"] CMD ["sh", "-c", "openresty -p `pwd` -g 'daemon off;'"]

View File

@ -12,3 +12,12 @@ lint:
test: test:
./test.sh ./test.sh
test-server:
docker run --rm -t -v $(PWD):/opt/app -p 8080:8080 \
--add-host=miti.sh=127.0.0.1 \
--add-host=webdevcat.me=127.0.0.1 \
$(image)
busted:
docker run --rm -t -w /opt/app -v $(PWD):/opt/app $(image) busted

View File

@ -82,6 +82,10 @@ example:
$ pygmentize -S default -f html $ pygmentize -S default -f html
### run tests
$ make test
## gotchas ## gotchas
### What is error `cosmo failed: [string "..."]:62: cannot resume dead coroutine`? ### What is error `cosmo failed: [string "..."]:62: cannot resume dead coroutine`?

31
spec/routes_spec.moon Normal file
View File

@ -0,0 +1,31 @@
http = require "luajit-curl-helper.http"
describe "routes", ->
it "loads miti.sh", ->
request = http.init "http://miti.sh:8080"
st = request\perform!
error request\lastError! if not st
status_code = request\statusCode!
status_message = request\statusMessage!
assert.same status_code, 200
assert.same status_message, "OK"
it "redirects webdevcat.me to miti.sh", ->
request = http.init "http://webdevcat.me:8080"
st = request\perform!
error request\lastError! if not st
status_code = request\statusCode!
status_message = request\statusMessage!
header = request\header!
assert.same status_code, 301
assert.same status_message, "Moved Permanently"
assert.same header.Location, "http://miti.sh:8080/"
-- body = request\body!
-- print "body", body

22
test.sh
View File

@ -15,19 +15,21 @@ echo "Test container started"
# docker exec -t $container_id curl http://miti.sh:8080 # docker exec -t $container_id curl http://miti.sh:8080
# docker exec -t $container_id curl -v http://webdevcat.me:8080 # docker exec -t $container_id curl -v http://webdevcat.me:8080
echo "redirects webdevcat.me to miti.sh" docker exec -t $container_id busted
response=$(docker exec -t $container_id curl -so /dev/null \ # echo "redirects webdevcat.me to miti.sh"
-w "%{http_code} %{redirect_url}" http://webdevcat.me:8080)
expected="301 http://miti.sh:8080/" # response=$(docker exec -t $container_id curl -so /dev/null \
# -w "%{http_code} %{redirect_url}" http://webdevcat.me:8080)
if [[ $response != $expected ]]; then # expected="301 http://miti.sh:8080/"
echo "TEST FAILED"
printf "expected: $expected\ngot: $response\n" # if [[ $response != $expected ]]; then
else # echo "TEST FAILED"
echo "TEST PASSED" # printf "expected: $expected\ngot: $response\n"
fi # else
# echo "TEST PASSED"
# fi
echo "Stopping test container" echo "Stopping test container"
docker exec $container_id openresty -p /opt/app -s stop docker exec $container_id openresty -p /opt/app -s stop