diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b7ff565 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM openresty/openresty:bookworm-buildpack + +WORKDIR /opt/app + +RUN luarocks install moonscript +RUN luarocks install busted +RUN luarocks install luajit-curl +RUN luarocks install luasocket # needed for testing nginx reverse proxy + +RUN openssl req -x509 -newkey rsa:4096 -nodes \ + -keyout /etc/ssl/private/domain.abc.pem \ + -out /etc/ssl/certs/domain.abc.pem \ + -sha256 -days 365 -subj '/CN=domain.abc' \ + -addext "subjectAltName=DNS:domain.abc" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9e6395b --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +image = test-nginx +loopback = 127.0.0.1 + +image-build: + docker build -t $(image) . + +image-rm: + docker image rm $(image) + +test: + @ct=$(shell docker run --rm -d \ + -v $(PWD)/conf/conf.d:/etc/nginx/conf.d \ + -v $(PWD)/html:/var/www \ + -v $(PWD):/opt/app \ + --network no-internet \ + --add-host=domain.abc=$(loopback) \ + $(image)); \ + docker exec -t $$ct busted; \ + docker exec $$ct openresty -s stop diff --git a/conf/conf.d/default.conf b/conf/conf.d/default.conf index 10ff1ba..b5f5a68 100644 --- a/conf/conf.d/default.conf +++ b/conf/conf.d/default.conf @@ -11,10 +11,17 @@ # See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files # +server { + listen 80; + return 301 https://$host$request_uri; +} server { - listen 80; - server_name localhost; + listen 443 ssl; + server_name domain.abc; + + ssl_certificate /etc/ssl/certs/domain.abc.pem; + ssl_certificate_key /etc/ssl/private/domain.abc.pem; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; diff --git a/spec/nginx_spec.moon b/spec/nginx_spec.moon new file mode 100644 index 0000000..2334cc6 --- /dev/null +++ b/spec/nginx_spec.moon @@ -0,0 +1,26 @@ +http = require "luajit-curl-helper.http" + +req = (url) -> + request = http.init url + st = request\perform! + error request\lastError! if not st + request + +describe "test environment", -> + it "can't connect to the internet", -> + assert.has_error (-> req "http://example.org"), + "Couldn't resolve host name" + +describe "https://domain.abc", -> + it "sends /index.html", -> + request = req "https://domain.abc" + assert.same request\statusCode!, 200 + assert.same request\statusMessage!, "OK" + assert.same request\body!\match("%s+(.-)%s+"), "hello world!" + +describe "http://domain.abc", -> + it "redirects to https", -> + request = req "http://domain.abc" + assert.same request\statusCode!, 301 + assert.same request\statusMessage!, "Moved Permanently" + assert.same request\header!.Location, "https://domain.abc/"