@@ -47,9 +47,10 @@ func Test_Cache_Expired(t *testing.T) {
47
47
t .Parallel ()
48
48
app := fiber .New ()
49
49
app .Use (New (Config {Expiration : 2 * time .Second }))
50
-
50
+ count := 0
51
51
app .Get ("/" , func (c fiber.Ctx ) error {
52
- return c .SendString (strconv .FormatInt (time .Now ().UnixNano (), 10 ))
52
+ count ++
53
+ return c .SendString (strconv .Itoa (count ))
53
54
})
54
55
55
56
resp , err := app .Test (httptest .NewRequest (fiber .MethodGet , "/" , nil ))
@@ -86,9 +87,10 @@ func Test_Cache(t *testing.T) {
86
87
app := fiber .New ()
87
88
app .Use (New ())
88
89
90
+ count := 0
89
91
app .Get ("/" , func (c fiber.Ctx ) error {
90
- now := strconv . FormatInt ( time . Now (). UnixNano (), 10 )
91
- return c .SendString (now )
92
+ count ++
93
+ return c .SendString (strconv . Itoa ( count ) )
92
94
})
93
95
94
96
req := httptest .NewRequest (fiber .MethodGet , "/" , nil )
@@ -305,9 +307,10 @@ func Test_Cache_Invalid_Expiration(t *testing.T) {
305
307
cache := New (Config {Expiration : 0 * time .Second })
306
308
app .Use (cache )
307
309
310
+ count := 0
308
311
app .Get ("/" , func (c fiber.Ctx ) error {
309
- now := strconv . FormatInt ( time . Now (). UnixNano (), 10 )
310
- return c .SendString (now )
312
+ count ++
313
+ return c .SendString (strconv . Itoa ( count ) )
311
314
})
312
315
313
316
req := httptest .NewRequest (fiber .MethodGet , "/" , nil )
@@ -414,8 +417,10 @@ func Test_Cache_NothingToCache(t *testing.T) {
414
417
415
418
app .Use (New (Config {Expiration : - (time .Second * 1 )}))
416
419
420
+ count := 0
417
421
app .Get ("/" , func (c fiber.Ctx ) error {
418
- return c .SendString (time .Now ().String ())
422
+ count ++
423
+ return c .SendString (strconv .Itoa (count ))
419
424
})
420
425
421
426
resp , err := app .Test (httptest .NewRequest (fiber .MethodGet , "/" , nil ))
@@ -447,12 +452,16 @@ func Test_Cache_CustomNext(t *testing.T) {
447
452
CacheControl : true ,
448
453
}))
449
454
455
+ count := 0
450
456
app .Get ("/" , func (c fiber.Ctx ) error {
451
- return c .SendString (time .Now ().String ())
457
+ count ++
458
+ return c .SendString (strconv .Itoa (count ))
452
459
})
453
460
461
+ errorCount := 0
454
462
app .Get ("/error" , func (c fiber.Ctx ) error {
455
- return c .Status (fiber .StatusInternalServerError ).SendString (time .Now ().String ())
463
+ errorCount ++
464
+ return c .Status (fiber .StatusInternalServerError ).SendString (strconv .Itoa (errorCount ))
456
465
})
457
466
458
467
resp , err := app .Test (httptest .NewRequest (fiber .MethodGet , "/" , nil ))
@@ -508,9 +517,11 @@ func Test_CustomExpiration(t *testing.T) {
508
517
return time .Second * time .Duration (newCacheTime )
509
518
}}))
510
519
520
+ count := 0
511
521
app .Get ("/" , func (c fiber.Ctx ) error {
522
+ count ++
512
523
c .Response ().Header .Add ("Cache-Time" , "1" )
513
- return c .SendString (strconv .FormatInt ( time . Now (). UnixNano (), 10 ))
524
+ return c .SendString (strconv .Itoa ( count ))
514
525
})
515
526
516
527
resp , err := app .Test (httptest .NewRequest (fiber .MethodGet , "/" , nil ))
@@ -588,8 +599,11 @@ func Test_CacheHeader(t *testing.T) {
588
599
return c .SendString (fiber .Query [string ](c , "cache" ))
589
600
})
590
601
602
+ count := 0
591
603
app .Get ("/error" , func (c fiber.Ctx ) error {
592
- return c .Status (fiber .StatusInternalServerError ).SendString (time .Now ().String ())
604
+ count ++
605
+ c .Response ().Header .Add ("Cache-Time" , "1" )
606
+ return c .Status (fiber .StatusInternalServerError ).SendString (strconv .Itoa (count ))
593
607
})
594
608
595
609
resp , err := app .Test (httptest .NewRequest (fiber .MethodGet , "/" , nil ))
@@ -615,10 +629,13 @@ func Test_Cache_WithHead(t *testing.T) {
615
629
app := fiber .New ()
616
630
app .Use (New ())
617
631
632
+ count := 0
618
633
handler := func (c fiber.Ctx ) error {
619
- now := strconv .FormatInt (time .Now ().UnixNano (), 10 )
620
- return c .SendString (now )
634
+ count ++
635
+ c .Response ().Header .Add ("Cache-Time" , "1" )
636
+ return c .SendString (strconv .Itoa (count ))
621
637
}
638
+
622
639
app .Route ("/" ).Get (handler ).Head (handler )
623
640
624
641
req := httptest .NewRequest (fiber .MethodHead , "/" , nil )
@@ -708,8 +725,10 @@ func Test_CacheInvalidation(t *testing.T) {
708
725
},
709
726
}))
710
727
728
+ count := 0
711
729
app .Get ("/" , func (c fiber.Ctx ) error {
712
- return c .SendString (time .Now ().String ())
730
+ count ++
731
+ return c .SendString (strconv .Itoa (count ))
713
732
})
714
733
715
734
resp , err := app .Test (httptest .NewRequest (fiber .MethodGet , "/" , nil ))
@@ -731,6 +750,93 @@ func Test_CacheInvalidation(t *testing.T) {
731
750
require .NotEqual (t , body , bodyInvalidate )
732
751
}
733
752
753
+ func Test_CacheInvalidation_noCacheEntry (t * testing.T ) {
754
+ t .Parallel ()
755
+ t .Run ("Cache Invalidator should not be called if no cache entry exist " , func (t * testing.T ) {
756
+ t .Parallel ()
757
+ app := fiber .New ()
758
+ cacheInvalidatorExecuted := false
759
+ app .Use (New (Config {
760
+ CacheControl : true ,
761
+ CacheInvalidator : func (c fiber.Ctx ) bool {
762
+ cacheInvalidatorExecuted = true
763
+ return fiber .Query [bool ](c , "invalidate" )
764
+ },
765
+ MaxBytes : 10 * 1024 * 1024 ,
766
+ }))
767
+ _ , err := app .Test (httptest .NewRequest (fiber .MethodGet , "/?invalidate=true" , nil ))
768
+ require .NoError (t , err )
769
+ require .False (t , cacheInvalidatorExecuted )
770
+ })
771
+ }
772
+
773
+ func Test_CacheInvalidation_removeFromHeap (t * testing.T ) {
774
+ t .Parallel ()
775
+ t .Run ("Invalidate and remove from the heap" , func (t * testing.T ) {
776
+ t .Parallel ()
777
+ app := fiber .New ()
778
+ app .Use (New (Config {
779
+ CacheControl : true ,
780
+ CacheInvalidator : func (c fiber.Ctx ) bool {
781
+ return fiber .Query [bool ](c , "invalidate" )
782
+ },
783
+ MaxBytes : 10 * 1024 * 1024 ,
784
+ }))
785
+
786
+ count := 0
787
+ app .Get ("/" , func (c fiber.Ctx ) error {
788
+ count ++
789
+ return c .SendString (strconv .Itoa (count ))
790
+ })
791
+
792
+ resp , err := app .Test (httptest .NewRequest (fiber .MethodGet , "/" , nil ))
793
+ require .NoError (t , err )
794
+ body , err := io .ReadAll (resp .Body )
795
+ require .NoError (t , err )
796
+
797
+ respCached , err := app .Test (httptest .NewRequest (fiber .MethodGet , "/" , nil ))
798
+ require .NoError (t , err )
799
+ bodyCached , err := io .ReadAll (respCached .Body )
800
+ require .NoError (t , err )
801
+ require .True (t , bytes .Equal (body , bodyCached ))
802
+ require .NotEmpty (t , respCached .Header .Get (fiber .HeaderCacheControl ))
803
+
804
+ respInvalidate , err := app .Test (httptest .NewRequest (fiber .MethodGet , "/?invalidate=true" , nil ))
805
+ require .NoError (t , err )
806
+ bodyInvalidate , err := io .ReadAll (respInvalidate .Body )
807
+ require .NoError (t , err )
808
+ require .NotEqual (t , body , bodyInvalidate )
809
+ })
810
+ }
811
+
812
+ func Test_CacheStorage_CustomHeaders (t * testing.T ) {
813
+ t .Parallel ()
814
+ app := fiber .New ()
815
+ app .Use (New (Config {
816
+ CacheControl : true ,
817
+ Storage : memory .New (),
818
+ MaxBytes : 10 * 1024 * 1024 ,
819
+ }))
820
+
821
+ app .Get ("/" , func (c fiber.Ctx ) error {
822
+ c .Response ().Header .Set ("Content-Type" , "text/xml" )
823
+ c .Response ().Header .Set ("Content-Encoding" , "utf8" )
824
+ return c .Send ([]byte ("<xml><value>Test</value></xml>" ))
825
+ })
826
+
827
+ resp , err := app .Test (httptest .NewRequest (fiber .MethodGet , "/" , nil ))
828
+ require .NoError (t , err )
829
+ body , err := io .ReadAll (resp .Body )
830
+ require .NoError (t , err )
831
+
832
+ respCached , err := app .Test (httptest .NewRequest (fiber .MethodGet , "/" , nil ))
833
+ require .NoError (t , err )
834
+ bodyCached , err := io .ReadAll (respCached .Body )
835
+ require .NoError (t , err )
836
+ require .True (t , bytes .Equal (body , bodyCached ))
837
+ require .NotEmpty (t , respCached .Header .Get (fiber .HeaderCacheControl ))
838
+ }
839
+
734
840
// Because time points are updated once every X milliseconds, entries in tests can often have
735
841
// equal expiration times and thus be in an random order. This closure hands out increasing
736
842
// time intervals to maintain strong ascending order of expiration
0 commit comments