@@ -21,7 +21,7 @@ test('should add session object to request', async (t) => {
2121test ( 'should destroy the session' , async ( t ) => {
2222 t . plan ( 3 )
2323 const port = await testServer ( ( request , reply ) => {
24- request . destroySession ( ( err ) => {
24+ request . session . destroy ( ( err ) => {
2525 t . falsy ( err )
2626 t . is ( request . session , null )
2727 reply . send ( 200 )
@@ -167,8 +167,13 @@ test('should generate new sessionId', async (t) => {
167167 fastify . register ( fastifySession , options )
168168 fastify . get ( '/' , ( request , reply ) => {
169169 oldSessionId = request . session . sessionId
170- request . session . regenerate ( )
171- reply . send ( 200 )
170+ request . session . regenerate ( error => {
171+ if ( error ) {
172+ reply . status ( 500 ) . send ( 'Error ' + error )
173+ } else {
174+ reply . send ( 200 )
175+ }
176+ } )
172177 } )
173178 fastify . get ( '/check' , ( request , reply ) => {
174179 t . not ( request . session . sessionId , oldSessionId )
@@ -271,6 +276,40 @@ test('should decryptSession with custom cookie options', async (t) => {
271276 } )
272277} )
273278
279+ test ( 'should bubble up errors with destroy call if session expired' , async ( t ) => {
280+ t . plan ( 2 )
281+ const fastify = Fastify ( )
282+ const store = {
283+ set ( id , data , cb ) { cb ( null ) } ,
284+ get ( id , cb ) {
285+ cb ( null , { expires : Date . now ( ) - 1000 , cookie : { expires : Date . now ( ) - 1000 } } )
286+ } ,
287+ destroy ( id , cb ) { cb ( new Error ( 'No can do' ) ) }
288+ }
289+
290+ const options = {
291+ secret : 'cNaoPYAwF60HZJzkcNaoPYAwF60HZJzk' ,
292+ store,
293+ cookie : { secure : false }
294+ }
295+
296+ fastify . register ( fastifyCookie )
297+ fastify . register ( fastifySession , options )
298+
299+ fastify . get ( '/' , ( request , reply ) => {
300+ reply . send ( 200 )
301+ } )
302+ await fastify . listen ( 0 )
303+ fastify . server . unref ( )
304+
305+ const { statusCode, body } = await request ( {
306+ url : 'http://localhost:' + fastify . server . address ( ) . port ,
307+ headers : { cookie : 'sessionId=_TuQsCBgxtHB3bu6wsRpTXfjqR5sK-q_.3mu5mErW+QI7w+Q0V2fZtrztSvqIpYgsnnC8LQf6ERY;' }
308+ } )
309+ t . is ( statusCode , 500 )
310+ t . is ( JSON . parse ( body ) . message , 'No can do' )
311+ } )
312+
274313test ( 'should not reset session cookie expiration if rolling is false' , async ( t ) => {
275314 t . plan ( 3 )
276315
@@ -361,8 +400,13 @@ test('should use custom sessionId generator if available (with request)', async
361400 } )
362401 fastify . get ( '/login' , ( request , reply ) => {
363402 request . session . returningVisitor = true
364- request . session . regenerate ( )
365- reply . status ( 200 ) . send ( 'OK ' + request . session . sessionId )
403+ request . session . regenerate ( error => {
404+ if ( error ) {
405+ reply . status ( 500 ) . send ( 'Error ' + error )
406+ } else {
407+ reply . status ( 200 ) . send ( 'OK ' + request . session . sessionId )
408+ }
409+ } )
366410 } )
367411 await fastify . listen ( 0 )
368412 fastify . server . unref ( )
@@ -417,8 +461,13 @@ test('should use custom sessionId generator if available (with request and rolli
417461 } )
418462 fastify . get ( '/login' , ( request , reply ) => {
419463 request . session . returningVisitor = true
420- request . session . regenerate ( )
421- reply . status ( 200 ) . send ( 'OK ' + request . session . sessionId )
464+ request . session . regenerate ( error => {
465+ if ( error ) {
466+ reply . status ( 500 ) . send ( 'Error ' + error )
467+ } else {
468+ reply . status ( 200 ) . send ( 'OK ' + request . session . sessionId )
469+ }
470+ } )
422471 } )
423472 await fastify . listen ( 0 )
424473 fastify . server . unref ( )
@@ -444,3 +493,50 @@ test('should use custom sessionId generator if available (with request and rolli
444493 t . is ( response3 . statusCode , 200 )
445494 t . true ( sessionBody3 . startsWith ( 'returningVisitor-' ) )
446495} )
496+
497+ test ( 'should reload the session' , async ( t ) => {
498+ t . plan ( 4 )
499+ const port = await testServer ( ( request , reply ) => {
500+ request . session . someData = 'some-data'
501+ t . is ( request . session . someData , 'some-data' )
502+
503+ request . session . reload ( ( err ) => {
504+ t . falsy ( err )
505+
506+ t . is ( request . session . someData , undefined )
507+
508+ reply . send ( 200 )
509+ } )
510+ } , DEFAULT_OPTIONS )
511+
512+ const { response } = await request ( `http://localhost:${ port } ` )
513+
514+ t . is ( response . statusCode , 200 )
515+ } )
516+
517+ test ( 'should save the session' , async ( t ) => {
518+ t . plan ( 6 )
519+ const port = await testServer ( ( request , reply ) => {
520+ request . session . someData = 'some-data'
521+ t . is ( request . session . someData , 'some-data' )
522+
523+ request . session . save ( ( err ) => {
524+ t . falsy ( err )
525+
526+ t . is ( request . session . someData , 'some-data' )
527+
528+ // unlike previous test, here the session data remains after a save
529+ request . session . reload ( ( err ) => {
530+ t . falsy ( err )
531+
532+ t . is ( request . session . someData , 'some-data' )
533+
534+ reply . send ( 200 )
535+ } )
536+ } )
537+ } , DEFAULT_OPTIONS )
538+
539+ const { response } = await request ( `http://localhost:${ port } ` )
540+
541+ t . is ( response . statusCode , 200 )
542+ } )
0 commit comments