From 7a9ce919868642a5778e78b3f007ab62b8ea3dc8 Mon Sep 17 00:00:00 2001 From: Mohammed Al Sahaf Date: Sat, 6 Jun 2026 22:26:11 +0300 Subject: [PATCH] add `fs` and `try_files` tests Signed-off-by: Mohammed Al Sahaf --- .../http/file_server/assets/indexed/style.css | 0 caddytest/spec/http/fs/spec.hurl | 95 ++++++++++++++ caddytest/spec/http/try_files/spec.hurl | 117 ++++++++++++++++++ 3 files changed, 212 insertions(+) create mode 100644 caddytest/spec/http/file_server/assets/indexed/style.css create mode 100644 caddytest/spec/http/fs/spec.hurl create mode 100644 caddytest/spec/http/try_files/spec.hurl diff --git a/caddytest/spec/http/file_server/assets/indexed/style.css b/caddytest/spec/http/file_server/assets/indexed/style.css new file mode 100644 index 000000000..e69de29bb diff --git a/caddytest/spec/http/fs/spec.hurl b/caddytest/spec/http/fs/spec.hurl new file mode 100644 index 000000000..83499a5a5 --- /dev/null +++ b/caddytest/spec/http/fs/spec.hurl @@ -0,0 +1,95 @@ +# Spec: fs directive — selects which named filesystem to use for file_server etc. +# Only the "default" (OS) filesystem is available without external plugins. +# These tests verify the directive parses, is applied conditionally, and does +# not interfere with file_server resolving files from the configured root. + +# Configure Caddy with explicit default filesystem +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + root * {{indexed_root}} + fs default + file_server +} +``` +HTTP 200 + +# PROPERTY: `fs default` selects the OS-backed filesystem and serves files normally +GET https://localhost:9443/index.txt +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "index.txt" + + +# PROPERTY: default filesystem honors index files for directory requests +GET https://localhost:9443/ +[Options] +insecure: true +HTTP 200 +[Asserts] +header "Content-Type" contains "text/html" +body contains "Index.html" + + +# Configure Caddy with `fs` applied conditionally via named matcher +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + root * {{indexed_root}} + @indexed path /indexed/* + fs @indexed default + file_server +} +``` +HTTP 200 + +# PROPERTY: matcher-scoped `fs` does not break unrelated paths +GET https://localhost:9443/index.txt +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "index.txt" + + +# Configure Caddy with fs and browse listing +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + root * {{unindexed_root}} + fs default + file_server browse +} +``` +HTTP 200 + +# PROPERTY: fs default works with file_server browse (directory listing fallback) +GET https://localhost:9443/ +[Options] +insecure: true +HTTP 200 +[Asserts] +header "Content-Type" contains "text/html" diff --git a/caddytest/spec/http/try_files/spec.hurl b/caddytest/spec/http/try_files/spec.hurl new file mode 100644 index 000000000..0aacc8bc7 --- /dev/null +++ b/caddytest/spec/http/try_files/spec.hurl @@ -0,0 +1,117 @@ +# Configure Caddy with try_files directive +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + root * {{indexed_root}} + try_files {path} {path}.txt {path}/index.html + file_server +} +``` + +# try_files tries the original path first +GET https://localhost:9443/index.txt +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "index.txt" + + +# try_files falls through to next option if first doesn't exist +GET https://localhost:9443/index +[Options] +insecure: true +HTTP 200 +[Asserts] +body == "index.txt" + + +# try_files falls through to directory index when the directory exists +GET https://localhost:9443/ +[Options] +insecure: true +HTTP 200 +[Asserts] +body contains "Index.html" + + +# Configure Caddy with try_files and final fallback +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + root * {{indexed_root}} + try_files {path} /index.html + file_server +} +``` + +# try_files uses final fallback when nothing matches +GET https://localhost:9443/does-not-exist +[Options] +insecure: true +HTTP 200 +[Asserts] +body contains "Index.html" + + +# Configure Caddy with try_files policy +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + root * {{indexed_root}} + try_files {path} {path}/ =404 +} +``` + +# try_files with =404 returns 404 when no files match +GET https://localhost:9443/nonexistent +[Options] +insecure: true +HTTP 404 + + +# Configure Caddy with try_files and query parameters +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + root * {{indexed_root}} + try_files {path} /index.html + file_server +} +``` + +# try_files preserves query parameters +GET https://localhost:9443/missing?param=value +[Options] +insecure: true +HTTP 200 +[Asserts] +body contains "Index.html"