@@ -115,3 +115,91 @@ test('Anti re-use with set expiry of 15 minutes', async t => {
115
115
t . assert . ok ( getResponse )
116
116
t . assert . deepStrictEqual ( JSON . parse ( getResponse . payload ) , { } )
117
117
} )
118
+
119
+ test ( 'Anti re-use should still allow touch() to work' , async t => {
120
+ const fastify = Fastify ( { logger : false } )
121
+ const clock = FakeTimers . install ( {
122
+ shouldAdvanceTime : true ,
123
+ now : Date . now ( )
124
+ } )
125
+ t . after ( ( ) => {
126
+ fastify . close ( )
127
+ clock . reset ( )
128
+ clock . uninstall ( )
129
+ } )
130
+
131
+ fastify . register ( require ( '../' ) , {
132
+ key,
133
+ expiry : 15 * 60 // 15 minutes
134
+ } )
135
+
136
+ fastify . post ( '/' , ( request , reply ) => {
137
+ request . session . set ( 'some' , request . body . some )
138
+ request . session . set ( 'some2' , request . body . some2 )
139
+ request . session . touch ( )
140
+ reply . send ( 'hello world' )
141
+ } )
142
+
143
+ fastify . get ( '/' , ( request , reply ) => {
144
+ const some = request . session . get ( 'some' )
145
+ const some2 = request . session . get ( 'some2' )
146
+ reply . send ( { some, some2 } )
147
+ } )
148
+
149
+ const payload = {
150
+ some : 'someData' ,
151
+ some2 : { a : 1 , b : 2 , c : 3 }
152
+ }
153
+
154
+ const firstPostResponse = await fastify . inject ( {
155
+ method : 'POST' ,
156
+ url : '/' ,
157
+ payload
158
+ } )
159
+ const oldCookie = firstPostResponse . headers [ 'set-cookie' ]
160
+
161
+ t . assert . ok ( firstPostResponse )
162
+ t . assert . strictEqual ( firstPostResponse . statusCode , 200 )
163
+ t . assert . ok ( firstPostResponse . headers [ 'set-cookie' ] )
164
+
165
+ clock . jump ( '00:14:59' ) // forward just before expiry
166
+
167
+ const secondPostResponse = await fastify . inject ( {
168
+ method : 'POST' ,
169
+ url : '/' ,
170
+ payload
171
+ } )
172
+ const newCookie = secondPostResponse . headers [ 'set-cookie' ]
173
+
174
+ t . assert . ok ( secondPostResponse )
175
+ t . assert . strictEqual ( secondPostResponse . statusCode , 200 )
176
+ t . assert . ok ( secondPostResponse . headers [ 'set-cookie' ] )
177
+
178
+ clock . jump ( '00:00:02' ) // forward just after expiry
179
+
180
+ const withNewCookie = await fastify . inject ( {
181
+ method : 'GET' ,
182
+ url : '/' ,
183
+ headers : {
184
+ cookie : newCookie
185
+ }
186
+ } )
187
+
188
+ t . assert . ok ( withNewCookie )
189
+
190
+ // this should return the payload because the cookie was updated 2 seconds before
191
+ t . assert . deepStrictEqual ( JSON . parse ( withNewCookie . payload ) , payload )
192
+
193
+ const withOldCookie = await fastify . inject ( {
194
+ method : 'GET' ,
195
+ url : '/' ,
196
+ headers : {
197
+ cookie : oldCookie
198
+ }
199
+ } )
200
+
201
+ t . assert . ok ( withOldCookie )
202
+
203
+ // this should be empty because the old session is expired
204
+ t . assert . deepStrictEqual ( JSON . parse ( withOldCookie . payload ) , { } )
205
+ } )
0 commit comments