From 87178d5d025d1ec9974455e0077288d923f58696 Mon Sep 17 00:00:00 2001 From: Mohammed Al Sahaf Date: Sat, 6 Jun 2026 22:12:33 +0300 Subject: [PATCH] add `handle` tests Signed-off-by: Mohammed Al Sahaf --- caddytest/spec/http/handle/spec.hurl | 204 +++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 caddytest/spec/http/handle/spec.hurl diff --git a/caddytest/spec/http/handle/spec.hurl b/caddytest/spec/http/handle/spec.hurl new file mode 100644 index 000000000..ac0b90e9f --- /dev/null +++ b/caddytest/spec/http/handle/spec.hurl @@ -0,0 +1,204 @@ +# Configure Caddy with handle directive +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + handle /api/* { + respond "API handler" + } + handle /static/* { + respond "Static handler" + } + handle { + respond "Default handler" + } +} +``` + +# handle matches first matching path +GET https://localhost:9443/api/users +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "API handler" + + +# handle matches second path +GET https://localhost:9443/static/file.css +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "Static handler" + + +# handle uses default when nothing matches +GET https://localhost:9443/other +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "Default handler" + + +# Configure Caddy with handle and named matchers +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + @api { + path /api/* + method GET + } + handle @api { + respond "GET API" + } + handle /api/* { + respond "Other API methods" + } + handle { + respond "Not API" + } +} +``` + +# handle evaluates named matchers correctly +GET https://localhost:9443/api/users +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "GET API" + + +# handle falls through to next handler when matcher doesn't match +POST https://localhost:9443/api/users +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "Other API methods" + + +# handle uses final fallback +GET https://localhost:9443/home +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "Not API" + + +# Configure Caddy with handle and mutually exclusive paths +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + handle /foo { + respond "foo" + } + handle /foo/bar { + respond "foo/bar" + } + handle { + respond "neither" + } +} +``` + +# handle stops at first match (doesn't cascade) +GET https://localhost:9443/foo +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "foo" + + +# Configure Caddy with handle containing multiple directives +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + handle /api/* { + header X-Handler "API" + uri strip_prefix /api + respond "Path: {uri}" + } + handle { + respond "Not handled" + } +} +``` + +# handle can contain multiple directives +GET https://localhost:9443/api/test +[Options] +insecure: true +HTTP 200 +[Asserts] +header "X-Handler" == "API" +body == "Path: /test" + + +# Configure Caddy with handle_path +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + handle_path /api/* { + respond "Path after strip: {uri}" + } + handle { + respond "Original: {uri}" + } +} +``` + +# handle_path automatically strips prefix +GET https://localhost:9443/api/v1/users +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "Path after strip: /v1/users" + + +# handle_path doesn't affect non-matching paths +GET https://localhost:9443/other +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "Original: /other"