Skip to content

Commit 30bb1c2

Browse files
committed
Adding tests for coverage
Signed-off-by: brunodmartins <[email protected]>
1 parent ad51c7b commit 30bb1c2

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

Diff for: middleware/cache/cache_test.go

+69-2
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,8 @@ func Test_CustomCacheHeader(t *testing.T) {
697697
require.Equal(t, cacheMiss, resp.Header.Get("Cache-Status"))
698698
}
699699

700-
func Test_CacheInvalidation(t *testing.T) {
700+
func Test_CacheInvalidation_byRequest(t *testing.T) {
701701
t.Parallel()
702-
703702
t.Run("Invalidation by requests", func(t *testing.T) {
704703
t.Parallel()
705704
app := fiber.New()
@@ -732,7 +731,10 @@ func Test_CacheInvalidation(t *testing.T) {
732731
require.NoError(t, err)
733732
require.NotEqual(t, body, bodyInvalidate)
734733
})
734+
}
735735

736+
func Test_CacheInvalidation_noCacheEntry(t *testing.T) {
737+
t.Parallel()
736738
t.Run("Cache Invalidator should not be called if no cache entry exist ", func(t *testing.T) {
737739
t.Parallel()
738740
app := fiber.New()
@@ -751,6 +753,71 @@ func Test_CacheInvalidation(t *testing.T) {
751753
})
752754
}
753755

756+
func Test_CacheInvalidation_removeFromHeap(t *testing.T) {
757+
t.Parallel()
758+
t.Run("Invalidate and remove from the heap", func(t *testing.T) {
759+
t.Parallel()
760+
app := fiber.New()
761+
app.Use(New(Config{
762+
CacheControl: true,
763+
CacheInvalidator: func(c fiber.Ctx) bool {
764+
return fiber.Query[bool](c, "invalidate")
765+
},
766+
MaxBytes: 10 * 1024 * 1024,
767+
}))
768+
769+
app.Get("/", func(c fiber.Ctx) error {
770+
return c.SendString(time.Now().String())
771+
})
772+
773+
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
774+
require.NoError(t, err)
775+
body, err := io.ReadAll(resp.Body)
776+
require.NoError(t, err)
777+
778+
respCached, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
779+
require.NoError(t, err)
780+
bodyCached, err := io.ReadAll(respCached.Body)
781+
require.NoError(t, err)
782+
require.True(t, bytes.Equal(body, bodyCached))
783+
require.NotEmpty(t, respCached.Header.Get(fiber.HeaderCacheControl))
784+
785+
respInvalidate, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/?invalidate=true", nil))
786+
require.NoError(t, err)
787+
bodyInvalidate, err := io.ReadAll(respInvalidate.Body)
788+
require.NoError(t, err)
789+
require.NotEqual(t, body, bodyInvalidate)
790+
})
791+
}
792+
793+
func Test_CacheStorage_CustomHeaders(t *testing.T) {
794+
t.Parallel()
795+
app := fiber.New()
796+
app.Use(New(Config{
797+
CacheControl: true,
798+
Storage: memory.New(),
799+
MaxBytes: 10 * 1024 * 1024,
800+
}))
801+
802+
app.Get("/", func(c fiber.Ctx) error {
803+
c.Response().Header.Set("Content-Type", "text/xml")
804+
c.Response().Header.Set("Content-Encoding", "utf8")
805+
return c.Send([]byte("<xml><value>Test</value></xml>"))
806+
})
807+
808+
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
809+
require.NoError(t, err)
810+
body, err := io.ReadAll(resp.Body)
811+
require.NoError(t, err)
812+
813+
respCached, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
814+
require.NoError(t, err)
815+
bodyCached, err := io.ReadAll(respCached.Body)
816+
require.NoError(t, err)
817+
require.True(t, bytes.Equal(body, bodyCached))
818+
require.NotEmpty(t, respCached.Header.Get(fiber.HeaderCacheControl))
819+
}
820+
754821
// Because time points are updated once every X milliseconds, entries in tests can often have
755822
// equal expiration times and thus be in an random order. This closure hands out increasing
756823
// time intervals to maintain strong ascending order of expiration

0 commit comments

Comments
 (0)