responseheaders_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package responseheaders
  2. import (
  3. "net/http"
  4. "testing"
  5. "github.com/Wei-Shaw/sub2api/internal/config"
  6. )
  7. func TestFilterHeadersDisabledUsesDefaultAllowlist(t *testing.T) {
  8. src := http.Header{}
  9. src.Add("Content-Type", "application/json")
  10. src.Add("X-Request-Id", "req-123")
  11. src.Add("X-Test", "ok")
  12. src.Add("Connection", "keep-alive")
  13. src.Add("Content-Length", "123")
  14. cfg := config.ResponseHeaderConfig{
  15. Enabled: false,
  16. ForceRemove: []string{"x-request-id"},
  17. }
  18. filtered := FilterHeaders(src, CompileHeaderFilter(cfg))
  19. if filtered.Get("Content-Type") != "application/json" {
  20. t.Fatalf("expected Content-Type passthrough, got %q", filtered.Get("Content-Type"))
  21. }
  22. if filtered.Get("X-Request-Id") != "req-123" {
  23. t.Fatalf("expected X-Request-Id allowed, got %q", filtered.Get("X-Request-Id"))
  24. }
  25. if filtered.Get("X-Test") != "" {
  26. t.Fatalf("expected X-Test removed, got %q", filtered.Get("X-Test"))
  27. }
  28. if filtered.Get("Connection") != "" {
  29. t.Fatalf("expected Connection to be removed, got %q", filtered.Get("Connection"))
  30. }
  31. if filtered.Get("Content-Length") != "" {
  32. t.Fatalf("expected Content-Length to be removed, got %q", filtered.Get("Content-Length"))
  33. }
  34. }
  35. func TestFilterHeadersAllowsReasoningIncludedByDefault(t *testing.T) {
  36. src := http.Header{}
  37. src.Set("X-Reasoning-Included", "1")
  38. filtered := FilterHeaders(src, CompileHeaderFilter(config.ResponseHeaderConfig{}))
  39. if got := filtered.Get("X-Reasoning-Included"); got != "1" {
  40. t.Fatalf("expected X-Reasoning-Included passthrough, got %q", got)
  41. }
  42. }
  43. func TestFilterHeadersForceRemoveOverridesReasoningIncluded(t *testing.T) {
  44. src := http.Header{}
  45. src.Set("X-Reasoning-Included", "1")
  46. filtered := FilterHeaders(src, CompileHeaderFilter(config.ResponseHeaderConfig{
  47. Enabled: true,
  48. ForceRemove: []string{"x-reasoning-included"},
  49. }))
  50. if got := filtered.Get("X-Reasoning-Included"); got != "" {
  51. t.Fatalf("expected X-Reasoning-Included removal, got %q", got)
  52. }
  53. }
  54. func TestFilterHeadersEnabledUsesAllowlist(t *testing.T) {
  55. src := http.Header{}
  56. src.Add("Content-Type", "application/json")
  57. src.Add("X-Extra", "ok")
  58. src.Add("X-Remove", "nope")
  59. src.Add("X-Blocked", "nope")
  60. cfg := config.ResponseHeaderConfig{
  61. Enabled: true,
  62. AdditionalAllowed: []string{"x-extra"},
  63. ForceRemove: []string{"x-remove"},
  64. }
  65. filtered := FilterHeaders(src, CompileHeaderFilter(cfg))
  66. if filtered.Get("Content-Type") != "application/json" {
  67. t.Fatalf("expected Content-Type allowed, got %q", filtered.Get("Content-Type"))
  68. }
  69. if filtered.Get("X-Extra") != "ok" {
  70. t.Fatalf("expected X-Extra allowed, got %q", filtered.Get("X-Extra"))
  71. }
  72. if filtered.Get("X-Remove") != "" {
  73. t.Fatalf("expected X-Remove removed, got %q", filtered.Get("X-Remove"))
  74. }
  75. if filtered.Get("X-Blocked") != "" {
  76. t.Fatalf("expected X-Blocked removed, got %q", filtered.Get("X-Blocked"))
  77. }
  78. }