|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "compress/gzip" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCompressionHandler(t *testing.T) { |
| 16 | + largeBody := strings.Repeat("A", 2000) |
| 17 | + |
| 18 | + upstream := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 19 | + w.Header().Set("Content-Type", "text/plain") |
| 20 | + _, err := w.Write([]byte(largeBody)) |
| 21 | + require.NoError(t, err) |
| 22 | + }) |
| 23 | + |
| 24 | + t.Run("compresses responses", func(t *testing.T) { |
| 25 | + handler := NewCompressionHandler(0, false, upstream) |
| 26 | + |
| 27 | + req := httptest.NewRequest("GET", "/", nil) |
| 28 | + req.Header.Set("Accept-Encoding", "gzip") |
| 29 | + rr := httptest.NewRecorder() |
| 30 | + |
| 31 | + handler.ServeHTTP(rr, req) |
| 32 | + |
| 33 | + assert.Equal(t, "gzip", rr.Header().Get("Content-Encoding")) |
| 34 | + |
| 35 | + reader, err := gzip.NewReader(rr.Body) |
| 36 | + require.NoError(t, err) |
| 37 | + defer reader.Close() |
| 38 | + body, err := io.ReadAll(reader) |
| 39 | + require.NoError(t, err) |
| 40 | + assert.Equal(t, largeBody, string(body)) |
| 41 | + }) |
| 42 | + |
| 43 | + t.Run("applies jitter when configured", func(t *testing.T) { |
| 44 | + handler := NewCompressionHandler(32, false, upstream) |
| 45 | + |
| 46 | + req := httptest.NewRequest("GET", "/", nil) |
| 47 | + req.Header.Set("Accept-Encoding", "gzip") |
| 48 | + rr := httptest.NewRecorder() |
| 49 | + |
| 50 | + handler.ServeHTTP(rr, req) |
| 51 | + |
| 52 | + require.Equal(t, "gzip", rr.Header().Get("Content-Encoding")) |
| 53 | + |
| 54 | + // Check for GZIP header with FCOMMENT flag (0x10) |
| 55 | + bodyBytes := rr.Body.Bytes() |
| 56 | + require.Greater(t, len(bodyBytes), 10) |
| 57 | + hasComment := (bodyBytes[3] & 0x10) != 0 |
| 58 | + assert.True(t, hasComment, "Expected FCOMMENT flag due to jitter") |
| 59 | + }) |
| 60 | + |
| 61 | + t.Run("wraps with guard when disableOnAuth is true", func(t *testing.T) { |
| 62 | + handler := NewCompressionHandler(0, true, upstream) |
| 63 | + |
| 64 | + req := httptest.NewRequest("GET", "/", nil) |
| 65 | + req.Header.Set("Accept-Encoding", "gzip") |
| 66 | + req.Header.Set("Cookie", "session=secret") |
| 67 | + rr := httptest.NewRecorder() |
| 68 | + |
| 69 | + handler.ServeHTTP(rr, req) |
| 70 | + |
| 71 | + // Should NOT be compressed due to Cookie header |
| 72 | + assert.Empty(t, rr.Header().Get("Content-Encoding")) |
| 73 | + assert.Equal(t, largeBody, rr.Body.String()) |
| 74 | + }) |
| 75 | + |
| 76 | + t.Run("compresses authenticated requests when disableOnAuth is false", func(t *testing.T) { |
| 77 | + handler := NewCompressionHandler(0, false, upstream) |
| 78 | + |
| 79 | + req := httptest.NewRequest("GET", "/", nil) |
| 80 | + req.Header.Set("Accept-Encoding", "gzip") |
| 81 | + req.Header.Set("Cookie", "session=secret") |
| 82 | + rr := httptest.NewRecorder() |
| 83 | + |
| 84 | + handler.ServeHTTP(rr, req) |
| 85 | + |
| 86 | + assert.Equal(t, "gzip", rr.Header().Get("Content-Encoding")) |
| 87 | + }) |
| 88 | +} |
0 commit comments