From 34c822a3b8cdb98f00d94e487966757e82ba08e5 Mon Sep 17 00:00:00 2001 From: KaKa Date: Fri, 25 Mar 2022 21:04:19 +0800 Subject: [PATCH 1/2] Revert "major: drop node 10 and ava update (#65)" (#76) * Revert "major: drop node 10 and ava update (#65)" This reverts commit da33d5dc8ce8f722092db9fd85c33724e73f0b77. * Revert "feat: replace got by undici (#67)" This reverts commit a860eaab6f5504b31036b7088db79fd7da5e0979. --- .github/workflows/ci.yml | 2 +- package.json | 6 +++--- test/secret.test.js | 43 ++++++++++++++++++++++++++++------------ test/session.test.js | 18 +++++++++-------- test/store.test.js | 2 +- test/util.js | 15 +++++--------- 6 files changed, 50 insertions(+), 36 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9c5618f..11c8620 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,7 +16,7 @@ jobs: strategy: matrix: - node-version: [12, 14, 16] + node-version: [10, 12, 14, 16] os: [macos-latest, ubuntu-latest, windows-latest] steps: diff --git a/package.json b/package.json index 8a69f13..f4702f1 100644 --- a/package.json +++ b/package.json @@ -31,14 +31,14 @@ }, "devDependencies": { "@types/node": "^17.0.0", - "ava": "^4.1.0", + "ava": "^3.6.0", "fastify": "^3.0.0", "fastify-cookie": "^5.3.1", + "got": "^11.6.0", "nyc": "^15.0.0", "standard": "^16.0.1", "tsd": "^0.19.0", - "typescript": "^4.0.2", - "undici": "^4.16.0" + "typescript": "^4.0.2" }, "types": "types/types.d.ts", "files": [ diff --git a/test/secret.test.js b/test/secret.test.js index 3fff7e0..7a1ce03 100644 --- a/test/secret.test.js +++ b/test/secret.test.js @@ -5,63 +5,80 @@ const Fastify = require('fastify') const fastifyCookie = require('fastify-cookie') const fastifySession = require('..') -test('register should fail if no secret is specified', async t => { +test.cb('register should fail if no secret is specified', t => { t.plan(1) const fastify = Fastify() const options = {} fastify.register(fastifyCookie) fastify.register(fastifySession, options) - - await t.throwsAsync(fastify.ready, { instanceOf: Error, message: 'the secret option is required!' }) + fastify.ready((err) => { + t.true(err instanceof Error) + t.end() + }) }) -test('register should succeed if valid secret is specified', async t => { +test.cb('register should succeed if valid secret is specified', t => { t.plan(1) const fastify = Fastify() const options = { secret: 'cNaoPYAwF60HZJzkcNaoPYAwF60HZJzk' } fastify.register(fastifyCookie) fastify.register(fastifySession, options) - t.truthy(await fastify.ready()) + fastify.ready((err) => { + t.falsy(err) + t.end() + }) }) -test('register should fail if the secret is too short', async t => { +test.cb('register should fail if the secret is too short', t => { t.plan(1) const fastify = Fastify() const options = { secret: 'geheim' } fastify.register(fastifyCookie) fastify.register(fastifySession, options) - await t.throwsAsync(fastify.ready, { instanceOf: Error, message: 'the secret must have length 32 or greater' }) + fastify.ready((err) => { + t.true(err instanceof Error) + t.end() + }) }) -test('register should succeed if secret is short, but in an array', async t => { +test.cb('register should succeed if secret is short, but in an array', t => { t.plan(1) const fastify = Fastify() const options = { secret: ['geheim'] } fastify.register(fastifyCookie) fastify.register(fastifySession, options) - t.truthy(await fastify.ready()) + fastify.ready((err) => { + t.falsy(err) + t.end() + }) }) -test('register should succeed if multiple secrets are present', async t => { +test.cb('register should succeed if multiple secrets are present', t => { t.plan(1) const fastify = Fastify() const options = { secret: ['geheim', 'test'] } fastify.register(fastifyCookie) fastify.register(fastifySession, options) - t.truthy(await fastify.ready()) + fastify.ready((err) => { + t.falsy(err) + t.end() + }) }) -test('register should fail if no secret is present in array', async t => { +test.cb('register should fail if no secret is present in array', t => { t.plan(1) const fastify = Fastify() const options = { secret: [] } fastify.register(fastifyCookie) fastify.register(fastifySession, options) - await t.throwsAsync(fastify.ready, { instanceOf: Error, message: 'at least one secret is required' }) + fastify.ready((err) => { + t.true(err instanceof Error) + t.end() + }) }) diff --git a/test/session.test.js b/test/session.test.js index 266f080..1872a36 100644 --- a/test/session.test.js +++ b/test/session.test.js @@ -191,16 +191,18 @@ test('should generate new sessionId', async (t) => { t.is(response2.statusCode, 200) }) -test('should decorate the server with decryptSession', async t => { +test.cb('should decorate the server with decryptSession', t => { t.plan(2) const fastify = Fastify() const options = { secret: 'cNaoPYAwF60HZJzkcNaoPYAwF60HZJzk' } fastify.register(fastifyCookie) fastify.register(fastifySession, options) - - t.truthy(await fastify.ready()) - t.truthy(fastify.decryptSession) + fastify.ready((err) => { + t.falsy(err) + t.truthy(fastify.decryptSession) + t.end() + }) }) test('should decryptSession with custom request object', async (t) => { @@ -371,7 +373,7 @@ test('should use custom sessionId generator if available (with request)', async url: 'http://localhost:' + fastify.server.address().port }) t.is(response1.statusCode, 200) - t.true(response1.headers['set-cookie'] !== undefined) + t.true(response1.headers['set-cookie'] != null) t.true(sessionBody1.startsWith('custom-')) const { response: response2 } = await request({ @@ -379,7 +381,7 @@ test('should use custom sessionId generator if available (with request)', async headers: { Cookie: response1.headers['set-cookie'] } }) t.is(response2.statusCode, 200) - t.true(response2.headers['set-cookie'] !== undefined) + t.true(response2.headers['set-cookie'] != null) const { response: response3, body: sessionBody3 } = await request({ url: 'http://localhost:' + fastify.server.address().port, @@ -427,7 +429,7 @@ test('should use custom sessionId generator if available (with request and rolli url: 'http://localhost:' + fastify.server.address().port }) t.is(response1.statusCode, 200) - t.true(response1.headers['set-cookie'] !== undefined) + t.true(response1.headers['set-cookie'] != null) t.true(sessionBody1.startsWith('custom-')) const { response: response2 } = await request({ @@ -435,7 +437,7 @@ test('should use custom sessionId generator if available (with request and rolli headers: { Cookie: response1.headers['set-cookie'] } }) t.is(response2.statusCode, 200) - t.true(response2.headers['set-cookie'] !== undefined) + t.true(response2.headers['set-cookie'] != null) const { response: response3, body: sessionBody3 } = await request({ url: 'http://localhost:' + fastify.server.address().port, diff --git a/test/store.test.js b/test/store.test.js index cc73e24..b347129 100644 --- a/test/store.test.js +++ b/test/store.test.js @@ -103,7 +103,7 @@ test('should set new session cookie if expired', async (t) => { }) t.is(statusCode, 500) - t.is(cookie, undefined) + t.is(cookie, null) }) test('store should be an event emitter', t => { diff --git a/test/util.js b/test/util.js index 31969b8..98a0d9e 100644 --- a/test/util.js +++ b/test/util.js @@ -1,7 +1,7 @@ 'use strict' const Fastify = require('fastify') -const undici = require('undici') +const got = require('got') const fastifyCookie = require('fastify-cookie') const fastifySession = require('../lib/fastifySession') @@ -23,19 +23,14 @@ async function testServer (handler, sessionOptions, plugin) { async function request (options) { let response - let body try { - if (typeof options === 'string') { - response = await undici.request(options) - } else { - response = await undici.request(options.url, options) - } - body = await response.body.text() + response = await got(options) } catch (err) { response = err.response } - const { statusCode } = response - const cookie = response.headers['set-cookie'] + const { statusCode, body } = response + const cookieHeader = response.headers['set-cookie'] + const cookie = cookieHeader ? cookieHeader[0] : null return { response, body, statusCode, cookie } } From 765e3c7023b044977094e4b1f7fef8691b1f1e9a Mon Sep 17 00:00:00 2001 From: KaKa Date: Fri, 25 Mar 2022 21:08:06 +0800 Subject: [PATCH 2/2] Bumped v6.5.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f4702f1..d075c36 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fastify/session", - "version": "6.5.1", + "version": "6.5.2", "description": "a session plugin for fastify", "main": "lib/fastifySession.js", "scripts": {