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

Function Test_Cache_WithHead

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

Source from the content-addressed store, hash-verified

1684}
1685
1686func Test_Cache_WithHead(t *testing.T) {
1687 t.Parallel()
1688
1689 app := fiber.New()
1690 app.Use(New())
1691
1692 count := 0
1693 handler := func(c fiber.Ctx) error {
1694 count++
1695 c.Response().Header.Add("Cache-Time", "1")
1696 return c.SendString(strconv.Itoa(count))
1697 }
1698
1699 app.RouteChain("/").Get(handler).Head(handler)
1700
1701 req := httptest.NewRequest(fiber.MethodHead, "/", http.NoBody)
1702 resp, err := app.Test(req)
1703 require.NoError(t, err)
1704 require.Equal(t, cacheMiss, resp.Header.Get("X-Cache"))
1705
1706 cachedReq := httptest.NewRequest(fiber.MethodHead, "/", http.NoBody)
1707 cachedResp, err := app.Test(cachedReq)
1708 require.NoError(t, err)
1709 require.Equal(t, cacheHit, cachedResp.Header.Get("X-Cache"))
1710
1711 body, err := io.ReadAll(resp.Body)
1712 require.NoError(t, err)
1713 cachedBody, err := io.ReadAll(cachedResp.Body)
1714 require.NoError(t, err)
1715
1716 require.Equal(t, cachedBody, body)
1717}
1718
1719func Test_Cache_WithHeadThenGet(t *testing.T) {
1720 t.Parallel()

Callers

nothing calls this directly

Calls 10

TestMethod · 0.80
NewFunction · 0.70
NewMethod · 0.65
UseMethod · 0.65
AddMethod · 0.65
ResponseMethod · 0.65
SendStringMethod · 0.65
HeadMethod · 0.65
GetMethod · 0.65
RouteChainMethod · 0.65

Tested by

no test coverage detected