Test_Cache_VaryAndAuth tests vary and auth functionality
(t *testing.T)
| 4616 | |
| 4617 | // Test_Cache_VaryAndAuth tests vary and auth functionality |
| 4618 | func 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") |
nothing calls this directly
no test coverage detected