From 07806715c05aedb4c76ffcaecd80b7afcf933967 Mon Sep 17 00:00:00 2001 From: Mohammed Al Sahaf Date: Sat, 6 Jun 2026 22:19:39 +0300 Subject: [PATCH] add `encode` tests Signed-off-by: Mohammed Al Sahaf --- caddytest/spec/http/encode/spec.hurl | 193 +++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 caddytest/spec/http/encode/spec.hurl diff --git a/caddytest/spec/http/encode/spec.hurl b/caddytest/spec/http/encode/spec.hurl new file mode 100644 index 000000000..8a733d2f8 --- /dev/null +++ b/caddytest/spec/http/encode/spec.hurl @@ -0,0 +1,193 @@ +# Configure Caddy with encode directive +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + encode gzip { + minimum_length 1 + } + respond "This is a test response that should not be compressed with gzip encoding." +} +``` + +# encode gzip compresses responses when client accepts gzip +GET https://localhost:9443 +Accept-Encoding: gzip +[Options] +insecure: true +HTTP 200 +[Asserts] +header "Content-Encoding" == "gzip" +header "Vary" contains "Accept-Encoding" + + +# encode does not compress when client doesn't accept encoding +GET https://localhost:9443 +[Options] +insecure: true +HTTP 200 +[Asserts] +header "Content-Encoding" not exists + + +# Configure Caddy with multiple encoding formats +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + encode zstd gzip { + minimum_length 1 + } + respond "This is a test response that should be compressed." +} +``` + +# encode prefers zstd when client accepts it +GET https://localhost:9443 +Accept-Encoding: gzip, zstd +[Options] +insecure: true +HTTP 200 +[Asserts] +header "Content-Encoding" == "zstd" + + +# encode falls back to gzip when zstd not accepted +GET https://localhost:9443 +Accept-Encoding: gzip +[Options] +insecure: true +HTTP 200 +[Asserts] +header "Content-Encoding" == "gzip" + + +# Configure Caddy with encode matching +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + encode gzip { + match { + header Content-Type text/* + } + minimum_length 1 + } + header Content-Type text/plain + respond {path} { + close + } +} +``` + +# encode only compresses matching content types +GET https://localhost:9443/test.txt +Accept-Encoding: gzip +[Options] +insecure: true +HTTP 200 +[Asserts] +header "Content-Encoding" == "gzip" + + +# Configure Caddy with encode minimum length +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + encode gzip { + minimum_length 100 + } + respond "short" +} +``` + +# encode respects minimum length (should not compress) +GET https://localhost:9443 +Accept-Encoding: gzip +[Options] +insecure: true +HTTP 200 +[Asserts] +header "Content-Encoding" not exists +body == "short" + + +# Configure Caddy with large response +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + encode gzip { + minimum_length 10 + } + respond "This is a longer response that should definitely be compressed because it exceeds the minimum length." +} +``` + +# encode compresses when exceeding minimum length +GET https://localhost:9443 +Accept-Encoding: gzip +[Options] +insecure: true +HTTP 200 +[Asserts] +header "Content-Encoding" == "gzip" + + +# Configure Caddy with brotli encoding +POST http://localhost:2019/load +Content-Type: text/caddyfile +``` +{ + skip_install_trust + http_port 9080 + https_port 9443 + local_certs +} +localhost { + encode zstd gzip { + minimum_length 1 + } + respond "This response can be compressed with zstd or gzip." +} +``` + +# encode picks the first listed encoder when the client accepts it +GET https://localhost:9443 +Accept-Encoding: gzip, zstd +[Options] +insecure: true +HTTP 200 +[Asserts] +header "Content-Encoding" == "zstd"