@@ -697,9 +697,8 @@ func Test_CustomCacheHeader(t *testing.T) {
697
697
require .Equal (t , cacheMiss , resp .Header .Get ("Cache-Status" ))
698
698
}
699
699
700
- func Test_CacheInvalidation (t * testing.T ) {
700
+ func Test_CacheInvalidation_byRequest (t * testing.T ) {
701
701
t .Parallel ()
702
-
703
702
t .Run ("Invalidation by requests" , func (t * testing.T ) {
704
703
t .Parallel ()
705
704
app := fiber .New ()
@@ -732,7 +731,10 @@ func Test_CacheInvalidation(t *testing.T) {
732
731
require .NoError (t , err )
733
732
require .NotEqual (t , body , bodyInvalidate )
734
733
})
734
+ }
735
735
736
+ func Test_CacheInvalidation_noCacheEntry (t * testing.T ) {
737
+ t .Parallel ()
736
738
t .Run ("Cache Invalidator should not be called if no cache entry exist " , func (t * testing.T ) {
737
739
t .Parallel ()
738
740
app := fiber .New ()
@@ -751,6 +753,71 @@ func Test_CacheInvalidation(t *testing.T) {
751
753
})
752
754
}
753
755
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
+
754
821
// Because time points are updated once every X milliseconds, entries in tests can often have
755
822
// equal expiration times and thus be in an random order. This closure hands out increasing
756
823
// time intervals to maintain strong ascending order of expiration
0 commit comments