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

Function Test_CacheHeader

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

Source from the content-addressed store, hash-verified

1640}
1641
1642func Test_CacheHeader(t *testing.T) {
1643 t.Parallel()
1644
1645 app := fiber.New()
1646
1647 app.Use(New(Config{
1648 Expiration: 10 * time.Second,
1649 Next: func(c fiber.Ctx) bool {
1650 return c.Response().StatusCode() != fiber.StatusOK
1651 },
1652 }))
1653
1654 app.Get("/", func(c fiber.Ctx) error {
1655 return c.SendString("Hello, World!")
1656 })
1657
1658 app.Post("/", func(c fiber.Ctx) error {
1659 return c.SendString(fiber.Query[string](c, "cache"))
1660 })
1661
1662 count := 0
1663 app.Get("/error", func(c fiber.Ctx) error {
1664 count++
1665 c.Response().Header.Add("Cache-Time", "1")
1666 return c.Status(fiber.StatusInternalServerError).SendString(strconv.Itoa(count))
1667 })
1668
1669 resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", http.NoBody))
1670 require.NoError(t, err)
1671 require.Equal(t, cacheMiss, resp.Header.Get("X-Cache"))
1672
1673 resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/", http.NoBody))
1674 require.NoError(t, err)
1675 require.Equal(t, cacheHit, resp.Header.Get("X-Cache"))
1676
1677 resp, err = app.Test(httptest.NewRequest(fiber.MethodPost, "/?cache=12345", http.NoBody))
1678 require.NoError(t, err)
1679 require.Equal(t, cacheUnreachable, resp.Header.Get("X-Cache"))
1680
1681 errRespCached, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/error", http.NoBody))
1682 require.NoError(t, err)
1683 require.Equal(t, cacheUnreachable, errRespCached.Header.Get("X-Cache"))
1684}
1685
1686func Test_Cache_WithHead(t *testing.T) {
1687 t.Parallel()

Callers

nothing calls this directly

Calls 11

TestMethod · 0.80
NewFunction · 0.70
NewMethod · 0.65
UseMethod · 0.65
ResponseMethod · 0.65
GetMethod · 0.65
SendStringMethod · 0.65
PostMethod · 0.65
AddMethod · 0.65
StatusMethod · 0.65
StatusCodeMethod · 0.45

Tested by

no test coverage detected