Home / Function/ Test_CacheDateAndAgeHandling() — fiber Function Reference

Test_CacheDateAndAgeHandling() — fiber Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

middleware/cache/cache_test.go lines 2085–2161

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

	type testCase struct {
		name             string
		cacheControl     string
		cacheHeader      string
		dateOffset       time.Duration
		expiration       time.Duration
		expectAgeAtLeast int
		expectCount      int
		originAge        int
	}

	cases := []testCase{
		{
			name:             "age derived from past date without Age header",
			dateOffset:       -1 * time.Minute,
			cacheControl:     "public, max-age=120",
			cacheHeader:      cacheHit,
			expiration:       5 * time.Minute,
			expectAgeAtLeast: 1,
			expectCount:      1,
		},
		{
			name:         "stale due to past date despite max-age",
			dateOffset:   -90 * time.Second,
			cacheControl: "public, max-age=30",
			cacheHeader:  cacheUnreachable,
			expiration:   5 * time.Minute,
			expectCount:  2,
			originAge:    90,
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			app := fiber.New()
			app.Use(New(Config{Expiration: tc.expiration}))
			app.Use(setResponseDate(time.Now().Add(tc.dateOffset).UTC()))

			var count int
			app.Get("/", func(c fiber.Ctx) error {
				count++
				if tc.originAge > 0 {
					c.Response().Header.Set(fiber.HeaderAge, strconv.Itoa(tc.originAge))
				}
				c.Set(fiber.HeaderCacheControl, tc.cacheControl)
				return c.SendString(strconv.Itoa(count))
			})

			_, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", http.NoBody))
			require.NoError(t, err)

			if tc.cacheHeader == cacheHit {
				time.Sleep(2 * time.Second)
			}

			resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", http.NoBody))
			require.NoError(t, err)
			require.Equal(t, tc.cacheHeader, resp.Header.Get("X-Cache"))
			if tc.cacheHeader == cacheHit {
				ageVal, err := strconv.Atoi(resp.Header.Get(fiber.HeaderAge))
				require.NoError(t, err)
				require.GreaterOrEqual(t, ageVal, tc.expectAgeAtLeast)
				require.Equal(t, 1, count)
			} else {
				body, err := io.ReadAll(resp.Body)
				require.NoError(t, err)
				require.Equal(t, strconv.Itoa(tc.expectCount), string(body))
				require.Equal(t, tc.expectCount, count)
			}
		})
	}
}

Subdomains

Frequently Asked Questions

What does Test_CacheDateAndAgeHandling() do?
Test_CacheDateAndAgeHandling() is a function in the fiber codebase, defined in middleware/cache/cache_test.go.
Where is Test_CacheDateAndAgeHandling() defined?
Test_CacheDateAndAgeHandling() is defined in middleware/cache/cache_test.go at line 2085.
What does Test_CacheDateAndAgeHandling() call?
Test_CacheDateAndAgeHandling() calls 1 function(s): setResponseDate.

Analyze Your Own Codebase

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

Try Supermodel Free