|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestResponseHeaderHandler(t *testing.T) { |
| 11 | + t.Run("sets response headers from query parameters", func(t *testing.T) { |
| 12 | + req := httptest.NewRequest(http.MethodGet, "/response-header?X-Custom-Header=custom-value&X-Request-Id=12345", nil) |
| 13 | + rec := httptest.NewRecorder() |
| 14 | + |
| 15 | + ResponseHeaderHandler(rec, req) |
| 16 | + |
| 17 | + if rec.Code != http.StatusOK { |
| 18 | + t.Errorf("expected status 200, got %d", rec.Code) |
| 19 | + } |
| 20 | + |
| 21 | + contentType := rec.Header().Get("Content-Type") |
| 22 | + if contentType != "application/json" { |
| 23 | + t.Errorf("expected Content-Type application/json, got %s", contentType) |
| 24 | + } |
| 25 | + |
| 26 | + // Check response headers |
| 27 | + if rec.Header().Get("X-Custom-Header") != "custom-value" { |
| 28 | + t.Errorf("expected X-Custom-Header=custom-value, got %s", rec.Header().Get("X-Custom-Header")) |
| 29 | + } |
| 30 | + |
| 31 | + if rec.Header().Get("X-Request-Id") != "12345" { |
| 32 | + t.Errorf("expected X-Request-Id=12345, got %s", rec.Header().Get("X-Request-Id")) |
| 33 | + } |
| 34 | + |
| 35 | + // Check response body |
| 36 | + var resp ResponseHeaderResponse |
| 37 | + if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil { |
| 38 | + t.Fatalf("failed to decode response: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + if resp.Headers["X-Custom-Header"] != "custom-value" { |
| 42 | + t.Errorf("expected body X-Custom-Header=custom-value, got %s", resp.Headers["X-Custom-Header"]) |
| 43 | + } |
| 44 | + |
| 45 | + if resp.Headers["X-Request-Id"] != "12345" { |
| 46 | + t.Errorf("expected body X-Request-Id=12345, got %s", resp.Headers["X-Request-Id"]) |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("handles standard HTTP headers", func(t *testing.T) { |
| 51 | + req := httptest.NewRequest(http.MethodGet, "/response-header?Cache-Control=no-cache&Content-Language=en-US", nil) |
| 52 | + rec := httptest.NewRecorder() |
| 53 | + |
| 54 | + ResponseHeaderHandler(rec, req) |
| 55 | + |
| 56 | + if rec.Code != http.StatusOK { |
| 57 | + t.Errorf("expected status 200, got %d", rec.Code) |
| 58 | + } |
| 59 | + |
| 60 | + if rec.Header().Get("Cache-Control") != "no-cache" { |
| 61 | + t.Errorf("expected Cache-Control=no-cache, got %s", rec.Header().Get("Cache-Control")) |
| 62 | + } |
| 63 | + |
| 64 | + if rec.Header().Get("Content-Language") != "en-US" { |
| 65 | + t.Errorf("expected Content-Language=en-US, got %s", rec.Header().Get("Content-Language")) |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("handles empty query parameters", func(t *testing.T) { |
| 70 | + req := httptest.NewRequest(http.MethodGet, "/response-header", nil) |
| 71 | + rec := httptest.NewRecorder() |
| 72 | + |
| 73 | + ResponseHeaderHandler(rec, req) |
| 74 | + |
| 75 | + if rec.Code != http.StatusOK { |
| 76 | + t.Errorf("expected status 200, got %d", rec.Code) |
| 77 | + } |
| 78 | + |
| 79 | + var resp ResponseHeaderResponse |
| 80 | + if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil { |
| 81 | + t.Fatalf("failed to decode response: %v", err) |
| 82 | + } |
| 83 | + |
| 84 | + if len(resp.Headers) != 0 { |
| 85 | + t.Errorf("expected empty headers map, got %d headers", len(resp.Headers)) |
| 86 | + } |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("uses first value when multiple values exist", func(t *testing.T) { |
| 90 | + req := httptest.NewRequest(http.MethodGet, "/response-header?X-Multi=first&X-Multi=second", nil) |
| 91 | + rec := httptest.NewRecorder() |
| 92 | + |
| 93 | + ResponseHeaderHandler(rec, req) |
| 94 | + |
| 95 | + if rec.Code != http.StatusOK { |
| 96 | + t.Errorf("expected status 200, got %d", rec.Code) |
| 97 | + } |
| 98 | + |
| 99 | + headerValue := rec.Header().Get("X-Multi") |
| 100 | + if headerValue != "first" { |
| 101 | + t.Errorf("expected X-Multi=first, got %s", headerValue) |
| 102 | + } |
| 103 | + }) |
| 104 | +} |
0 commit comments