Add test for nginx reverse proxy rule

This commit is contained in:
Catalin Constantin Mititiuc 2025-06-12 15:27:23 -07:00
parent 1e33d2770a
commit 74632be2e1
6 changed files with 38 additions and 34 deletions

View File

@ -11,8 +11,13 @@ RUN apt-get update && apt-get install -y \
wget -q -O $pkgname $pkgurl && dpkg -i $pkgname && rm $pkgname
RUN luarocks install sitegen
RUN luarocks install busted
RUN luarocks install luajit-curl
# needed for sitegen watcher
RUN luarocks install inotify INOTIFY_INCDIR=/usr/include/x86_64-linux-gnu/
# needed for testing
RUN luarocks install busted
RUN luarocks install luajit-curl
RUN luarocks install luasocket # needed for testing nginx reverse proxy
CMD ["sh", "-c", "openresty -p `pwd` -g 'daemon off;'"]

View File

@ -21,12 +21,12 @@ ROOT = /var/lib/gitea/data/gitea-repositories
[server]
; SSH_DOMAIN = localhost
SSH_DOMAIN = git.miti.sh
; SSH_DOMAIN = git.miti.sh
; DOMAIN = localhost
DOMAIN = git.miti.sh
HTTP_PORT = 3000
; DOMAIN = git.miti.sh
; HTTP_PORT = 3000
; ROOT_URL = http://localhost:3000/
ROOT_URL = https://git.miti.sh
ROOT_URL = https://git.miti.sh:3000
APP_DATA_PATH = /var/lib/gitea/data
DISABLE_SSH = false
SSH_PORT = 22

View File

@ -37,7 +37,7 @@ http {
location / {
client_max_body_size 1024M;
proxy_pass http://localhost:3000;
proxy_pass http://unix:/run/gitea/gitea.socket;
proxy_set_header Connection $http_connection;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;

View File

@ -104,12 +104,14 @@ describe "http://miti.sh/posts", ->
assert.same request\body!\match("<title>(.*)</title>"), "miti.sh · Posts"
describe "http://git.miti.sh", ->
it "doesn't find it?", ->
it "reverse proxies request to unix socket", ->
os.execute "mkdir /run/gitea"
p = io.popen "luajit unixstreamsrvr.lua"
os.execute("sleep 1")
os.execute("chown -R nobody /run/gitea")
request = req "http://git.miti.sh"
require("moon").p request\body!
require("moon").p request\header!
-- assert.same request\statusCode!, 404
-- assert.same request\statusMessage!, "Not Found"
assert.is_not_nil p\read"*a"\match "Host: git.miti.sh"
describe "http://apps.miti.sh", ->
it "doesn't find it", ->

29
test.sh
View File

@ -1,20 +1,19 @@
#!/usr/bin/env bash
# echo "hello"
# curl http://webdevcat.me:8080
image=miti.sh
loopback=127.0.0.1
echo "Starting test container..."
# Make sure to create 'no-internet' network, if it doesn't already exist:
# $ docker network create --internal no-internet
container_id=$(docker run --rm -d -v $(pwd):/opt/app --network no-internet \
--add-host=miti.sh=127.0.0.1 \
--add-host=apps.miti.sh=127.0.0.1 \
--add-host=git.miti.sh=127.0.0.1 \
--add-host=webdevcat.me=127.0.0.1 \
--add-host=apps.webdevcat.me=127.0.0.1 \
--add-host=git.webdevcat.me=127.0.0.1 \
--add-host=miti.sh=$loopback \
--add-host=git.miti.sh=$loopback \
--add-host=apps.miti.sh=$loopback \
--add-host=webdevcat.me=$loopback \
--add-host=git.webdevcat.me=$loopback \
--add-host=apps.webdevcat.me=$loopback \
$image)
echo "Test container started"
@ -23,20 +22,6 @@ echo "Test container started"
# docker exec -t $container_id cat /opt/app/logs/error.log
docker exec -t $container_id busted
# echo "redirects webdevcat.me to miti.sh"
# response=$(docker exec -t $container_id curl -so /dev/null \
# -w "%{http_code} %{redirect_url}" http://webdevcat.me:8080)
# expected="301 http://miti.sh:8080/"
# if [[ $response != $expected ]]; then
# echo "TEST FAILED"
# printf "expected: $expected\ngot: $response\n"
# else
# echo "TEST PASSED"
# fi
echo "Stopping test container"
docker exec $container_id openresty -p /opt/app -s stop

12
unixstreamsrvr.lua Normal file
View File

@ -0,0 +1,12 @@
socket = require"socket"
socket.unix = require"socket.unix"
u = assert(socket.unix.stream())
assert(u:bind("/run/gitea/gitea.socket"))
assert(u:listen())
c = assert(u:accept())
m = assert(c:receive())
repeat
print(m)
m = assert(c:receive())
until m == ""