|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/klauspost/compress/gzhttp" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestCompressionGuardMiddleware(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + requestHeaders map[string]string |
| 16 | + responseHeader map[string]string |
| 17 | + wantNoCompress bool |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "No auth headers", |
| 21 | + requestHeaders: map[string]string{}, |
| 22 | + wantNoCompress: false, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "Cookie header present", |
| 26 | + requestHeaders: map[string]string{"Cookie": "session=123"}, |
| 27 | + wantNoCompress: true, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "Authorization header present", |
| 31 | + requestHeaders: map[string]string{"Authorization": "Bearer token"}, |
| 32 | + wantNoCompress: true, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "X-Csrf-Token header present", |
| 36 | + requestHeaders: map[string]string{"X-Csrf-Token": "token"}, |
| 37 | + wantNoCompress: true, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "Set-Cookie response header", |
| 41 | + responseHeader: map[string]string{"Set-Cookie": "session=123"}, |
| 42 | + wantNoCompress: true, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "Cache-Control private response header", |
| 46 | + responseHeader: map[string]string{"Cache-Control": "private, max-age=3600"}, |
| 47 | + wantNoCompress: true, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "Cache-Control no-store response header", |
| 51 | + responseHeader: map[string]string{"Cache-Control": "no-store"}, |
| 52 | + wantNoCompress: true, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "Cache-Control private directive with value", |
| 56 | + responseHeader: map[string]string{"Cache-Control": `public, private="Set-Cookie"`}, |
| 57 | + wantNoCompress: true, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "Cache-Control token parsing avoids false positives", |
| 61 | + responseHeader: map[string]string{"Cache-Control": "public, my-private-setting=value"}, |
| 62 | + wantNoCompress: false, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "Vary Cookie response header", |
| 66 | + responseHeader: map[string]string{"Vary": "Cookie"}, |
| 67 | + wantNoCompress: true, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "Vary token parsing avoids false positives", |
| 71 | + responseHeader: map[string]string{"Vary": "Accept-Encoding, Cookie-Name"}, |
| 72 | + wantNoCompress: false, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "Case-insensitive header checks (response)", |
| 76 | + responseHeader: map[string]string{"cache-control": "PRIVATE"}, |
| 77 | + wantNoCompress: true, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "Case-insensitive header checks (request)", |
| 81 | + requestHeaders: map[string]string{"cookie": "session=123"}, |
| 82 | + wantNoCompress: true, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + for _, tt := range tests { |
| 87 | + t.Run(tt.name, func(t *testing.T) { |
| 88 | + handler := NewCompressionGuardMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 89 | + for k, v := range tt.responseHeader { |
| 90 | + w.Header().Set(k, v) |
| 91 | + } |
| 92 | + w.WriteHeader(http.StatusOK) |
| 93 | + })) |
| 94 | + |
| 95 | + req := httptest.NewRequest("GET", "/", nil) |
| 96 | + for k, v := range tt.requestHeaders { |
| 97 | + req.Header.Set(k, v) |
| 98 | + } |
| 99 | + |
| 100 | + rr := httptest.NewRecorder() |
| 101 | + handler.ServeHTTP(rr, req) |
| 102 | + |
| 103 | + if tt.wantNoCompress { |
| 104 | + assert.Equal(t, "1", rr.Header().Get(gzhttp.HeaderNoCompression)) |
| 105 | + } else { |
| 106 | + assert.Empty(t, rr.Header().Get(gzhttp.HeaderNoCompression)) |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
0 commit comments