@@ -700,14 +700,68 @@ func Test_CustomCacheHeader(t *testing.T) {
700
700
func Test_CacheInvalidation (t * testing.T ) {
701
701
t .Parallel ()
702
702
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 ) {
704
757
t .Parallel ()
705
758
app := fiber .New ()
706
759
app .Use (New (Config {
707
760
CacheControl : true ,
708
761
CacheInvalidator : func (c fiber.Ctx ) bool {
709
762
return fiber .Query [bool ](c , "invalidate" )
710
763
},
764
+ MaxBytes : 10 * 1024 * 1024 ,
711
765
}))
712
766
713
767
app .Get ("/" , func (c fiber.Ctx ) error {
@@ -732,23 +786,34 @@ func Test_CacheInvalidation(t *testing.T) {
732
786
require .NoError (t , err )
733
787
require .NotEqual (t , body , bodyInvalidate )
734
788
})
789
+ }
735
790
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>" ))
751
804
})
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 ))
752
817
}
753
818
754
819
// Because time points are updated once every X milliseconds, entries in tests can often have
0 commit comments