Home / Function/ Test_Cache_WithNoCacheRequestDirective() — fiber Function Reference

Test_Cache_WithNoCacheRequestDirective() — fiber Function Reference

Architecture documentation for the Test_Cache_WithNoCacheRequestDirective() function in cache_test.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  cca0386c_22b9_f530_23ca_f9ebcc5899de["Test_Cache_WithNoCacheRequestDirective()"]
  8453a087_9678_fe96_1b20_2d125b6f8656["cache_test.go"]
  cca0386c_22b9_f530_23ca_f9ebcc5899de -->|defined in| 8453a087_9678_fe96_1b20_2d125b6f8656
  style cca0386c_22b9_f530_23ca_f9ebcc5899de fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/cache/cache_test.go lines 593–687

func Test_Cache_WithNoCacheRequestDirective(t *testing.T) {
	t.Parallel()

	app := fiber.New()
	app.Use(New())

	app.Get("/", func(c fiber.Ctx) error {
		return c.SendString(fiber.Query(c, "id", "1"))
	})

	// Request id = 1
	req := httptest.NewRequest(fiber.MethodGet, "/", http.NoBody)
	resp, err := app.Test(req)
	require.NoError(t, err)
	body, err := io.ReadAll(resp.Body)
	require.NoError(t, err)
	require.Equal(t, cacheMiss, resp.Header.Get("X-Cache"))
	require.Equal(t, []byte("1"), body)
	// Response cached, entry id = 1

	// Request id = 2 without Cache-Control: no-cache
	cachedReq := httptest.NewRequest(fiber.MethodGet, "/?id=2", http.NoBody)
	cachedResp, err := app.Test(cachedReq)
	require.NoError(t, err)
	cachedBody, err := io.ReadAll(cachedResp.Body)
	require.NoError(t, err)
	require.Equal(t, cacheHit, cachedResp.Header.Get("X-Cache"))
	require.Equal(t, []byte("1"), cachedBody)
	// Response not cached, returns cached response, entry id = 1

	// Request id = 2 with Cache-Control: no-cache
	noCacheReq := httptest.NewRequest(fiber.MethodGet, "/?id=2", http.NoBody)
	noCacheReq.Header.Set(fiber.HeaderCacheControl, noCache)
	noCacheResp, err := app.Test(noCacheReq)
	require.NoError(t, err)
	noCacheBody, err := io.ReadAll(noCacheResp.Body)
	require.NoError(t, err)
	require.Equal(t, cacheMiss, noCacheResp.Header.Get("X-Cache"))
	require.Equal(t, []byte("2"), noCacheBody)
	// Response cached, returns updated response, entry = 2

	/* Check Test_Cache_WithETagAndNoCacheRequestDirective */
	// Request id = 2 with Cache-Control: no-cache again
	noCacheReq1 := httptest.NewRequest(fiber.MethodGet, "/?id=2", http.NoBody)
	noCacheReq1.Header.Set(fiber.HeaderCacheControl, noCache)
	noCacheResp1, err := app.Test(noCacheReq1)
	require.NoError(t, err)
	noCacheBody1, err := io.ReadAll(noCacheResp1.Body)
	require.NoError(t, err)
	require.Equal(t, cacheMiss, noCacheResp1.Header.Get("X-Cache"))
	require.Equal(t, []byte("2"), noCacheBody1)
	// Response cached, returns updated response, entry = 2

	// Request id = 3 with Cache-Control: NO-CACHE
	noCacheReqUpper := httptest.NewRequest(fiber.MethodGet, "/?id=3", http.NoBody)
	noCacheReqUpper.Header.Set(fiber.HeaderCacheControl, "NO-CACHE")
	noCacheRespUpper, err := app.Test(noCacheReqUpper)
	require.NoError(t, err)
	noCacheBodyUpper, err := io.ReadAll(noCacheRespUpper.Body)
	require.NoError(t, err)
	require.Equal(t, cacheMiss, noCacheRespUpper.Header.Get("X-Cache"))
	require.Equal(t, []byte("3"), noCacheBodyUpper)
	// Response cached, returns updated response, entry = 3

	// Request id = 4 with Cache-Control: my-no-cache
	invalidReq := httptest.NewRequest(fiber.MethodGet, "/?id=4", http.NoBody)
	invalidReq.Header.Set(fiber.HeaderCacheControl, "my-no-cache")
	invalidResp, err := app.Test(invalidReq)
	require.NoError(t, err)
	invalidBody, err := io.ReadAll(invalidResp.Body)
	require.NoError(t, err)
	require.Equal(t, cacheHit, invalidResp.Header.Get("X-Cache"))
	require.Equal(t, []byte("3"), invalidBody)
	// Response served from cache, existing entry = 3

	// Request id = 4 again without Cache-Control: no-cache
	cachedInvalidReq := httptest.NewRequest(fiber.MethodGet, "/?id=4", http.NoBody)
	cachedInvalidResp, err := app.Test(cachedInvalidReq)
	require.NoError(t, err)
	cachedInvalidBody, err := io.ReadAll(cachedInvalidResp.Body)
	require.NoError(t, err)

Subdomains

Frequently Asked Questions

What does Test_Cache_WithNoCacheRequestDirective() do?
Test_Cache_WithNoCacheRequestDirective() is a function in the fiber codebase, defined in middleware/cache/cache_test.go.
Where is Test_Cache_WithNoCacheRequestDirective() defined?
Test_Cache_WithNoCacheRequestDirective() is defined in middleware/cache/cache_test.go at line 593.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free