Skip to content

Commit 1cca826

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

File tree

1 file changed

+81
-16
lines changed

1 file changed

+81
-16
lines changed

middleware/cache/cache_test.go

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -700,14 +700,68 @@ func Test_CustomCacheHeader(t *testing.T) {
700700
func Test_CacheInvalidation(t *testing.T) {
701701
t.Parallel()
702702

703-
t.Run("Invalidation by requests", func(t *testing.T) {
703+
app := fiber.New()
704+
app.Use(New(Config{
705+
CacheControl: true,
706+
CacheInvalidator: func(c fiber.Ctx) bool {
707+
return fiber.Query[bool](c, "invalidate")
708+
},
709+
}))
710+
711+
app.Get("/", func(c fiber.Ctx) error {
712+
return c.SendString(time.Now().String())
713+
})
714+
715+
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
716+
require.NoError(t, err)
717+
body, err := io.ReadAll(resp.Body)
718+
require.NoError(t, err)
719+
720+
respCached, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
721+
require.NoError(t, err)
722+
bodyCached, err := io.ReadAll(respCached.Body)
723+
require.NoError(t, err)
724+
require.True(t, bytes.Equal(body, bodyCached))
725+
require.NotEmpty(t, respCached.Header.Get(fiber.HeaderCacheControl))
726+
727+
respInvalidate, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/?invalidate=true", nil))
728+
require.NoError(t, err)
729+
bodyInvalidate, err := io.ReadAll(respInvalidate.Body)
730+
require.NoError(t, err)
731+
require.NotEqual(t, body, bodyInvalidate)
732+
}
733+
734+
func Test_CacheInvalidation_noCacheEntry(t *testing.T) {
735+
t.Parallel()
736+
t.Run("Cache Invalidator should not be called if no cache entry exist ", func(t *testing.T) {
737+
t.Parallel()
738+
app := fiber.New()
739+
cacheInvalidatorExecuted := false
740+
app.Use(New(Config{
741+
CacheControl: true,
742+
CacheInvalidator: func(c fiber.Ctx) bool {
743+
cacheInvalidatorExecuted = true
744+
return fiber.Query[bool](c, "invalidate")
745+
},
746+
MaxBytes: 10 * 1024 * 1024,
747+
}))
748+
_, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/?invalidate=true", nil))
749+
require.NoError(t, err)
750+
require.False(t, cacheInvalidatorExecuted)
751+
})
752+
}
753+
754+
func Test_CacheInvalidation_removeFromHeap(t *testing.T) {
755+
t.Parallel()
756+
t.Run("Invalidate and remove from the heap", func(t *testing.T) {
704757
t.Parallel()
705758
app := fiber.New()
706759
app.Use(New(Config{
707760
CacheControl: true,
708761
CacheInvalidator: func(c fiber.Ctx) bool {
709762
return fiber.Query[bool](c, "invalidate")
710763
},
764+
MaxBytes: 10 * 1024 * 1024,
711765
}))
712766

713767
app.Get("/", func(c fiber.Ctx) error {
@@ -732,23 +786,34 @@ func Test_CacheInvalidation(t *testing.T) {
732786
require.NoError(t, err)
733787
require.NotEqual(t, body, bodyInvalidate)
734788
})
789+
}
735790

736-
t.Run("Cache Invalidator should not be called if no cache entry exist ", func(t *testing.T) {
737-
t.Parallel()
738-
app := fiber.New()
739-
cacheInvalidatorExecuted := false
740-
app.Use(New(Config{
741-
CacheControl: true,
742-
CacheInvalidator: func(c fiber.Ctx) bool {
743-
cacheInvalidatorExecuted = true
744-
return fiber.Query[bool](c, "invalidate")
745-
},
746-
MaxBytes: 10 * 1024 * 1024,
747-
}))
748-
_, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/?invalidate=true", nil))
749-
require.NoError(t, err)
750-
require.False(t, cacheInvalidatorExecuted)
791+
func Test_CacheStorage_CustomHeaders(t *testing.T) {
792+
t.Parallel()
793+
app := fiber.New()
794+
app.Use(New(Config{
795+
CacheControl: true,
796+
Storage: memory.New(),
797+
MaxBytes: 10 * 1024 * 1024,
798+
}))
799+
800+
app.Get("/", func(c fiber.Ctx) error {
801+
c.Response().Header.Set("Content-Type", "text/xml")
802+
c.Response().Header.Set("Content-Encoding", "utf8")
803+
return c.Send([]byte("<xml><value>Test</value></xml>"))
751804
})
805+
806+
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
807+
require.NoError(t, err)
808+
body, err := io.ReadAll(resp.Body)
809+
require.NoError(t, err)
810+
811+
respCached, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
812+
require.NoError(t, err)
813+
bodyCached, err := io.ReadAll(respCached.Body)
814+
require.NoError(t, err)
815+
require.True(t, bytes.Equal(body, bodyCached))
816+
require.NotEmpty(t, respCached.Header.Get(fiber.HeaderCacheControl))
752817
}
753818

754819
// Because time points are updated once every X milliseconds, entries in tests can often have

0 commit comments

Comments
 (0)