From 595da3016466ba7666a0398c69af7e815a960e63 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Wed, 24 Aug 2022 16:29:25 +0200 Subject: [PATCH 1/3] add unit test for maxAge --- test/session.test.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/session.test.js b/test/session.test.js index 6d36c85..1161be9 100644 --- a/test/session.test.js +++ b/test/session.test.js @@ -797,3 +797,38 @@ test('only save session when it changes', async t => { // no set-cookie t.equal(setCookieHeader2, undefined) }) + +test('will not update expires property of the session using Session#touch() if maxAge is not set', async (t) => { + t.plan(4) + + const fastify = Fastify() + fastify.register(fastifyCookie) + fastify.register(fastifySession, { + secret: DEFAULT_SECRET, + rolling: false, + cookie: { secure: false } + }) + fastify.addHook('onRequest', (request, reply, done) => { + request.session.touch() + reply.send(request.session.cookie.expires) + done() + }) + + fastify.get('/', (request, reply) => reply.send(200)) + await fastify.listen({ port: 0 }) + t.teardown(() => { fastify.close() }) + + const response1 = await fastify.inject({ + url: '/' + }) + t.equal(response1.statusCode, 200) + await new Promise(resolve => setTimeout(resolve, 1)) + t.equal(response1.body, 'null') + + const response2 = await fastify.inject({ + url: '/', + headers: { Cookie: response1.headers['set-cookie'] } + }) + t.equal(response2.statusCode, 200) + t.equal(response2.body, 'null') +}) From c7b54bcbbe5726d917fb328febc0926d85c75e51 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Wed, 24 Aug 2022 19:13:42 +0200 Subject: [PATCH 2/3] improve test --- test/session.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/session.test.js b/test/session.test.js index 1161be9..f65f53a 100644 --- a/test/session.test.js +++ b/test/session.test.js @@ -2,6 +2,7 @@ const test = require('tap').test const Fastify = require('fastify') +const fastifyPlugin = require('fastify-plugin') const fastifyCookie = require('@fastify/cookie') const fastifySession = require('..') const { buildFastify, DEFAULT_OPTIONS, DEFAULT_COOKIE, DEFAULT_SESSION_ID, DEFAULT_SECRET, DEFAULT_COOKIE_VALUE } = require('./util') @@ -810,11 +811,10 @@ test('will not update expires property of the session using Session#touch() if m }) fastify.addHook('onRequest', (request, reply, done) => { request.session.touch() - reply.send(request.session.cookie.expires) done() }) - fastify.get('/', (request, reply) => reply.send(200)) + fastify.get('/', (request, reply) => reply.send({ expires: request.session.cookie.expires })) await fastify.listen({ port: 0 }) t.teardown(() => { fastify.close() }) @@ -823,12 +823,12 @@ test('will not update expires property of the session using Session#touch() if m }) t.equal(response1.statusCode, 200) await new Promise(resolve => setTimeout(resolve, 1)) - t.equal(response1.body, 'null') + t.same(response1.json(), { expires: null }) const response2 = await fastify.inject({ url: '/', headers: { Cookie: response1.headers['set-cookie'] } }) t.equal(response2.statusCode, 200) - t.equal(response2.body, 'null') + t.same(response2.json(), { expires: null }) }) From 01c56bd1a475123db331ddb0cb63b9a009a8fdee Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Wed, 24 Aug 2022 19:30:25 +0200 Subject: [PATCH 3/3] Update test/session.test.js --- test/session.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/session.test.js b/test/session.test.js index f65f53a..9885c03 100644 --- a/test/session.test.js +++ b/test/session.test.js @@ -2,7 +2,6 @@ const test = require('tap').test const Fastify = require('fastify') -const fastifyPlugin = require('fastify-plugin') const fastifyCookie = require('@fastify/cookie') const fastifySession = require('..') const { buildFastify, DEFAULT_OPTIONS, DEFAULT_COOKIE, DEFAULT_SESSION_ID, DEFAULT_SECRET, DEFAULT_COOKIE_VALUE } = require('./util')