MCPcopy
hub / github.com/gofiber/fiber / Test_CacheClampsInvalidStoredDate

Function Test_CacheClampsInvalidStoredDate

middleware/cache/cache_test.go:2674–2723  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2672}
2673
2674func Test_CacheClampsInvalidStoredDate(t *testing.T) {
2675 t.Parallel()
2676
2677 storage := newMutatingStorage(func(key string, val []byte) []byte {
2678 if strings.HasSuffix(key, "_body") {
2679 return val
2680 }
2681
2682 var it item
2683 if _, err := it.UnmarshalMsg(val); err != nil {
2684 return val
2685 }
2686
2687 it.date = uint64(math.MaxInt64) + 1024
2688 updated, err := it.MarshalMsg(nil)
2689 if err != nil {
2690 return val
2691 }
2692
2693 return updated
2694 })
2695
2696 app := fiber.New()
2697 app.Use(New(Config{
2698 Expiration: time.Minute,
2699 Storage: storage,
2700 }))
2701
2702 app.Get("/", func(c fiber.Ctx) error {
2703 c.Set(fiber.HeaderCacheControl, "public, max-age=60")
2704 return c.SendString("ok")
2705 })
2706
2707 resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", http.NoBody))
2708 require.NoError(t, err)
2709 require.Equal(t, cacheMiss, resp.Header.Get("X-Cache"))
2710
2711 resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/", http.NoBody))
2712 require.NoError(t, err)
2713 require.Equal(t, cacheHit, resp.Header.Get("X-Cache"))
2714
2715 parsedDate, err := http.ParseTime(resp.Header.Get(fiber.HeaderDate))
2716 require.NoError(t, err)
2717 require.WithinDuration(t, time.Now(), parsedDate, time.Minute)
2718
2719 ageVal, err := strconv.Atoi(resp.Header.Get(fiber.HeaderAge))
2720 require.NoError(t, err)
2721 require.Less(t, ageVal, 60)
2722 require.GreaterOrEqual(t, ageVal, 0)
2723}
2724
2725func Test_CacheClampsFutureStoredDate(t *testing.T) {
2726 t.Parallel()

Callers

nothing calls this directly

Calls 12

UnmarshalMsgMethod · 0.95
MarshalMsgMethod · 0.95
newMutatingStorageFunction · 0.85
TestMethod · 0.80
NewFunction · 0.70
NewMethod · 0.65
UseMethod · 0.65
GetMethod · 0.65
SetMethod · 0.65
SendStringMethod · 0.65
NowMethod · 0.45
LessMethod · 0.45

Tested by

no test coverage detected