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

Function Test_Cache_VaryAndAuth

middleware/cache/cache_test.go:4618–4686  ·  view source on GitHub ↗

Test_Cache_VaryAndAuth tests vary and auth functionality

(t *testing.T)

Source from the content-addressed store, hash-verified

4616
4617// Test_Cache_VaryAndAuth tests vary and auth functionality
4618func Test_Cache_VaryAndAuth(t *testing.T) {
4619 t.Parallel()
4620
4621 t.Run("storeVaryManifest failure", func(t *testing.T) {
4622 t.Parallel()
4623 storage := newFailingCacheStorage()
4624 storage.errs["set|manifest"] = errors.New("storage fail")
4625 manager := &manager{storage: storage}
4626 err := storeVaryManifest(context.Background(), manager, "manifest", []string{"Accept"}, 3600*time.Second)
4627 require.Error(t, err)
4628 })
4629
4630 t.Run("loadVaryManifest not found", func(t *testing.T) {
4631 t.Parallel()
4632 storage := newFailingCacheStorage()
4633 manager := &manager{storage: storage}
4634 varyNames, found, err := loadVaryManifest(context.Background(), manager, "nonexistent")
4635 require.NoError(t, err)
4636 require.False(t, found)
4637 require.Nil(t, varyNames)
4638 })
4639
4640 t.Run("vary with multiple headers", func(t *testing.T) {
4641 t.Parallel()
4642 app := fiber.New()
4643 app.Use(New(Config{Expiration: 1 * time.Hour}))
4644 app.Get("/test", func(c fiber.Ctx) error {
4645 c.Response().Header.Set("Vary", "Accept, Accept-Encoding")
4646 c.Response().Header.Set("Cache-Control", "max-age=3600")
4647 return c.SendString("test")
4648 })
4649
4650 req := httptest.NewRequest(fiber.MethodGet, "/test", http.NoBody)
4651 req.Header.Set("Accept", "application/json")
4652 req.Header.Set("Accept-Encoding", "gzip")
4653 rsp, err := app.Test(req)
4654 require.NoError(t, err)
4655 require.Equal(t, cacheMiss, rsp.Header.Get("X-Cache"))
4656
4657 req2 := httptest.NewRequest(fiber.MethodGet, "/test", http.NoBody)
4658 req2.Header.Set("Accept", "application/json")
4659 req2.Header.Set("Accept-Encoding", "gzip")
4660 rsp2, err := app.Test(req2)
4661 require.NoError(t, err)
4662 require.Equal(t, cacheHit, rsp2.Header.Get("X-Cache"))
4663 })
4664
4665 t.Run("auth with must-revalidate", func(t *testing.T) {
4666 t.Parallel()
4667 app := fiber.New()
4668 app.Use(New(Config{Expiration: 1 * time.Hour}))
4669 app.Get("/test", func(c fiber.Ctx) error {
4670 c.Response().Header.Set("Cache-Control", "must-revalidate, max-age=3600")
4671 return c.SendString("content")
4672 })
4673
4674 req := httptest.NewRequest(fiber.MethodGet, "/test", http.NoBody)
4675 req.Header.Set("Authorization", "Bearer token1")

Callers

nothing calls this directly

Calls 12

newFailingCacheStorageFunction · 0.85
storeVaryManifestFunction · 0.85
loadVaryManifestFunction · 0.85
TestMethod · 0.80
NewFunction · 0.70
NewMethod · 0.65
ErrorMethod · 0.65
UseMethod · 0.65
GetMethod · 0.65
SetMethod · 0.65
ResponseMethod · 0.65
SendStringMethod · 0.65

Tested by

no test coverage detected