TestHandlerCacheControl checks that Cache-Control header is set correctly in different cases
()
| 255 | |
| 256 | // TestHandlerCacheControl checks that Cache-Control header is set correctly in different cases |
| 257 | func (s *HandlerTestSuite) TestHandlerCacheControl() { |
| 258 | type testCase struct { |
| 259 | name string |
| 260 | cacheControlPassthrough bool |
| 261 | setupOriginHeaders func() |
| 262 | timestampOffset *time.Duration // nil for no timestamp, otherwise the offset from now |
| 263 | expectedStatusCode int |
| 264 | validate func(*http.Response) |
| 265 | } |
| 266 | |
| 267 | // Duration variables for test cases |
| 268 | var ( |
| 269 | oneHour = time.Hour |
| 270 | thirtyMinutes = 30 * time.Minute |
| 271 | fortyFiveMinutes = 45 * time.Minute |
| 272 | twoHours = time.Hour * 2 |
| 273 | oneMinuteDelta = float64(time.Minute) |
| 274 | ) |
| 275 | |
| 276 | defaultTTL := 4242 |
| 277 | |
| 278 | testCases := []testCase{ |
| 279 | { |
| 280 | name: "Passthrough", |
| 281 | cacheControlPassthrough: true, |
| 282 | setupOriginHeaders: func() { |
| 283 | s.testServer().SetHeaders(httpheaders.CacheControl, "max-age=3600, public") |
| 284 | }, |
| 285 | timestampOffset: nil, |
| 286 | expectedStatusCode: 200, |
| 287 | validate: func(res *http.Response) { |
| 288 | s.Require().Equal("max-age=3600, public", res.Header.Get(httpheaders.CacheControl)) |
| 289 | }, |
| 290 | }, |
| 291 | // Checks that expires gets convert to cache-control |
| 292 | { |
| 293 | name: "ExpiresPassthrough", |
| 294 | cacheControlPassthrough: true, |
| 295 | setupOriginHeaders: func() { |
| 296 | s.testServer().SetHeaders(httpheaders.Expires, time.Now().Add(oneHour).UTC().Format(http.TimeFormat)) |
| 297 | }, |
| 298 | timestampOffset: nil, |
| 299 | expectedStatusCode: 200, |
| 300 | validate: func(res *http.Response) { |
| 301 | // When expires is converted to cache-control, the expires header should be empty |
| 302 | s.Require().Empty(res.Header.Get(httpheaders.Expires)) |
| 303 | s.Require().InDelta(oneHour, s.maxAgeValue(res), oneMinuteDelta) |
| 304 | }, |
| 305 | }, |
| 306 | // It would be set to something like default ttl |
| 307 | { |
| 308 | name: "PassthroughDisabled", |
| 309 | cacheControlPassthrough: false, |
| 310 | setupOriginHeaders: func() { |
| 311 | s.testServer().SetHeaders(httpheaders.CacheControl, "max-age=3600, public") |
| 312 | }, |
| 313 | timestampOffset: nil, |
| 314 | expectedStatusCode: 200, |
nothing calls this directly
no test coverage detected