diff --git a/.taprc b/.taprc deleted file mode 100644 index eb6eb3e..0000000 --- a/.taprc +++ /dev/null @@ -1,2 +0,0 @@ -files: - - test/**/*.test.js diff --git a/package.json b/package.json index b07c8fd..f0bf175 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "lint:fix": "eslint --fix", "test": "npm run test:unit && npm run test:typescript", "test:typescript": "tsd", - "test:unit": "tap", + "test:unit": "c8 --100 node --test", "test:unit:verbose": "npm run test:unit -- -Rspec" }, "precommit": [ @@ -42,10 +42,10 @@ "@fastify/pre-commit": "^2.1.0", "@types/node": "^22.0.0", "benchmark": "^2.1.4", + "c8": "^10.1.2", "fastify": "^5.0.0", "neostandard": "^0.11.9", "sinon": "^19.0.2", - "tap": "^18.6.1", "tsd": "^0.31.1" }, "dependencies": { diff --git a/test/cookie.test.js b/test/cookie.test.js index 77e1c22..53a05e9 100644 --- a/test/cookie.test.js +++ b/test/cookie.test.js @@ -1,13 +1,14 @@ 'use strict' -const { test } = require('tap') +const { describe, test } = require('node:test') const Fastify = require('fastify') const sinon = require('sinon') const { sign, unsign } = require('../signer') const plugin = require('../') -test('cookies get set correctly', (t) => { - t.plan(7) +test('cookies get set correctly', async (t) => { + t.plan(6) + const fastify = Fastify() fastify.register(plugin) @@ -17,24 +18,24 @@ test('cookies get set correctly', (t) => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) - - const cookies = res.cookies - t.equal(cookies.length, 1) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, 'foo') - t.equal(cookies[0].path, '/') }) + + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) + + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 1) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, 'foo') + t.assert.strictEqual(cookies[0].path, '/') }) -test('express cookie compatibility', (t) => { - t.plan(7) +test('express cookie compatibility', async (t) => { + t.plan(6) + const fastify = Fastify() fastify.register(plugin) @@ -44,24 +45,23 @@ test('express cookie compatibility', (t) => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/espresso' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) - - const cookies = res.cookies - t.equal(cookies.length, 1) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, 'foo') - t.equal(cookies[0].path, '/') }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) + + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 1) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, 'foo') + t.assert.strictEqual(cookies[0].path, '/') }) -test('should set multiple cookies', (t) => { - t.plan(10) +test('should set multiple cookies', async (t) => { + t.plan(9) + const fastify = Fastify() fastify.register(plugin) @@ -73,27 +73,26 @@ test('should set multiple cookies', (t) => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) - - const cookies = res.cookies - t.equal(cookies.length, 3) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, 'foo') - t.equal(cookies[1].name, 'bar') - t.equal(cookies[1].value, 'test') - t.equal(cookies[2].name, 'wee') - t.equal(cookies[2].value, 'woo') }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) + + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 3) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, 'foo') + t.assert.strictEqual(cookies[1].name, 'bar') + t.assert.strictEqual(cookies[1].value, 'test') + t.assert.strictEqual(cookies[2].name, 'wee') + t.assert.strictEqual(cookies[2].value, 'woo') }) -test('should set multiple cookies', (t) => { - t.plan(12) +test('should set multiple cookies', async (t) => { + t.plan(11) + const fastify = Fastify() fastify.register(plugin) @@ -110,30 +109,29 @@ test('should set multiple cookies', (t) => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) - - const cookies = res.cookies - t.equal(cookies.length, 3) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, 'foo') - t.equal(cookies[1].name, 'bar') - t.equal(cookies[1].value, 'test') - t.equal(cookies[2].name, 'wee') - t.equal(cookies[2].value, 'woo') - - t.equal(res.headers['set-cookie'][1], 'bar=test; Partitioned; SameSite=Lax') - t.equal(res.headers['set-cookie'][2], 'wee=woo; Secure; Partitioned; SameSite=Lax') }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) + + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 3) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, 'foo') + t.assert.strictEqual(cookies[1].name, 'bar') + t.assert.strictEqual(cookies[1].value, 'test') + t.assert.strictEqual(cookies[2].name, 'wee') + t.assert.strictEqual(cookies[2].value, 'woo') + + t.assert.strictEqual(res.headers['set-cookie'][1], 'bar=test; Partitioned; SameSite=Lax') + t.assert.strictEqual(res.headers['set-cookie'][2], 'wee=woo; Secure; Partitioned; SameSite=Lax') }) -test('should set multiple cookies (an array already exists)', (t) => { - t.plan(10) +test('should set multiple cookies (an array already exists)', async (t) => { + t.plan(9) + const fastify = Fastify() fastify.register(plugin) @@ -145,28 +143,27 @@ test('should set multiple cookies (an array already exists)', (t) => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) - - const cookies = res.cookies - t.equal(cookies.length, 3) - t.equal(cookies[0].name, 'bar') - t.equal(cookies[0].value, 'bar') - t.equal(cookies[0].path, undefined) - - t.equal(cookies[1].name, 'foo') - t.equal(cookies[1].value, 'foo') - t.equal(cookies[2].path, '/path') }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) + + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 3) + t.assert.strictEqual(cookies[0].name, 'bar') + t.assert.strictEqual(cookies[0].value, 'bar') + t.assert.strictEqual(cookies[0].path, undefined) + + t.assert.strictEqual(cookies[1].name, 'foo') + t.assert.strictEqual(cookies[1].value, 'foo') + t.assert.strictEqual(cookies[2].path, '/path') }) -test('cookies get set correctly with millisecond dates', (t) => { - t.plan(8) +test('cookies get set correctly with millisecond dates', async (t) => { + t.plan(7) + const fastify = Fastify() fastify.register(plugin) @@ -176,26 +173,25 @@ test('cookies get set correctly with millisecond dates', (t) => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) - - const cookies = res.cookies - t.equal(cookies.length, 1) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, 'foo') - t.equal(cookies[0].path, '/') - const expires = new Date(cookies[0].expires) - t.ok(expires < new Date(Date.now() + 5000)) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) + + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 1) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, 'foo') + t.assert.strictEqual(cookies[0].path, '/') + const expires = new Date(cookies[0].expires) + t.assert.ok(expires < new Date(Date.now() + 5000)) }) -test('share options for setCookie and clearCookie', (t) => { - t.plan(8) +test('share options for setCookie and clearCookie', async (t) => { + t.plan(7) + const fastify = Fastify() const secret = 'testsecret' fastify.register(plugin, { secret }) @@ -212,26 +208,25 @@ test('share options for setCookie and clearCookie', (t) => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) + }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) - const cookies = res.cookies - t.equal(cookies.length, 1) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, '') - t.equal(cookies[0].maxAge, 0) + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 1) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, '') + t.assert.strictEqual(cookies[0].maxAge, 0) - t.ok(new Date(cookies[0].expires) < new Date()) - }) + t.assert.ok(new Date(cookies[0].expires) < new Date()) }) -test('expires should not be overridden in clearCookie', (t) => { - t.plan(7) +test('expires should not be overridden in clearCookie', async (t) => { + t.plan(6) + const fastify = Fastify() const secret = 'testsecret' fastify.register(plugin, { secret }) @@ -248,122 +243,120 @@ test('expires should not be overridden in clearCookie', (t) => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) - - const cookies = res.cookies - t.equal(cookies.length, 1) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, '') - const expires = new Date(cookies[0].expires) - t.ok(expires < new Date(Date.now() + 5000)) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) + + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 1) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, '') + const expires = new Date(cookies[0].expires) + t.assert.ok(expires < new Date(Date.now() + 5000)) }) -test('parses incoming cookies', (t) => { - t.plan(15) +test('parses incoming cookies', async (t) => { + t.plan(14) + const fastify = Fastify() fastify.register(plugin) // check that it parses the cookies in the onRequest hook for (const hook of ['preValidation', 'preHandler']) { fastify.addHook(hook, (req, reply, done) => { - t.ok(req.cookies) - t.ok(req.cookies.bar) - t.equal(req.cookies.bar, 'bar') + t.assert.ok(req.cookies) + t.assert.ok(req.cookies.bar) + t.assert.strictEqual(req.cookies.bar, 'bar') done() }) } fastify.addHook('preParsing', (req, reply, payload, done) => { - t.ok(req.cookies) - t.ok(req.cookies.bar) - t.equal(req.cookies.bar, 'bar') + t.assert.ok(req.cookies) + t.assert.ok(req.cookies.bar) + t.assert.strictEqual(req.cookies.bar, 'bar') done() }) fastify.get('/test2', (req, reply) => { - t.ok(req.cookies) - t.ok(req.cookies.bar) - t.equal(req.cookies.bar, 'bar') + t.assert.ok(req.cookies) + t.assert.ok(req.cookies.bar) + t.assert.strictEqual(req.cookies.bar, 'bar') reply.send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test2', headers: { cookie: 'bar=bar' } - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) }) -test('defined and undefined cookies', (t) => { - t.plan(23) +test('defined and undefined cookies', async (t) => { + t.plan(22) + const fastify = Fastify() fastify.register(plugin) // check that it parses the cookies in the onRequest hook for (const hook of ['preValidation', 'preHandler']) { fastify.addHook(hook, (req, reply, done) => { - t.ok(req.cookies) + t.assert.ok(req.cookies) - t.ok(req.cookies.bar) - t.notOk(req.cookies.baz) + t.assert.ok(req.cookies.bar) + t.assert.ok(!req.cookies.baz) - t.equal(req.cookies.bar, 'bar') - t.equal(req.cookies.baz, undefined) + t.assert.strictEqual(req.cookies.bar, 'bar') + t.assert.strictEqual(req.cookies.baz, undefined) done() }) } fastify.addHook('preParsing', (req, reply, payload, done) => { - t.ok(req.cookies) + t.assert.ok(req.cookies) + + t.assert.ok(req.cookies.bar) + t.assert.ok(!req.cookies.baz) - t.ok(req.cookies.bar) - t.notOk(req.cookies.baz) + t.assert.strictEqual(req.cookies.bar, 'bar') + t.assert.strictEqual(req.cookies.baz, undefined) - t.equal(req.cookies.bar, 'bar') - t.equal(req.cookies.baz, undefined) done() }) fastify.get('/test2', (req, reply) => { - t.ok(req.cookies) + t.assert.ok(req.cookies) - t.ok(req.cookies.bar) - t.notOk(req.cookies.baz) + t.assert.ok(req.cookies.bar) + t.assert.ok(!req.cookies.baz) - t.equal(req.cookies.bar, 'bar') - t.equal(req.cookies.baz, undefined) + t.assert.strictEqual(req.cookies.bar, 'bar') + t.assert.strictEqual(req.cookies.baz, undefined) reply.send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test2', headers: { cookie: 'bar=bar' } - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) }) -test('does not modify supplied cookie options object', (t) => { - t.plan(3) +test('does not modify supplied cookie options object', async (t) => { + t.plan(2) + const expireDate = Date.now() + 1000 const cookieOptions = { path: '/', @@ -378,21 +371,20 @@ test('does not modify supplied cookie options object', (t) => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.strictSame(cookieOptions, { - path: '/', - expires: expireDate - }) + }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(cookieOptions, { + path: '/', + expires: expireDate }) }) -test('cookies gets cleared correctly', (t) => { - t.plan(5) +test('cookies gets cleared correctly', async (t) => { + t.plan(4) + const fastify = Fastify() fastify.register(plugin) @@ -402,25 +394,22 @@ test('cookies gets cleared correctly', (t) => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) - - const cookies = res.cookies - t.equal(cookies.length, 1) - t.equal(new Date(cookies[0].expires) < new Date(), true) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) + + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 1) + t.assert.strictEqual(new Date(cookies[0].expires) < new Date(), true) }) -test('cookies signature', (t) => { - t.plan(9) +describe('cookies signature', () => { + test('unsign', async (t) => { + t.plan(5) - t.test('unsign', t => { - t.plan(6) const fastify = Fastify() const secret = 'bar' fastify.register(plugin, { secret }) @@ -431,23 +420,22 @@ test('cookies signature', (t) => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) - - const cookies = res.cookies - t.equal(cookies.length, 1) - t.equal(cookies[0].name, 'foo') - t.same(unsign(cookies[0].value, secret), { valid: true, renew: false, value: 'foo' }) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) + + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 1) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.deepStrictEqual(unsign(cookies[0].value, secret), { valid: true, renew: false, value: 'foo' }) }) - t.test('key rotation uses first key to sign', t => { - t.plan(6) + test('key rotation uses first key to sign', async (t) => { + t.plan(5) + const fastify = Fastify() const secret1 = 'secret-1' const secret2 = 'secret-2' @@ -459,23 +447,22 @@ test('cookies signature', (t) => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) - - const cookies = res.cookies - t.equal(cookies.length, 1) - t.equal(cookies[0].name, 'foo') - t.same(unsign(cookies[0].value, secret1), { valid: true, renew: false, value: 'cookieVal' }) // decode using first key }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) + + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 1) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.deepStrictEqual(unsign(cookies[0].value, secret1), { valid: true, renew: false, value: 'cookieVal' }) // decode using first key }) - t.test('unsginCookie via fastify instance', t => { - t.plan(3) + test('unsginCookie via fastify instance', async (t) => { + t.plan(2) + const fastify = Fastify() const secret = 'bar' @@ -487,21 +474,20 @@ test('cookies signature', (t) => { }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1', headers: { cookie: `foo=${sign('foo', secret)}` } - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { unsigned: { value: 'foo', renew: false, valid: true } }) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { unsigned: { value: 'foo', renew: false, valid: true } }) }) - t.test('unsignCookie via request decorator', t => { - t.plan(3) + test('unsignCookie via request decorator', async (t) => { + t.plan(2) + const fastify = Fastify() const secret = 'bar' fastify.register(plugin, { secret }) @@ -512,21 +498,20 @@ test('cookies signature', (t) => { }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1', headers: { cookie: `foo=${sign('foo', secret)}` } - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { unsigned: { value: 'foo', renew: false, valid: true } }) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { unsigned: { value: 'foo', renew: false, valid: true } }) }) - t.test('unsignCookie via reply decorator', t => { - t.plan(3) + test('unsignCookie via reply decorator', async (t) => { + t.plan(2) + const fastify = Fastify() const secret = 'bar' fastify.register(plugin, { secret }) @@ -537,21 +522,20 @@ test('cookies signature', (t) => { }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1', headers: { cookie: `foo=${sign('foo', secret)}` } - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { unsigned: { value: 'foo', renew: false, valid: true } }) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { unsigned: { value: 'foo', renew: false, valid: true } }) }) - t.test('unsignCookie via request decorator after rotation', t => { - t.plan(3) + test('unsignCookie via request decorator after rotation', async (t) => { + t.plan(2) + const fastify = Fastify() const secret1 = 'sec-1' const secret2 = 'sec-2' @@ -563,21 +547,20 @@ test('cookies signature', (t) => { }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1', headers: { cookie: `foo=${sign('foo', secret2)}` } - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { unsigned: { value: 'foo', renew: true, valid: true } }) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { unsigned: { value: 'foo', renew: true, valid: true } }) }) - t.test('unsignCookie via reply decorator after rotation', t => { - t.plan(3) + test('unsignCookie via reply decorator after rotation', async (t) => { + t.plan(2) + const fastify = Fastify() const secret1 = 'sec-1' const secret2 = 'sec-2' @@ -589,21 +572,20 @@ test('cookies signature', (t) => { }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1', headers: { cookie: `foo=${sign('foo', secret2)}` } - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { unsigned: { value: 'foo', renew: true, valid: true } }) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { unsigned: { value: 'foo', renew: true, valid: true } }) }) - t.test('unsignCookie via request decorator failure response', t => { - t.plan(3) + test('unsignCookie via request decorator failure response', async (t) => { + t.plan(2) + const fastify = Fastify() const secret1 = 'sec-1' const secret2 = 'sec-2' @@ -615,21 +597,20 @@ test('cookies signature', (t) => { }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1', headers: { cookie: `foo=${sign('foo', 'invalid-secret')}` } - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { unsigned: { value: null, renew: false, valid: false } }) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { unsigned: { value: null, renew: false, valid: false } }) }) - t.test('unsignCookie reply decorator failure response', t => { - t.plan(3) + test('unsignCookie reply decorator failure response', async (t) => { + t.plan(2) + const fastify = Fastify() const secret1 = 'sec-1' const secret2 = 'sec-2' @@ -641,22 +622,21 @@ test('cookies signature', (t) => { }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1', headers: { cookie: `foo=${sign('foo', 'invalid-secret')}` } - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { unsigned: { value: null, renew: false, valid: false } }) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { unsigned: { value: null, renew: false, valid: false } }) }) }) -test('custom signer', t => { - t.plan(7) +test('custom signer', async (t) => { + t.plan(6) + const fastify = Fastify() const signStub = sinon.stub().returns('SIGNED-VALUE') const unsignStub = sinon.stub().returns('ORIGINAL VALUE') @@ -669,24 +649,23 @@ test('custom signer', t => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) - - const cookies = res.cookies - t.equal(cookies.length, 1) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, 'SIGNED-VALUE') - t.ok(signStub.calledOnceWithExactly('bar')) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) + + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 1) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, 'SIGNED-VALUE') + t.assert.ok(signStub.calledOnceWithExactly('bar')) }) -test('unsignCookie decorator with custom signer', t => { - t.plan(4) +test('unsignCookie decorator with custom signer', async (t) => { + t.plan(3) + const fastify = Fastify() const signStub = sinon.stub().returns('SIGNED-VALUE') const unsignStub = sinon.stub().returns('ORIGINAL VALUE') @@ -699,22 +678,21 @@ test('unsignCookie decorator with custom signer', t => { }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1', headers: { cookie: 'foo=SOME-SIGNED-VALUE' } - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { unsigned: 'ORIGINAL VALUE' }) - t.ok(unsignStub.calledOnceWithExactly('SOME-SIGNED-VALUE')) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { unsigned: 'ORIGINAL VALUE' }) + t.assert.ok(unsignStub.calledOnceWithExactly('SOME-SIGNED-VALUE')) }) -test('pass options to `cookies.parse`', (t) => { - t.plan(6) +test('pass options to `cookies.parse`', async (t) => { + t.plan(5) + const fastify = Fastify() fastify.register(plugin, { parseOptions: { @@ -723,31 +701,30 @@ test('pass options to `cookies.parse`', (t) => { }) fastify.get('/test1', (req, reply) => { - t.ok(req.cookies) - t.ok(req.cookies.foo) - t.equal(req.cookies.foo, 'bartest') + t.assert.ok(req.cookies) + t.assert.ok(req.cookies.foo) + t.assert.strictEqual(req.cookies.foo, 'bartest') reply.send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1', headers: { cookie: 'foo=bar' } - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) function decoder (str) { return str + 'test' } }) -test('issue 53', (t) => { - t.plan(5) +test('issue 53', async (t) => { + t.plan(3) + const fastify = Fastify() fastify.register(plugin) @@ -755,7 +732,7 @@ test('issue 53', (t) => { let count = 1 fastify.get('/foo', (req, reply) => { if (count > 1) { - t.not(cookies, req.cookies) + t.assert.notEqual(cookies, req.cookies) return reply.send('done') } @@ -763,44 +740,44 @@ test('issue 53', (t) => { cookies = req.cookies reply.send('done') }) + { + const res = await fastify.inject({ url: '/foo' }) + t.assert.strictEqual(res.body, 'done') + } - fastify.inject({ url: '/foo' }, (err, response) => { - t.error(err) - t.equal(response.body, 'done') - }) - - fastify.inject({ url: '/foo' }, (err, response) => { - t.error(err) - t.equal(response.body, 'done') - }) + { + const res = await fastify.inject({ url: '/foo' }) + t.assert.strictEqual(res.body, 'done') + } }) -test('serialize cookie manually using decorator', (t) => { +test('serialize cookie manually using decorator', async (t) => { t.plan(2) + const fastify = Fastify() fastify.register(plugin) - fastify.ready(() => { - t.ok(fastify.serializeCookie) - t.same(fastify.serializeCookie('foo', 'bar', {}), 'foo=bar') - t.end() - }) + await new Promise(resolve => fastify.ready(resolve)) + + t.assert.ok(fastify.serializeCookie) + t.assert.deepStrictEqual(fastify.serializeCookie('foo', 'bar', {}), 'foo=bar') }) -test('parse cookie manually using decorator', (t) => { +test('parse cookie manually using decorator', async (t) => { t.plan(2) + const fastify = Fastify() fastify.register(plugin) - fastify.ready(() => { - t.ok(fastify.parseCookie) - t.same(fastify.parseCookie('foo=bar', {}), { foo: 'bar' }) - t.end() - }) + await new Promise(resolve => fastify.ready(resolve)) + + t.assert.ok(fastify.parseCookie) + t.assert.deepStrictEqual({ ...fastify.parseCookie('foo=bar', {}) }, { foo: 'bar' }) }) -test('cookies set with plugin options parseOptions field', (t) => { - t.plan(8) +test('cookies set with plugin options parseOptions field', async (t) => { + t.plan(7) + const fastify = Fastify() fastify.register(plugin, { parseOptions: { @@ -813,27 +790,25 @@ test('cookies set with plugin options parseOptions field', (t) => { reply.setCookie('foo', 'foo').send({ hello: 'world' }) }) - fastify.inject( + const res = await fastify.inject( { method: 'GET', url: '/test' - }, - (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) - - const cookies = res.cookies - t.equal(cookies.length, 1) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, 'foo') - t.equal(cookies[0].path, '/test') - t.equal(cookies[0].domain, 'example.com') - } - ) + }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) + + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 1) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, 'foo') + t.assert.strictEqual(cookies[0].path, '/test') + t.assert.strictEqual(cookies[0].domain, 'example.com') }) test('create signed cookie manually using signCookie decorator', async (t) => { + t.plan(2) + const fastify = Fastify() await fastify.register(plugin, { secret: 'secret' }) @@ -849,11 +824,13 @@ test('create signed cookie manually using signCookie decorator', async (t) => { url: '/test1', headers: { cookie: `foo=${fastify.signCookie('bar')}` } }) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { unsigned: { value: 'bar', renew: false, valid: true } }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { unsigned: { value: 'bar', renew: false, valid: true } }) }) test('handle secure:auto of cookieOptions', async (t) => { + t.plan(11) + const fastify = Fastify({ trustProxy: true }) await fastify.register(plugin) @@ -871,11 +848,11 @@ test('handle secure:auto of cookieOptions', async (t) => { }) const cookies = res.cookies - t.equal(cookies.length, 1) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, 'foo') - t.equal(cookies[0].secure, true) - t.equal(cookies[0].path, '/') + t.assert.strictEqual(cookies.length, 1) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, 'foo') + t.assert.strictEqual(cookies[0].secure, true) + t.assert.strictEqual(cookies[0].path, '/') const res2 = await fastify.inject({ method: 'GET', @@ -883,28 +860,29 @@ test('handle secure:auto of cookieOptions', async (t) => { }) const cookies2 = res2.cookies - t.equal(cookies2.length, 1) - t.equal(cookies2[0].name, 'foo') - t.equal(cookies2[0].value, 'foo') - t.equal(cookies2[0].sameSite, 'Lax') - t.same(cookies2[0].secure, null) - t.equal(cookies2[0].path, '/') + t.assert.strictEqual(cookies2.length, 1) + t.assert.strictEqual(cookies2[0].name, 'foo') + t.assert.strictEqual(cookies2[0].value, 'foo') + t.assert.strictEqual(cookies2[0].sameSite, 'Lax') + t.assert.deepStrictEqual(cookies2[0].secure, undefined) + t.assert.strictEqual(cookies2[0].path, '/') }) test('should not decorate fastify, request and reply if no secret was provided', async (t) => { t.plan(8) + const fastify = Fastify() await fastify.register(plugin) - t.notOk(fastify.signCookie) - t.notOk(fastify.unsignCookie) + t.assert.ok(!fastify.signCookie) + t.assert.ok(!fastify.unsignCookie) fastify.get('/testDecorators', (req, reply) => { - t.notOk(req.signCookie) - t.notOk(reply.signCookie) - t.notOk(req.unsignCookie) - t.notOk(reply.unsignCookie) + t.assert.ok(!req.signCookie) + t.assert.ok(!reply.signCookie) + t.assert.ok(!req.unsignCookie) + t.assert.ok(!reply.unsignCookie) reply.send({ unsigned: req.unsignCookie(req.cookies.foo) @@ -916,111 +894,110 @@ test('should not decorate fastify, request and reply if no secret was provided', url: '/testDecorators' }) - t.equal(res.statusCode, 500) - t.same(JSON.parse(res.body), { + t.assert.strictEqual(res.statusCode, 500) + t.assert.deepStrictEqual(JSON.parse(res.body), { statusCode: 500, error: 'Internal Server Error', message: 'req.unsignCookie is not a function' }) }) -test('dont add auto cookie parsing to onRequest-hook if hook-option is set to false', (t) => { - t.plan(6) +test('dont add auto cookie parsing to onRequest-hook if hook-option is set to false', async (t) => { + t.plan(5) + const fastify = Fastify() fastify.register(plugin, { hook: false }) for (const hook of ['preValidation', 'preHandler', 'preParsing']) { fastify.addHook(hook, async (req) => { - t.equal(req.cookies, null) + t.assert.strictEqual(req.cookies, null) }) } fastify.get('/disable', (req, reply) => { - t.equal(req.cookies, null) + t.assert.strictEqual(req.cookies, null) reply.send() }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/disable', headers: { cookie: 'bar=bar' } - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) }) + t.assert.strictEqual(res.statusCode, 200) }) -test('result in an error if hook-option is set to an invalid value', (t) => { +test('result in an error if hook-option is set to an invalid value', async (t) => { t.plan(1) + const fastify = Fastify() - t.rejects( + await t.assert.rejects( async () => fastify.register(plugin, { hook: true }), new Error("@fastify/cookie: Invalid value provided for the hook-option. You can set the hook-option only to false, 'onRequest' , 'preParsing' , 'preValidation' or 'preHandler'") ) }) -test('correct working plugin if hook-option to preParsing', (t) => { - t.plan(5) +test('correct working plugin if hook-option to preParsing', async (t) => { + t.plan(4) + const fastify = Fastify() fastify.register(plugin, { hook: 'preParsing' }) fastify.addHook('onRequest', async (req) => { - t.equal(req.cookies, null) + t.assert.strictEqual(req.cookies, null) }) fastify.addHook('preValidation', async (req) => { - t.equal(req.cookies.bar, 'bar') + t.assert.strictEqual(req.cookies.bar, 'bar') }) fastify.get('/preparsing', (req, reply) => { - t.equal(req.cookies.bar, 'bar') + t.assert.strictEqual(req.cookies.bar, 'bar') reply.send() }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/preparsing', headers: { cookie: 'bar=bar' } - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) }) + t.assert.strictEqual(res.statusCode, 200) }) -test('if cookies are not set, then the handler creates an empty req.cookies object', (t) => { - t.plan(5) +test('if cookies are not set, then the handler creates an empty req.cookies object', async (t) => { + t.plan(4) + const fastify = Fastify() fastify.register(plugin, { hook: 'preParsing' }) fastify.addHook('onRequest', async (req) => { - t.equal(req.cookies, null) + t.assert.strictEqual(req.cookies, null) }) fastify.addHook('preValidation', async (req) => { - t.ok(req.cookies) + t.assert.ok(req.cookies) }) fastify.get('/preparsing', (req, reply) => { - t.ok(req.cookies) + t.assert.ok(req.cookies) reply.send() }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/preparsing' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) }) + t.assert.strictEqual(res.statusCode, 200) }) -test('clearCookie should include parseOptions', (t) => { - t.plan(10) +test('clearCookie should include parseOptions', async (t) => { + t.plan(9) + const fastify = Fastify() fastify.register(plugin, { parseOptions: { @@ -1041,29 +1018,28 @@ test('clearCookie should include parseOptions', (t) => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) + }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) - const cookies = res.cookies + const cookies = res.cookies - t.equal(cookies.length, 1) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, '') - t.equal(cookies[0].maxAge, 0) - t.equal(cookies[0].path, '/test') - t.equal(cookies[0].domain, 'example.com') + t.assert.strictEqual(cookies.length, 1) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, '') + t.assert.strictEqual(cookies[0].maxAge, 0) + t.assert.strictEqual(cookies[0].path, '/test') + t.assert.strictEqual(cookies[0].domain, 'example.com') - t.ok(new Date(cookies[0].expires) < new Date()) - }) + t.assert.ok(new Date(cookies[0].expires) < new Date()) }) -test('should update a cookie value when setCookie is called multiple times', (t) => { - t.plan(15) +test('should update a cookie value when setCookie is called multiple times', async (t) => { + t.plan(14) + const fastify = Fastify() const secret = 'testsecret' fastify.register(plugin, { secret }) @@ -1089,36 +1065,35 @@ test('should update a cookie value when setCookie is called multiple times', (t) .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) + }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) - const cookies = res.cookies - t.equal(cookies.length, 3) + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 3) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, '') - t.equal(cookies[0].path, '/foo') + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, '') + t.assert.strictEqual(cookies[0].path, '/foo') - t.equal(cookies[1].name, 'foo') - t.equal(cookies[1].value, sign('foo', secret)) - t.equal(cookies[1].maxAge, 36000) + t.assert.strictEqual(cookies[1].name, 'foo') + t.assert.strictEqual(cookies[1].value, sign('foo', secret)) + t.assert.strictEqual(cookies[1].maxAge, 36000) - t.equal(cookies[2].name, 'foos') - t.equal(cookies[2].value, sign('foosy', secret)) - t.equal(cookies[2].path, '/foo') - t.equal(cookies[2].maxAge, 36000) + t.assert.strictEqual(cookies[2].name, 'foos') + t.assert.strictEqual(cookies[2].value, sign('foosy', secret)) + t.assert.strictEqual(cookies[2].path, '/foo') + t.assert.strictEqual(cookies[2].maxAge, 36000) - t.ok(new Date(cookies[0].expires) < new Date()) - }) + t.assert.ok(new Date(cookies[0].expires) < new Date()) }) -test('should update a cookie value when setCookie is called multiple times (empty header)', (t) => { - t.plan(15) +test('should update a cookie value when setCookie is called multiple times (empty header)', async (t) => { + t.plan(14) + const fastify = Fastify() const secret = 'testsecret' fastify.register(plugin, { secret }) @@ -1145,36 +1120,35 @@ test('should update a cookie value when setCookie is called multiple times (empt .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) + }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) - const cookies = res.cookies - t.equal(cookies.length, 3) + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 3) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, '') - t.equal(cookies[0].path, '/foo') + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, '') + t.assert.strictEqual(cookies[0].path, '/foo') - t.equal(cookies[1].name, 'foo') - t.equal(cookies[1].value, sign('foo', secret)) - t.equal(cookies[1].maxAge, 36000) + t.assert.strictEqual(cookies[1].name, 'foo') + t.assert.strictEqual(cookies[1].value, sign('foo', secret)) + t.assert.strictEqual(cookies[1].maxAge, 36000) - t.equal(cookies[2].name, 'foos') - t.equal(cookies[2].value, sign('foosy', secret)) - t.equal(cookies[2].path, '/foo') - t.equal(cookies[2].maxAge, 36000) + t.assert.strictEqual(cookies[2].name, 'foos') + t.assert.strictEqual(cookies[2].value, sign('foosy', secret)) + t.assert.strictEqual(cookies[2].path, '/foo') + t.assert.strictEqual(cookies[2].maxAge, 36000) - t.ok(new Date(cookies[0].expires) < new Date()) - }) + t.assert.ok(new Date(cookies[0].expires) < new Date()) }) -test('should update a cookie value when setCookie is called multiple times (non-empty header)', (t) => { - t.plan(15) +test('should update a cookie value when setCookie is called multiple times (non-empty header)', async (t) => { + t.plan(14) + const fastify = Fastify() const secret = 'testsecret' fastify.register(plugin, { secret }) @@ -1201,36 +1175,35 @@ test('should update a cookie value when setCookie is called multiple times (non- .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) + }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) - const cookies = res.cookies - t.equal(cookies.length, 4) + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 4) - t.equal(cookies[1].name, 'foo') - t.equal(cookies[1].value, '') - t.equal(cookies[1].path, '/foo') + t.assert.strictEqual(cookies[1].name, 'foo') + t.assert.strictEqual(cookies[1].value, '') + t.assert.strictEqual(cookies[1].path, '/foo') - t.equal(cookies[2].name, 'foo') - t.equal(cookies[2].value, sign('foo', secret)) - t.equal(cookies[2].maxAge, 36000) + t.assert.strictEqual(cookies[2].name, 'foo') + t.assert.strictEqual(cookies[2].value, sign('foo', secret)) + t.assert.strictEqual(cookies[2].maxAge, 36000) - t.equal(cookies[3].name, 'foos') - t.equal(cookies[3].value, sign('foosy', secret)) - t.equal(cookies[3].path, '/foo') - t.equal(cookies[3].maxAge, 36000) + t.assert.strictEqual(cookies[3].name, 'foos') + t.assert.strictEqual(cookies[3].value, sign('foosy', secret)) + t.assert.strictEqual(cookies[3].path, '/foo') + t.assert.strictEqual(cookies[3].maxAge, 36000) - t.ok(new Date(cookies[1].expires) < new Date()) - }) + t.assert.ok(new Date(cookies[1].expires) < new Date()) }) -test('cookies get set correctly if set inside onSend', (t) => { - t.plan(7) +test('cookies get set correctly if set inside onSend', async (t) => { + t.plan(6) + const fastify = Fastify() fastify.register(plugin) @@ -1244,24 +1217,23 @@ test('cookies get set correctly if set inside onSend', (t) => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) - - const cookies = res.cookies - t.equal(cookies.length, 1) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, 'foo') - t.equal(cookies[0].path, '/') }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) + + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 1) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, 'foo') + t.assert.strictEqual(cookies[0].path, '/') }) -test('cookies get set correctly if set inside multiple onSends', (t) => { - t.plan(10) +test('cookies get set correctly if set inside multiple onSends', async (t) => { + t.plan(9) + const fastify = Fastify() fastify.register(plugin) @@ -1279,28 +1251,27 @@ test('cookies get set correctly if set inside multiple onSends', (t) => { .send({ hello: 'world' }) }) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) - - const cookies = res.cookies - t.equal(cookies.length, 2) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, 'foo') - t.equal(cookies[0].path, '/') - - t.equal(cookies[1].name, 'foo') - t.equal(cookies[1].value, 'foos') - t.equal(cookies[1].path, '/') }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) + + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 2) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, 'foo') + t.assert.strictEqual(cookies[0].path, '/') + + t.assert.strictEqual(cookies[1].name, 'foo') + t.assert.strictEqual(cookies[1].value, 'foos') + t.assert.strictEqual(cookies[1].path, '/') }) -test('cookies get set correctly if set inside onRequest', (t) => { - t.plan(7) +test('cookies get set correctly if set inside onRequest', async (t) => { + t.plan(6) + const fastify = Fastify() fastify.addHook('onRequest', async (req, reply) => { reply.setCookie('foo', 'foo', { path: '/' }) @@ -1309,24 +1280,23 @@ test('cookies get set correctly if set inside onRequest', (t) => { fastify.register(plugin) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1' - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) - - const cookies = res.cookies - t.equal(cookies.length, 1) - t.equal(cookies[0].name, 'foo') - t.equal(cookies[0].value, 'foo') - t.equal(cookies[0].path, '/') }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) + + const cookies = res.cookies + t.assert.strictEqual(cookies.length, 1) + t.assert.strictEqual(cookies[0].name, 'foo') + t.assert.strictEqual(cookies[0].value, 'foo') + t.assert.strictEqual(cookies[0].path, '/') }) -test('do not crash if the onRequest hook is not run', (t) => { - t.plan(3) +test('do not crash if the onRequest hook is not run', async (t) => { + t.plan(2) + const fastify = Fastify() fastify.addHook('onRequest', async (req, reply) => { return reply.send({ hello: 'world' }) @@ -1334,15 +1304,13 @@ test('do not crash if the onRequest hook is not run', (t) => { fastify.register(plugin) - fastify.inject({ + const res = await fastify.inject({ method: 'GET', url: '/test1', headers: { cookie: 'foo=foo' } - }, (err, res) => { - t.error(err) - t.equal(res.statusCode, 200) - t.same(JSON.parse(res.body), { hello: 'world' }) }) + t.assert.strictEqual(res.statusCode, 200) + t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' }) }) diff --git a/test/signer.test.js b/test/signer.test.js index 1ac7224..2d77845 100644 --- a/test/signer.test.js +++ b/test/signer.test.js @@ -1,138 +1,125 @@ 'use strict' -const { test } = require('tap') +const { beforeEach, describe, test } = require('node:test') const sinon = require('sinon') const crypto = require('node:crypto') -const { Signer, sign, unsign } = require('../signer') -test('default', t => { - t.plan(5) +const { Signer, sign, unsign } = require('../signer') +describe('default', () => { const secret = 'my-secret' const signer = Signer(secret) - t.test('signer.sign should throw if there is no value provided', (t) => { + test('signer.sign should throw if there is no value provided', (t) => { t.plan(1) - - t.throws(() => signer.sign(undefined), 'Cookie value must be provided as a string.') - }) - - t.test('signer.sign', (t) => { - t.plan(2) - - const input = 'some-value' - const result = signer.sign(input) - - t.equal(result, sign(input, secret)) - t.throws(() => sign(undefined), 'Cookie value must be provided as a string.') + t.assert.throws(() => signer.sign(undefined), err => err.message === 'Cookie value must be provided as a string.') }) - t.test('sign', (t) => { + test('sign', (t) => { t.plan(5) const input = 'some-value' const result = signer.sign(input) - t.equal(result, sign(input, secret)) - t.equal(result, sign(input, [secret])) - t.equal(result, sign(input, Buffer.from(secret))) - t.equal(result, sign(input, [Buffer.from(secret)])) + t.assert.strictEqual(result, sign(input, secret)) + t.assert.strictEqual(result, sign(input, [secret])) + t.assert.strictEqual(result, sign(input, Buffer.from(secret))) + t.assert.strictEqual(result, sign(input, [Buffer.from(secret)])) - t.throws(() => sign(undefined), 'Cookie value must be provided as a string.') + t.assert.throws(() => sign(undefined), err => err.message === 'Secret key must be a string or Buffer.') }) - t.test('signer.unsign', (t) => { + test('signer.unsign', (t) => { t.plan(4) const input = signer.sign('some-value', secret) const result = signer.unsign(input) - t.equal(result.valid, true) - t.equal(result.renew, false) - t.equal(result.value, 'some-value') - t.throws(() => signer.unsign(undefined), 'Signed cookie string must be provided.') + t.assert.strictEqual(result.valid, true) + t.assert.strictEqual(result.renew, false) + t.assert.strictEqual(result.value, 'some-value') + t.assert.throws(() => signer.unsign(undefined), err => err.message === 'Signed cookie string must be provided.') }) - t.test('unsign', (t) => { + test('unsign', (t) => { t.plan(6) const input = sign('some-value', secret) const result = unsign(input, secret) - t.equal(result.valid, true) - t.equal(result.renew, false) - t.equal(result.value, 'some-value') - t.same(result, unsign(input, [secret])) - t.throws(() => unsign(undefined), 'Secret key must be a string or Buffer.') - t.throws(() => unsign(undefined, secret), 'Signed cookie string must be provided.') + t.assert.strictEqual(result.valid, true) + t.assert.strictEqual(result.renew, false) + t.assert.strictEqual(result.value, 'some-value') + t.assert.deepStrictEqual(result, unsign(input, [secret])) + t.assert.throws(() => unsign(undefined), err => err.message === 'Secret key must be a string or Buffer.') + t.assert.throws(() => unsign(undefined, secret), err => err.message === 'Signed cookie string must be provided.') }) }) -test('key rotation', (t) => { - t.plan(3) +describe('key rotation', () => { const secret1 = 'my-secret-1' const secret2 = 'my-secret-2' const secret3 = 'my-secret-3' const signer = Signer([secret1, secret2, secret3]) const signSpy = sinon.spy(crypto, 'createHmac') - t.beforeEach(() => { + beforeEach(() => { signSpy.resetHistory() }) - t.test('signer.sign always signs using first key', (t) => { + test('signer.sign always signs using first key', (t) => { t.plan(1) const input = 'some-value' const result = signer.sign(input) - t.equal(result, sign(input, secret1)) + t.assert.strictEqual(result, sign(input, secret1)) }) - t.test('signer.unsign tries to decode using all keys till it finds', (t) => { + test('signer.unsign tries to decode using all keys till it finds', (t) => { t.plan(4) const input = sign('some-value', secret2) signSpy.resetHistory() const result = signer.unsign(input) - t.equal(result.valid, true) - t.equal(result.renew, true) - t.equal(result.value, 'some-value') - t.equal(signSpy.callCount, 2) // should have returned early when the right key was found + t.assert.strictEqual(result.valid, true) + t.assert.strictEqual(result.renew, true) + t.assert.strictEqual(result.value, 'some-value') + t.assert.strictEqual(signSpy.callCount, 2) // should have returned early when the right key was found }) - t.test('signer.unsign failure response', (t) => { + test('signer.unsign failure response', (t) => { t.plan(4) const input = sign('some-value', 'invalid-secret') signSpy.resetHistory() const result = signer.unsign(input) - t.equal(result.valid, false) - t.equal(result.renew, false) - t.equal(result.value, null) - t.equal(signSpy.callCount, 3) // should have tried all 3 + t.assert.strictEqual(result.valid, false) + t.assert.strictEqual(result.renew, false) + t.assert.strictEqual(result.value, null) + t.assert.strictEqual(signSpy.callCount, 3) // should have tried all 3 }) }) -test('Signer', t => { - t.plan(2) - - t.test('Signer needs a string or Buffer as secret', (t) => { +describe('Signer', () => { + test('Signer needs a string or Buffer as secret', (t) => { t.plan(6) - t.throws(() => Signer(1), 'Secret key must be a string or Buffer.') - t.throws(() => Signer(undefined), 'Secret key must be a string or Buffer.') - t.doesNotThrow(() => Signer('secret')) - t.doesNotThrow(() => Signer(['secret'])) - t.doesNotThrow(() => Signer(Buffer.from('deadbeef76543210', 'hex'))) - t.doesNotThrow(() => Signer([Buffer.from('deadbeef76543210', 'hex')])) + + t.assert.throws(() => Signer(1), err => err.message === 'Secret key must be a string or Buffer.') + t.assert.throws(() => Signer(undefined), err => err.message === 'Secret key must be a string or Buffer.') + t.assert.doesNotThrow(() => Signer('secret')) + t.assert.doesNotThrow(() => Signer(['secret'])) + t.assert.doesNotThrow(() => Signer(Buffer.from('deadbeef76543210', 'hex'))) + t.assert.doesNotThrow(() => Signer([Buffer.from('deadbeef76543210', 'hex')])) }) - t.test('Signer handles algorithm properly', (t) => { + test('Signer handles algorithm properly', (t) => { t.plan(3) - t.throws(() => Signer('secret', 'invalid'), 'Algorithm invalid not supported.') - t.doesNotThrow(() => Signer('secret', 'sha512')) - t.doesNotThrow(() => Signer('secret', 'sha256')) + + t.assert.throws(() => Signer('secret', 'invalid'), err => err.message === 'Algorithm invalid not supported.') + t.assert.doesNotThrow(() => Signer('secret', 'sha512')) + t.assert.doesNotThrow(() => Signer('secret', 'sha256')) }) })