From 579524782bd70afd550746112195c3344b7e917c Mon Sep 17 00:00:00 2001 From: tung Date: Fri, 17 May 2024 13:15:36 +0700 Subject: [PATCH] add TestCacheByRequestURICustomCacheStrategy --- cache_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/cache_test.go b/cache_test.go index 6d108d0..3f97d41 100644 --- a/cache_test.go +++ b/cache_test.go @@ -293,3 +293,28 @@ func TestCustomCacheStrategy(t *testing.T) { err := memoryStore.Get("custom_cache_key_1", &val) assert.Nil(t, err) } + +func TestCacheByRequestURICustomCacheStrategy(t *testing.T) { + const customKey = "CustomKey" + memoryStore := persist.NewMemoryStore(1 * time.Minute) + cacheURIMiddleware := CacheByRequestURI(memoryStore, 1*time.Second, WithCacheStrategyByRequest(func(c *gin.Context) (bool, Strategy) { + return true, Strategy{ + CacheKey: customKey, + CacheDuration: 2 * time.Second, + } + })) + + w1 := mockHttpRequest(cacheURIMiddleware, "/cache?uid=u1", true) + var val interface{} + err := memoryStore.Get(customKey, &val) + assert.Nil(t, err) + time.Sleep(1 * time.Second) + + w2 := mockHttpRequest(cacheURIMiddleware, "/cache?uid=u1", true) + assert.Equal(t, w1.Body, w2.Body) + assert.Equal(t, w1.Code, w2.Code) + time.Sleep(3 * time.Second) + + w3 := mockHttpRequest(cacheURIMiddleware, "/cache?uid=u1", true) + assert.NotEqual(t, w1.Body, w3.Body) +}