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

Function Test_CacheClampsFutureStoredDate

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

Source from the content-addressed store, hash-verified

2723}
2724
2725func Test_CacheClampsFutureStoredDate(t *testing.T) {
2726 t.Parallel()
2727
2728 storage := newMutatingStorage(func(key string, val []byte) []byte {
2729 if strings.HasSuffix(key, "_body") {
2730 return val
2731 }
2732
2733 var it item
2734 if _, err := it.UnmarshalMsg(val); err != nil {
2735 return val
2736 }
2737
2738 future := time.Now().Add(2 * time.Second).UTC()
2739 sec := max(future.Unix(), 0)
2740
2741 it.date = uint64(sec)
2742 updated, err := it.MarshalMsg(nil)
2743 if err != nil {
2744 return val
2745 }
2746
2747 return updated
2748 })
2749
2750 app := fiber.New()
2751 app.Use(New(Config{
2752 Expiration: time.Minute,
2753 Storage: storage,
2754 }))
2755
2756 app.Get("/", func(c fiber.Ctx) error {
2757 c.Set(fiber.HeaderCacheControl, "public, max-age=60")
2758 return c.SendString("ok")
2759 })
2760
2761 resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", http.NoBody))
2762 require.NoError(t, err)
2763 require.Equal(t, cacheMiss, resp.Header.Get("X-Cache"))
2764
2765 resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/", http.NoBody))
2766 require.NoError(t, err)
2767 require.Equal(t, cacheHit, resp.Header.Get("X-Cache"))
2768
2769 parsedDate, err := http.ParseTime(resp.Header.Get(fiber.HeaderDate))
2770 require.NoError(t, err)
2771 require.False(t, parsedDate.After(time.Now()))
2772
2773 ageVal, err := strconv.Atoi(resp.Header.Get(fiber.HeaderAge))
2774 require.NoError(t, err)
2775 require.GreaterOrEqual(t, ageVal, 0)
2776}
2777
2778func Test_RequestPragmaNoCacheTriggersMiss(t *testing.T) {
2779 t.Parallel()

Callers

nothing calls this directly

Calls 12

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

Tested by

no test coverage detected