@@ -25,13 +25,23 @@ func Test_Session(t *testing.T) {
25
25
ctx := app .AcquireCtx (& fasthttp.RequestCtx {})
26
26
defer app .ReleaseCtx (ctx )
27
27
28
+ // Get a new session
29
+ sess , err := store .Get (ctx )
30
+ utils .AssertEqual (t , nil , err )
31
+ utils .AssertEqual (t , true , sess .Fresh ())
32
+ token := sess .ID ()
33
+ sess .Save ()
34
+
35
+ app .ReleaseCtx (ctx )
36
+ ctx = app .AcquireCtx (& fasthttp.RequestCtx {})
37
+
28
38
// set session
29
- ctx .Request ().Header .SetCookie (store .sessionName , "123" )
39
+ ctx .Request ().Header .SetCookie (store .sessionName , token )
30
40
31
41
// get session
32
- sess , err : = store .Get (ctx )
42
+ sess , err = store .Get (ctx )
33
43
utils .AssertEqual (t , nil , err )
34
- utils .AssertEqual (t , true , sess .Fresh ())
44
+ utils .AssertEqual (t , false , sess .Fresh ())
35
45
36
46
// get keys
37
47
keys := sess .Keys ()
@@ -64,12 +74,14 @@ func Test_Session(t *testing.T) {
64
74
65
75
// get id
66
76
id := sess .ID ()
67
- utils .AssertEqual (t , "123" , id )
77
+ utils .AssertEqual (t , token , id )
68
78
69
79
// save the old session first
70
80
err = sess .Save ()
71
81
utils .AssertEqual (t , nil , err )
72
82
83
+ app .ReleaseCtx (ctx )
84
+
73
85
// requesting entirely new context to prevent falsy tests
74
86
ctx = app .AcquireCtx (& fasthttp.RequestCtx {})
75
87
defer app .ReleaseCtx (ctx )
@@ -108,7 +120,6 @@ func Test_Session_Types(t *testing.T) {
108
120
109
121
// fiber context
110
122
ctx := app .AcquireCtx (& fasthttp.RequestCtx {})
111
- defer app .ReleaseCtx (ctx )
112
123
113
124
// set cookie
114
125
ctx .Request ().Header .SetCookie (store .sessionName , "123" )
@@ -120,6 +131,10 @@ func Test_Session_Types(t *testing.T) {
120
131
121
132
// the session string is no longer be 123
122
133
newSessionIDString := sess .ID ()
134
+
135
+ app .ReleaseCtx (ctx )
136
+ ctx = app .AcquireCtx (& fasthttp.RequestCtx {})
137
+
123
138
ctx .Request ().Header .SetCookie (store .sessionName , newSessionIDString )
124
139
125
140
type User struct {
@@ -177,6 +192,11 @@ func Test_Session_Types(t *testing.T) {
177
192
err = sess .Save ()
178
193
utils .AssertEqual (t , nil , err )
179
194
195
+ app .ReleaseCtx (ctx )
196
+ ctx = app .AcquireCtx (& fasthttp.RequestCtx {})
197
+
198
+ ctx .Request ().Header .SetCookie (store .sessionName , newSessionIDString )
199
+
180
200
// get session
181
201
sess , err = store .Get (ctx )
182
202
utils .AssertEqual (t , nil , err )
@@ -203,6 +223,7 @@ func Test_Session_Types(t *testing.T) {
203
223
utils .AssertEqual (t , vfloat64 , sess .Get ("vfloat64" ).(float64 ))
204
224
utils .AssertEqual (t , vcomplex64 , sess .Get ("vcomplex64" ).(complex64 ))
205
225
utils .AssertEqual (t , vcomplex128 , sess .Get ("vcomplex128" ).(complex128 ))
226
+ app .ReleaseCtx (ctx )
206
227
}
207
228
208
229
// go test -run Test_Session_Store_Reset
@@ -214,7 +235,6 @@ func Test_Session_Store_Reset(t *testing.T) {
214
235
app := fiber .New ()
215
236
// fiber context
216
237
ctx := app .AcquireCtx (& fasthttp.RequestCtx {})
217
- defer app .ReleaseCtx (ctx )
218
238
219
239
// get session
220
240
sess , err := store .Get (ctx )
@@ -228,6 +248,12 @@ func Test_Session_Store_Reset(t *testing.T) {
228
248
229
249
// reset store
230
250
utils .AssertEqual (t , nil , store .Reset ())
251
+ id := sess .ID ()
252
+
253
+ app .ReleaseCtx (ctx )
254
+ ctx = app .AcquireCtx (& fasthttp.RequestCtx {})
255
+ defer app .ReleaseCtx (ctx )
256
+ ctx .Request ().Header .SetCookie (store .sessionName , id )
231
257
232
258
// make sure the session is recreated
233
259
sess , err = store .Get (ctx )
@@ -302,25 +328,37 @@ func Test_Session_Save_Expiration(t *testing.T) {
302
328
// set value
303
329
sess .Set ("name" , "john" )
304
330
331
+ token := sess .ID ()
332
+
305
333
// expire this session in 5 seconds
306
334
sess .SetExpiry (sessionDuration )
307
335
308
336
// save session
309
337
err = sess .Save ()
310
338
utils .AssertEqual (t , nil , err )
311
339
340
+ app .ReleaseCtx (ctx )
341
+ ctx = app .AcquireCtx (& fasthttp.RequestCtx {})
342
+
312
343
// here you need to get the old session yet
344
+ ctx .Request ().Header .SetCookie (store .sessionName , token )
313
345
sess , err = store .Get (ctx )
314
346
utils .AssertEqual (t , nil , err )
315
347
utils .AssertEqual (t , "john" , sess .Get ("name" ))
316
348
317
349
// just to make sure the session has been expired
318
350
time .Sleep (sessionDuration + (10 * time .Millisecond ))
319
351
352
+ app .ReleaseCtx (ctx )
353
+ ctx = app .AcquireCtx (& fasthttp.RequestCtx {})
354
+ defer app .ReleaseCtx (ctx )
355
+
320
356
// here you should get a new session
357
+ ctx .Request ().Header .SetCookie (store .sessionName , token )
321
358
sess , err = store .Get (ctx )
322
359
utils .AssertEqual (t , nil , err )
323
360
utils .AssertEqual (t , nil , sess .Get ("name" ))
361
+ utils .AssertEqual (t , true , sess .ID () != token )
324
362
})
325
363
}
326
364
@@ -364,7 +402,15 @@ func Test_Session_Destroy(t *testing.T) {
364
402
365
403
// set value & save
366
404
sess .Set ("name" , "fenny" )
405
+ id := sess .ID ()
367
406
utils .AssertEqual (t , nil , sess .Save ())
407
+
408
+ app .ReleaseCtx (ctx )
409
+ ctx = app .AcquireCtx (& fasthttp.RequestCtx {})
410
+ defer app .ReleaseCtx (ctx )
411
+
412
+ // get session
413
+ ctx .Request ().Header .Set (store .sessionName , id )
368
414
sess , err = store .Get (ctx )
369
415
utils .AssertEqual (t , nil , err )
370
416
@@ -408,7 +454,8 @@ func Test_Session_Cookie(t *testing.T) {
408
454
}
409
455
410
456
// go test -run Test_Session_Cookie_In_Response
411
- func Test_Session_Cookie_In_Response (t * testing.T ) {
457
+ // Regression: https://github.com/gofiber/fiber/pull/1191
458
+ func Test_Session_Cookie_In_Middleware_Chain (t * testing.T ) {
412
459
t .Parallel ()
413
460
store := New ()
414
461
app := fiber .New ()
@@ -421,15 +468,17 @@ func Test_Session_Cookie_In_Response(t *testing.T) {
421
468
sess , err := store .Get (ctx )
422
469
utils .AssertEqual (t , nil , err )
423
470
sess .Set ("id" , "1" )
471
+ id := sess .ID ()
424
472
utils .AssertEqual (t , true , sess .Fresh ())
425
473
utils .AssertEqual (t , nil , sess .Save ())
426
474
427
475
sess , err = store .Get (ctx )
428
476
utils .AssertEqual (t , nil , err )
429
477
sess .Set ("name" , "john" )
430
478
utils .AssertEqual (t , true , sess .Fresh ())
479
+ utils .AssertEqual (t , id , sess .ID ()) // session id should be the same
431
480
432
- utils .AssertEqual (t , "1" , sess .Get ( "id" ) )
481
+ utils .AssertEqual (t , sess .ID () != "1" , true )
433
482
utils .AssertEqual (t , "john" , sess .Get ("name" ))
434
483
}
435
484
@@ -441,24 +490,31 @@ func Test_Session_Deletes_Single_Key(t *testing.T) {
441
490
app := fiber .New ()
442
491
443
492
ctx := app .AcquireCtx (& fasthttp.RequestCtx {})
444
- defer app .ReleaseCtx (ctx )
445
493
446
494
sess , err := store .Get (ctx )
447
495
utils .AssertEqual (t , nil , err )
448
- ctx .Request ().Header .SetCookie (store .sessionName , sess .ID ())
449
-
496
+ id := sess .ID ()
450
497
sess .Set ("id" , "1" )
451
498
utils .AssertEqual (t , nil , sess .Save ())
452
499
500
+ app .ReleaseCtx (ctx )
501
+ ctx = app .AcquireCtx (& fasthttp.RequestCtx {})
502
+ ctx .Request ().Header .SetCookie (store .sessionName , id )
503
+
453
504
sess , err = store .Get (ctx )
454
505
utils .AssertEqual (t , nil , err )
455
506
sess .Delete ("id" )
456
507
utils .AssertEqual (t , nil , sess .Save ())
457
508
509
+ app .ReleaseCtx (ctx )
510
+ ctx = app .AcquireCtx (& fasthttp.RequestCtx {})
511
+ ctx .Request ().Header .SetCookie (store .sessionName , id )
512
+
458
513
sess , err = store .Get (ctx )
459
514
utils .AssertEqual (t , nil , err )
460
515
utils .AssertEqual (t , false , sess .Fresh ())
461
516
utils .AssertEqual (t , nil , sess .Get ("id" ))
517
+ app .ReleaseCtx (ctx )
462
518
}
463
519
464
520
// go test -run Test_Session_Reset
@@ -475,6 +531,9 @@ func Test_Session_Reset(t *testing.T) {
475
531
defer app .ReleaseCtx (ctx )
476
532
477
533
t .Run ("reset session data and id, and set fresh to be true" , func (t * testing.T ) {
534
+ t .Parallel ()
535
+ // fiber context
536
+ ctx := app .AcquireCtx (& fasthttp.RequestCtx {})
478
537
// a random session uuid
479
538
originalSessionUUIDString := ""
480
539
@@ -491,6 +550,9 @@ func Test_Session_Reset(t *testing.T) {
491
550
err = freshSession .Save ()
492
551
utils .AssertEqual (t , nil , err )
493
552
553
+ app .ReleaseCtx (ctx )
554
+ ctx = app .AcquireCtx (& fasthttp.RequestCtx {})
555
+
494
556
// set cookie
495
557
ctx .Request ().Header .SetCookie (store .sessionName , originalSessionUUIDString )
496
558
@@ -524,6 +586,8 @@ func Test_Session_Reset(t *testing.T) {
524
586
// Check that the session id is not in the header or cookie anymore
525
587
utils .AssertEqual (t , "" , string (ctx .Response ().Header .Peek (store .sessionName )))
526
588
utils .AssertEqual (t , "" , string (ctx .Request ().Header .Peek (store .sessionName )))
589
+
590
+ app .ReleaseCtx (ctx )
527
591
})
528
592
}
529
593
@@ -551,6 +615,12 @@ func Test_Session_Regenerate(t *testing.T) {
551
615
err = freshSession .Save ()
552
616
utils .AssertEqual (t , nil , err )
553
617
618
+ // release the context
619
+ app .ReleaseCtx (ctx )
620
+
621
+ // acquire a new context
622
+ ctx = app .AcquireCtx (& fasthttp.RequestCtx {})
623
+
554
624
// set cookie
555
625
ctx .Request ().Header .SetCookie (store .sessionName , originalSessionUUIDString )
556
626
@@ -566,6 +636,9 @@ func Test_Session_Regenerate(t *testing.T) {
566
636
567
637
// acquiredSession.fresh should be true after regenerating
568
638
utils .AssertEqual (t , true , acquiredSession .Fresh ())
639
+
640
+ // release the context
641
+ app .ReleaseCtx (ctx )
569
642
})
570
643
}
571
644
0 commit comments