@@ -217,3 +217,85 @@ func TestServerRateLimiting(t *testing.T) {
217217 // Should have some rate limited requests
218218 assert .Greater (t , rateLimitCount , 0 , "Rate limiter should have triggered" )
219219}
220+
221+ func TestServerGzipCompression (t * testing.T ) {
222+ validToken := "hvs.CABAAAAAAQAAAAAAAAAABBBB"
223+
224+ tests := []struct {
225+ name string
226+ path string
227+ acceptEncoding string
228+ setupStorage func () * FakeSecretMsgStorer
229+ expectedStatus int
230+ expectGzip bool
231+ checkVary bool
232+ }{
233+ {
234+ name : "health endpoint with gzip support" ,
235+ path : "/health" ,
236+ acceptEncoding : "gzip" ,
237+ setupStorage : func () * FakeSecretMsgStorer { return & FakeSecretMsgStorer {} },
238+ expectedStatus : http .StatusOK ,
239+ expectGzip : true ,
240+ checkVary : true ,
241+ },
242+ {
243+ name : "health endpoint without gzip support" ,
244+ path : "/health" ,
245+ acceptEncoding : "" ,
246+ setupStorage : func () * FakeSecretMsgStorer { return & FakeSecretMsgStorer {} },
247+ expectedStatus : http .StatusOK ,
248+ expectGzip : false ,
249+ checkVary : false ,
250+ },
251+ {
252+ name : "API JSON response with gzip support" ,
253+ path : "/secret?token=" + validToken ,
254+ acceptEncoding : "gzip" ,
255+ setupStorage : func () * FakeSecretMsgStorer {
256+ return & FakeSecretMsgStorer {
257+ token : validToken ,
258+ msg : "This is a secret message that is long enough to benefit from gzip compression" ,
259+ }
260+ },
261+ expectedStatus : http .StatusOK ,
262+ expectGzip : true ,
263+ checkVary : true ,
264+ },
265+ }
266+
267+ for _ , tt := range tests {
268+ t .Run (tt .name , func (t * testing.T ) {
269+ cnf := conf {
270+ HttpBindingAddress : ":8080" ,
271+ VaultPrefix : "cubbyhole/" ,
272+ AllowedOrigins : []string {"*" },
273+ }
274+ storage := tt .setupStorage ()
275+ handlers := NewSecretHandlers (storage )
276+ server := NewServer (cnf , handlers )
277+
278+ req := httptest .NewRequest (http .MethodGet , tt .path , nil )
279+ if tt .acceptEncoding != "" {
280+ req .Header .Set ("Accept-Encoding" , tt .acceptEncoding )
281+ }
282+ rec := httptest .NewRecorder ()
283+ server .handler ().ServeHTTP (rec , req )
284+
285+ assert .Equal (t , tt .expectedStatus , rec .Code )
286+
287+ if tt .expectGzip {
288+ assert .Equal (t , "gzip" , rec .Header ().Get ("Content-Encoding" ), "Response should be gzip compressed" )
289+ } else {
290+ assert .Empty (t , rec .Header ().Get ("Content-Encoding" ), "Response should not be compressed" )
291+ }
292+
293+ if tt .checkVary {
294+ varyHeader := rec .Header ().Get ("Vary" )
295+ assert .NotEmpty (t , varyHeader , "Should have Vary header" )
296+ // Vary header may contain "Origin" from CORS middleware, just verify it exists
297+ assert .Contains (t , "Origin,Accept-Encoding" , varyHeader , "Vary header should be set by middleware" )
298+ }
299+ })
300+ }
301+ }
0 commit comments