MCPcopy Create free account
hub / github.com/imgproxy/imgproxy / TestHandlerCacheControl

Method TestHandlerCacheControl

handlers/stream/handler_test.go:257–416  ·  view source on GitHub ↗

TestHandlerCacheControl checks that Cache-Control header is set correctly in different cases

()

Source from the content-addressed store, hash-verified

255
256// TestHandlerCacheControl checks that Cache-Control header is set correctly in different cases
257func (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,

Callers

nothing calls this directly

Calls 12

maxAgeValueMethod · 0.95
executeMethod · 0.95
NewFunction · 0.92
SetHeadersMethod · 0.80
AddMethod · 0.80
RunMethod · 0.80
SetBodyMethod · 0.80
GetMethod · 0.65
FormatMethod · 0.65
CloseMethod · 0.65
ReadMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected