From d87760adabf7a9df9324be85dd2a7deb642a37f7 Mon Sep 17 00:00:00 2001 From: Mohammed Al Sahaf Date: Wed, 12 Nov 2025 01:33:09 +0300 Subject: [PATCH] add `vars` tests Signed-off-by: Mohammed Al Sahaf --- caddytest/spec/http/vars/spec.hurl | 125 +++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 caddytest/spec/http/vars/spec.hurl diff --git a/caddytest/spec/http/vars/spec.hurl b/caddytest/spec/http/vars/spec.hurl new file mode 100644 index 000000000..effa1d4cb --- /dev/null +++ b/caddytest/spec/http/vars/spec.hurl @@ -0,0 +1,125 @@ +# Configure Caddy with vars directive +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + vars my_var "custom_value" + vars another_var "another_value" + respond "{vars.my_var} {vars.another_var}" +} +``` + +# Variables are accessible in placeholders +GET https://localhost:9443 +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "custom_value another_value" + + +# Configure Caddy with vars using placeholders +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + vars request_path {path} + vars request_method {method} + respond "Path: {vars.request_path}, Method: {vars.request_method}" +} +``` + +# Variables can be set from request placeholders +GET https://localhost:9443/test/path +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "Path: /test/path, Method: GET" + + +# POST method is captured correctly +POST https://localhost:9443/another +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "Path: /another, Method: POST" + + +# Configure Caddy with vars in route +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + route /api/* { + vars api_version "v1" + respond "API {vars.api_version}" + } + respond "Not API" +} +``` + +# Variables are scoped to their route +GET https://localhost:9443/api/users +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "API v1" + + +# Outside the route, variables are not set +GET https://localhost:9443/other +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "Not API" + + +# Configure Caddy with vars overwriting +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + # without `route`, middlewares are sorted an unstable sort + route { + vars my_var "2" + vars my_var "1" + } + respond "{vars.my_var}" +} +``` + +# Later vars directives overwrite earlier ones +GET https://localhost:9443 +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "1"