test-caddyfile-cache.sh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/sh
  2. set -eu
  3. repo_root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
  4. caddyfile="$repo_root/deploy/Caddyfile"
  5. active_config=$(sed 's/[[:space:]]*#.*$//' "$caddyfile")
  6. normalized_config=$(printf '%s\n' "$active_config" | awk '
  7. NF > 0 {
  8. for (field = 1; field <= NF; field++) {
  9. token = $field
  10. sub(/^["`]/, "", token)
  11. sub(/["`]$/, "", token)
  12. printf "%s%s", (field == 1 ? "" : " "), token
  13. }
  14. print ""
  15. }
  16. ')
  17. # Keep one canonical encode block instead of reimplementing Caddy matcher semantics.
  18. expected_encode_block=$(cat <<'EOF'
  19. encode {
  20. zstd
  21. gzip 6
  22. minimum_length 256
  23. match {
  24. header Content-Type text/css*
  25. header Content-Type text/csv*
  26. header Content-Type text/html*
  27. header Content-Type text/javascript*
  28. header Content-Type text/markdown*
  29. header Content-Type text/plain*
  30. header Content-Type text/xml*
  31. header Content-Type application/json*
  32. header Content-Type application/javascript*
  33. header Content-Type application/xml*
  34. header Content-Type application/rss+xml*
  35. header Content-Type image/svg+xml*
  36. }
  37. }
  38. EOF
  39. )
  40. if printf '%s\n' "$normalized_config" | grep -Eiq 'cache-control.*immutable'; then
  41. echo "Caddyfile must not force immutable caching; the backend owns asset cache policy" >&2
  42. exit 1
  43. fi
  44. if ! printf '%s\n' "$normalized_config" | grep -Eq '^reverse_proxy localhost:8080([[:space:]]|$)'; then
  45. echo "Caddyfile must continue proxying all application routes to localhost:8080" >&2
  46. exit 1
  47. fi
  48. if printf '%s\n' "$normalized_config" | grep -Eq '^import([[:space:]]|$)'; then
  49. echo "Caddyfile must not import configuration outside this canonical policy check" >&2
  50. exit 1
  51. fi
  52. if printf '%s\n' "$normalized_config" | grep -Eiq '^flush_interval([[:space:]]|$)'; then
  53. echo "Caddyfile must leave flush_interval unset so SSE auto-flushing and client cancellation remain intact" >&2
  54. exit 1
  55. fi
  56. encode_directive_count=$(printf '%s\n' "$normalized_config" | awk '$1 == "encode" { count++ } END { print count + 0 }')
  57. if [ "$encode_directive_count" -ne 1 ]; then
  58. echo "Caddyfile must contain exactly one explicit encode block" >&2
  59. exit 1
  60. fi
  61. actual_encode_block=$(printf '%s\n' "$normalized_config" | awk '
  62. $1 == "encode" { in_block = 1 }
  63. in_block {
  64. print
  65. for (field = 1; field <= NF; field++) {
  66. if ($field == "{") depth++
  67. if ($field == "}") depth--
  68. }
  69. if (depth == 0) exit
  70. }
  71. ')
  72. if [ "$actual_encode_block" != "$expected_encode_block" ]; then
  73. echo "Caddyfile encode block must keep the canonical non-SSE compression policy" >&2
  74. exit 1
  75. fi
  76. echo "Caddyfile preserves backend cache policy, SSE streaming, and non-SSE compression"