@@ -19,6 +19,8 @@ import (
19
19
)
20
20
21
21
func Test_Cache_CacheControl (t * testing.T ) {
22
+ t .Parallel ()
23
+
22
24
app := fiber .New ()
23
25
24
26
app .Use (New (Config {
@@ -77,6 +79,8 @@ func Test_Cache_Expired(t *testing.T) {
77
79
}
78
80
79
81
func Test_Cache (t * testing.T ) {
82
+ t .Parallel ()
83
+
80
84
app := fiber .New ()
81
85
app .Use (New ())
82
86
@@ -102,6 +106,8 @@ func Test_Cache(t *testing.T) {
102
106
}
103
107
104
108
func Test_Cache_WithSeveralRequests (t * testing.T ) {
109
+ t .Parallel ()
110
+
105
111
app := fiber .New ()
106
112
107
113
app .Use (New (Config {
@@ -135,6 +141,8 @@ func Test_Cache_WithSeveralRequests(t *testing.T) {
135
141
}
136
142
137
143
func Test_Cache_Invalid_Expiration (t * testing.T ) {
144
+ t .Parallel ()
145
+
138
146
app := fiber .New ()
139
147
cache := New (Config {Expiration : 0 * time .Second })
140
148
app .Use (cache )
@@ -161,6 +169,8 @@ func Test_Cache_Invalid_Expiration(t *testing.T) {
161
169
}
162
170
163
171
func Test_Cache_Invalid_Method (t * testing.T ) {
172
+ t .Parallel ()
173
+
164
174
app := fiber .New ()
165
175
166
176
app .Use (New ())
@@ -199,6 +209,8 @@ func Test_Cache_Invalid_Method(t *testing.T) {
199
209
}
200
210
201
211
func Test_Cache_NothingToCache (t * testing.T ) {
212
+ t .Parallel ()
213
+
202
214
app := fiber .New ()
203
215
204
216
app .Use (New (Config {Expiration : - (time .Second * 1 )}))
@@ -225,6 +237,8 @@ func Test_Cache_NothingToCache(t *testing.T) {
225
237
}
226
238
227
239
func Test_Cache_CustomNext (t * testing.T ) {
240
+ t .Parallel ()
241
+
228
242
app := fiber .New ()
229
243
230
244
app .Use (New (Config {
@@ -263,6 +277,8 @@ func Test_Cache_CustomNext(t *testing.T) {
263
277
}
264
278
265
279
func Test_CustomKey (t * testing.T ) {
280
+ t .Parallel ()
281
+
266
282
app := fiber .New ()
267
283
var called bool
268
284
app .Use (New (Config {KeyGenerator : func (c * fiber.Ctx ) string {
@@ -281,6 +297,8 @@ func Test_CustomKey(t *testing.T) {
281
297
}
282
298
283
299
func Test_CustomExpiration (t * testing.T ) {
300
+ t .Parallel ()
301
+
284
302
app := fiber .New ()
285
303
var called bool
286
304
var newCacheTime int
@@ -291,18 +309,45 @@ func Test_CustomExpiration(t *testing.T) {
291
309
}}))
292
310
293
311
app .Get ("/" , func (c * fiber.Ctx ) error {
294
- c .Response ().Header .Add ("Cache-Time" , "6000" )
295
- return c .SendString ("hi" )
312
+ c .Response ().Header .Add ("Cache-Time" , "1" )
313
+ now := fmt .Sprintf ("%d" , time .Now ().UnixNano ())
314
+ return c .SendString (now )
296
315
})
297
316
298
- req := httptest .NewRequest ("GET" , "/" , nil )
299
- _ , err := app .Test (req )
317
+ resp , err := app .Test (httptest .NewRequest ("GET" , "/" , nil ))
300
318
utils .AssertEqual (t , nil , err )
301
319
utils .AssertEqual (t , true , called )
302
- utils .AssertEqual (t , 6000 , newCacheTime )
320
+ utils .AssertEqual (t , 1 , newCacheTime )
321
+
322
+ // Sleep until the cache is expired
323
+ time .Sleep (1 * time .Second )
324
+
325
+ cachedResp , err := app .Test (httptest .NewRequest ("GET" , "/" , nil ))
326
+ utils .AssertEqual (t , nil , err )
327
+
328
+ body , err := ioutil .ReadAll (resp .Body )
329
+ utils .AssertEqual (t , nil , err )
330
+ cachedBody , err := ioutil .ReadAll (cachedResp .Body )
331
+ utils .AssertEqual (t , nil , err )
332
+
333
+ if bytes .Equal (body , cachedBody ) {
334
+ t .Errorf ("Cache should have expired: %s, %s" , body , cachedBody )
335
+ }
336
+
337
+ // Next response should be cached
338
+ cachedRespNextRound , err := app .Test (httptest .NewRequest ("GET" , "/" , nil ))
339
+ utils .AssertEqual (t , nil , err )
340
+ cachedBodyNextRound , err := ioutil .ReadAll (cachedRespNextRound .Body )
341
+ utils .AssertEqual (t , nil , err )
342
+
343
+ if ! bytes .Equal (cachedBodyNextRound , cachedBody ) {
344
+ t .Errorf ("Cache should not have expired: %s, %s" , cachedBodyNextRound , cachedBody )
345
+ }
303
346
}
304
347
305
348
func Test_AdditionalE2EResponseHeaders (t * testing.T ) {
349
+ t .Parallel ()
350
+
306
351
app := fiber .New ()
307
352
app .Use (New (Config {
308
353
StoreResponseHeaders : true ,
@@ -325,6 +370,8 @@ func Test_AdditionalE2EResponseHeaders(t *testing.T) {
325
370
}
326
371
327
372
func Test_CacheHeader (t * testing.T ) {
373
+ t .Parallel ()
374
+
328
375
app := fiber .New ()
329
376
330
377
app .Use (New (Config {
@@ -364,6 +411,8 @@ func Test_CacheHeader(t *testing.T) {
364
411
}
365
412
366
413
func Test_Cache_WithHead (t * testing.T ) {
414
+ t .Parallel ()
415
+
367
416
app := fiber .New ()
368
417
app .Use (New ())
369
418
@@ -389,6 +438,8 @@ func Test_Cache_WithHead(t *testing.T) {
389
438
}
390
439
391
440
func Test_Cache_WithHeadThenGet (t * testing.T ) {
441
+ t .Parallel ()
442
+
392
443
app := fiber .New ()
393
444
app .Use (New ())
394
445
app .Get ("/" , func (c * fiber.Ctx ) error {
@@ -425,6 +476,8 @@ func Test_Cache_WithHeadThenGet(t *testing.T) {
425
476
}
426
477
427
478
func Test_CustomCacheHeader (t * testing.T ) {
479
+ t .Parallel ()
480
+
428
481
app := fiber .New ()
429
482
430
483
app .Use (New (Config {
0 commit comments