Test_CustomExpiration() — fiber Function Reference
Architecture documentation for the Test_CustomExpiration() function in cache_test.go from the fiber codebase.
Entity Profile
Dependency Diagram
graph TD f66ad48d_b82f_7e76_4993_6ae050eea173["Test_CustomExpiration()"] 8453a087_9678_fe96_1b20_2d125b6f8656["cache_test.go"] f66ad48d_b82f_7e76_4993_6ae050eea173 -->|defined in| 8453a087_9678_fe96_1b20_2d125b6f8656 style f66ad48d_b82f_7e76_4993_6ae050eea173 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
middleware/cache/cache_test.go lines 1058–1114
func Test_CustomExpiration(t *testing.T) {
t.Parallel()
app := fiber.New()
var called bool
var newCacheTime int
app.Use(New(Config{ExpirationGenerator: func(c fiber.Ctx, _ *Config) time.Duration {
called = true
var err error
newCacheTime, err = strconv.Atoi(c.GetRespHeader("Cache-Time", "600"))
require.NoError(t, err)
return time.Second * time.Duration(newCacheTime)
}}))
count := 0
app.Get("/", func(c fiber.Ctx) error {
count++
c.Response().Header.Add("Cache-Time", "1")
return c.SendString(strconv.Itoa(count))
})
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", http.NoBody))
require.NoError(t, err)
require.True(t, called)
require.Equal(t, 1, newCacheTime)
// Wait until the cache expires (timestamp tick can delay expiry detection slightly).
expireDeadline := time.Now().Add(3 * time.Second)
var cachedResp *http.Response
for {
cachedResp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/", http.NoBody))
require.NoError(t, err)
if cachedResp.Header.Get("X-Cache") != cacheHit {
break
}
require.True(t, time.Now().Before(expireDeadline), "response remained cached beyond expected expiration")
time.Sleep(50 * time.Millisecond)
}
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
cachedBody, err := io.ReadAll(cachedResp.Body)
require.NoError(t, err)
if bytes.Equal(body, cachedBody) {
t.Errorf("Cache should have expired: %s, %s", body, cachedBody)
}
// Next response should be cached
cachedRespNextRound, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", http.NoBody))
require.NoError(t, err)
cachedBodyNextRound, err := io.ReadAll(cachedRespNextRound.Body)
require.NoError(t, err)
if !bytes.Equal(cachedBodyNextRound, cachedBody) {
t.Errorf("Cache should not have expired: %s, %s", cachedBodyNextRound, cachedBody)
}
}
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does Test_CustomExpiration() do?
Test_CustomExpiration() is a function in the fiber codebase, defined in middleware/cache/cache_test.go.
Where is Test_CustomExpiration() defined?
Test_CustomExpiration() is defined in middleware/cache/cache_test.go at line 1058.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free