From 7d46361c4c95ed52518112d5e10eb6b8a6771693 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Fri, 3 Jan 2025 18:16:36 +0000 Subject: [PATCH] test: prefix unused params with underscores (#277) --- examples/example.js | 4 +-- test/global.test.js | 62 +++++++++++++++++++++---------------------- test/routes.test.js | 26 +++++++++--------- types/index.test-d.ts | 14 +++++----- 4 files changed, 53 insertions(+), 53 deletions(-) diff --git a/examples/example.js b/examples/example.js index ab979e7..920a53e 100644 --- a/examples/example.js +++ b/examples/example.js @@ -26,14 +26,14 @@ const opts = { } } -fastify.get('/', opts, function (request, reply) { +fastify.get('/', opts, function (_request, reply) { reply .header('Content-Type', 'application/json') .code(200) .send({ hello: 'world' }) }) -fastify.get('/route-with-disabled-helmet', { ...opts, helmet: false }, function (request, reply) { +fastify.get('/route-with-disabled-helmet', { ...opts, helmet: false }, function (_request, reply) { reply .header('Content-Type', 'application/json') .code(200) diff --git a/test/global.test.js b/test/global.test.js index f26d2f5..8586f95 100644 --- a/test/global.test.js +++ b/test/global.test.js @@ -12,7 +12,7 @@ test('It should set the default headers', async (t) => { const fastify = Fastify() await fastify.register(helmet) - fastify.get('/', (request, reply) => { + fastify.get('/', (_request, reply) => { reply.send({ hello: 'world' }) }) @@ -46,7 +46,7 @@ test('It should not set the default headers when global is set to `false`', asyn const fastify = Fastify() await fastify.register(helmet, { global: false }) - fastify.get('/', (request, reply) => { + fastify.get('/', (_request, reply) => { reply.send({ hello: 'world' }) }) @@ -80,7 +80,7 @@ test('It should set the default cross-domain-policy', async (t) => { const fastify = Fastify() await fastify.register(helmet) - fastify.get('/', (request, reply) => { + fastify.get('/', (_request, reply) => { reply.send({ hello: 'world' }) }) @@ -106,7 +106,7 @@ test('It should be able to set cross-domain-policy', async (t) => { permittedCrossDomainPolicies: { permittedPolicies: 'by-content-type' } }) - fastify.get('/', (request, reply) => { + fastify.get('/', (_request, reply) => { reply.send({ hello: 'world' }) }) @@ -131,7 +131,7 @@ test('It should not disable the other headers when disabling one header', async const fastify = Fastify() await fastify.register(helmet, { frameguard: false }) - fastify.get('/', (request, reply) => { + fastify.get('/', (_request, reply) => { reply.send({ hello: 'world' }) }) @@ -173,7 +173,7 @@ test('It should be able to access default CSP directives through plugin export', } }) - fastify.get('/', (request, reply) => { + fastify.get('/', (_request, reply) => { reply.send({ hello: 'world' }) }) @@ -206,7 +206,7 @@ test('It should not set default directives when useDefaults is set to `false`', } }) - fastify.get('/', (request, reply) => { + fastify.get('/', (_request, reply) => { reply.send({ hello: 'world' }) }) @@ -231,7 +231,7 @@ test('It should auto generate nonce per request', async (t) => { enableCSPNonces: true }) - fastify.get('/', (request, reply) => { + fastify.get('/', (_request, reply) => { t.assert.ok(reply.cspNonce) reply.send(reply.cspNonce) }) @@ -265,7 +265,7 @@ test('It should allow merging options for enableCSPNonces', async (t) => { } }) - fastify.get('/', (request, reply) => { + fastify.get('/', (_request, reply) => { t.assert.ok(reply.cspNonce) reply.send(reply.cspNonce) }) @@ -297,7 +297,7 @@ test('It should not set default directives when using enableCSPNonces and useDef } }) - fastify.get('/', (request, reply) => { + fastify.get('/', (_request, reply) => { t.assert.ok(reply.cspNonce) reply.send(reply.cspNonce) }) @@ -328,7 +328,7 @@ test('It should not stack nonce array in csp header', async (t) => { } }) - fastify.get('/', (request, reply) => { + fastify.get('/', (_request, reply) => { t.assert.ok(reply.cspNonce) reply.send(reply.cspNonce) }) @@ -368,7 +368,7 @@ test('It should access the correct options property', async (t) => { } }) - fastify.get('/', (request, reply) => { + fastify.get('/', (_request, reply) => { t.assert.ok(reply.cspNonce) reply.send(reply.cspNonce) }) @@ -397,7 +397,7 @@ test('It should not set script-src or style-src', async (t) => { } }) - fastify.get('/', (request, reply) => { + fastify.get('/', (_request, reply) => { t.assert.ok(reply.cspNonce) reply.send(reply.cspNonce) }) @@ -418,7 +418,7 @@ test('It should add hooks correctly', async (t) => { const fastify = Fastify() - fastify.addHook('onRequest', async (request, reply) => { + fastify.addHook('onRequest', async (_request, reply) => { reply.header('x-fastify-global-test', 'ok') }) @@ -428,12 +428,12 @@ test('It should add hooks correctly', async (t) => { '/one', { onRequest: [ - async (request, reply) => { + async (_request, reply) => { reply.header('x-fastify-test-one', 'ok') } ] }, - (request, reply) => { + () => { return { message: 'one' } } ) @@ -441,16 +441,16 @@ test('It should add hooks correctly', async (t) => { fastify.get( '/two', { - onRequest: async (request, reply) => { + onRequest: async (_request, reply) => { reply.header('x-fastify-test-two', 'ok') } }, - (request, reply) => { + () => { return { message: 'two' } } ) - fastify.get('/three', { onRequest: async () => {} }, (request, reply) => { + fastify.get('/three', { onRequest: async () => {} }, () => { return { message: 'three' } }) @@ -537,7 +537,7 @@ test('It should add the `helmet` reply decorator', async (t) => { const fastify = Fastify() await fastify.register(helmet, { global: false }) - fastify.get('/', async (request, reply) => { + fastify.get('/', async (_request, reply) => { t.assert.ok(reply.helmet) t.assert.notStrictEqual(reply.helmet, null) @@ -579,7 +579,7 @@ test('It should not throw when trying to add the `helmet` and `cspNonce` reply d await fastify.register(helmet, { enableCSPNonces: true, global: true }) - fastify.get('/', async (request, reply) => { + fastify.get('/', async (_request, reply) => { t.assert.ok(reply.helmet) t.assert.notDeepStrictEqual(reply.helmet, null) t.assert.ok(reply.cspNonce) @@ -622,7 +622,7 @@ test('It should be able to pass custom options to the `helmet` reply decorator', const fastify = Fastify() await fastify.register(helmet, { global: false }) - fastify.get('/', async (request, reply) => { + fastify.get('/', async (_request, reply) => { t.assert.ok(reply.helmet) t.assert.notDeepStrictEqual(reply.helmet, null) @@ -744,17 +744,17 @@ test('It should apply helmet headers when returning error messages', async (t) = fastify.get( '/', { - onRequest: async (request, reply) => { + onRequest: async (_request, reply) => { reply.code(401) reply.send({ message: 'Unauthorized' }) } }, - async (request, reply) => { + async () => { return { message: 'ok' } } ) - fastify.get('/error-handler', {}, async (request, reply) => { + fastify.get('/error-handler', {}, async () => { return Promise.reject(new Error('error handler triggered')) }) @@ -828,7 +828,7 @@ test('It should not return a fastify `FST_ERR_REP_ALREADY_SENT - Reply already s const logs = [] const destination = new stream.Writable({ - write: function (chunk, encoding, next) { + write: function (chunk, _encoding, next) { logs.push(JSON.parse(chunk)) next() } @@ -839,7 +839,7 @@ test('It should not return a fastify `FST_ERR_REP_ALREADY_SENT - Reply already s await fastify.register(helmet) await fastify.register( fp( - async (instance, options) => { + async (instance, _options) => { instance.addHook('onRequest', async (request, reply) => { const unauthorized = new Error('Unauthorized') @@ -873,7 +873,7 @@ test('It should not return a fastify `FST_ERR_REP_ALREADY_SENT - Reply already s { config: { fail: true } }, - async (request, reply) => { + async () => { return { message: 'unreachable' } } ) @@ -934,7 +934,7 @@ test('It should forward `helmet` errors to `fastify-helmet`', async (t) => { } }) - fastify.get('/', async (request, reply) => { + fastify.get('/', async () => { return { message: 'ok' } }) @@ -981,14 +981,14 @@ test('It should be able to catch `helmet` errors with a fastify `onError` hook', } }) - fastify.addHook('onError', async (request, reply, error) => { + fastify.addHook('onError', async (_request, _reply, error) => { if (error) { errorDetected.push(error) t.assert.ok(error) } }) - fastify.get('/', async (request, reply) => { + fastify.get('/', async () => { return { message: 'ok' } }) diff --git a/test/routes.test.js b/test/routes.test.js index ddfafd9..0535f41 100644 --- a/test/routes.test.js +++ b/test/routes.test.js @@ -10,7 +10,7 @@ test('It should apply route specific helmet options over the global options', as const fastify = Fastify() await fastify.register(helmet, { global: true }) - fastify.get('/', { helmet: { frameguard: false } }, (request, reply) => { + fastify.get('/', { helmet: { frameguard: false } }, (_request, reply) => { reply.send({ hello: 'world' }) }) @@ -50,11 +50,11 @@ test('It should disable helmet on specific route when route `helmet` option is s const fastify = Fastify() await fastify.register(helmet, { global: true }) - fastify.get('/disabled', { helmet: false }, (request, reply) => { + fastify.get('/disabled', { helmet: false }, (_request, reply) => { reply.send({ hello: 'disabled' }) }) - fastify.get('/enabled', (request, reply) => { + fastify.get('/enabled', (_request, reply) => { reply.send({ hello: 'enabled' }) }) @@ -129,7 +129,7 @@ test('It should add CSPNonce decorator and hooks when route `enableCSPNonces` op enableCSPNonces: true } }, - (request, reply) => { + (_request, reply) => { t.assert.ok(reply.cspNonce) reply.send(reply.cspNonce) } @@ -160,7 +160,7 @@ test('It should add CSPNonce decorator and hooks with default options when route enableCSPNonces: false }) - fastify.get('/no-csp', (request, reply) => { + fastify.get('/no-csp', (_request, reply) => { t.assert.equal(reply.cspNonce, null) reply.send({ message: 'no csp' }) }) @@ -172,7 +172,7 @@ test('It should add CSPNonce decorator and hooks with default options when route enableCSPNonces: true } }, - (request, reply) => { + (_request, reply) => { t.assert.ok(reply.cspNonce) reply.send(reply.cspNonce) } @@ -207,7 +207,7 @@ test('It should not add CSPNonce decorator when route `enableCSPNonces` option i enableCSPNonces: true }) - fastify.get('/with-csp', (request, reply) => { + fastify.get('/with-csp', (_request, reply) => { t.assert.ok(reply.cspNonce) reply.send(reply.cspNonce) }) @@ -215,7 +215,7 @@ test('It should not add CSPNonce decorator when route `enableCSPNonces` option i fastify.get( '/no-csp', { helmet: { enableCSPNonces: false } }, - (request, reply) => { + (_request, reply) => { t.assert.equal(reply.cspNonce, null) reply.send({ message: 'no csp' }) } @@ -267,7 +267,7 @@ test('It should not set default directives when route useDefaults is set to `fal } } }, - (request, reply) => { + (_request, reply) => { reply.send({ hello: 'world' }) } ) @@ -306,7 +306,7 @@ test('It should not set `content-security-policy` header, if route contentSecuri contentSecurityPolicy: false } }, - (request, reply) => { + (_request, reply) => { reply.send({ hello: 'world' }) } ) @@ -403,7 +403,7 @@ test('It should throw an error when route specific helmet options are of an inva await fastify.register(helmet) try { - fastify.get('/', { helmet: 'invalid_options' }, (request, reply) => { + fastify.get('/', { helmet: 'invalid_options' }, () => { return { message: 'ok' } }) } catch (error) { @@ -421,7 +421,7 @@ test('It should forward `helmet` reply decorator and route specific errors to `f const fastify = Fastify() await fastify.register(helmet, { global: false }) - fastify.get('/helmet-reply-decorator-error', async (request, reply) => { + fastify.get('/helmet-reply-decorator-error', async (_request, reply) => { await reply.helmet({ contentSecurityPolicy: { directives: { @@ -444,7 +444,7 @@ test('It should forward `helmet` reply decorator and route specific errors to `f } } }, - async (request, reply) => { + async () => { return { message: 'ok' } } ) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 6bd6218..aa9a6ca 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -67,7 +67,7 @@ appFour.register(fastifyHelmet, { const appFive = fastify() appFive.register(fastifyHelmet, { enableCSPNonces: true }) -appFive.get('/', function (request, reply) { +appFive.get('/', function (_request, reply) { expectType<{ script: string; style: string; @@ -86,7 +86,7 @@ appSix.register(fastifyHelmet, { } }) -appSix.get('/', function (request, reply) { +appSix.get('/', function (_request, reply) { expectType<{ script: string; style: string; @@ -100,14 +100,14 @@ expectType(csp) const appSeven = fastify() appSeven.register(fastifyHelmet, { global: true }) -appSeven.get('/route-with-disabled-helmet', { helmet: false }, function (request, reply) { +appSeven.get('/route-with-disabled-helmet', { helmet: false }, function (_request, reply) { expectType(reply.helmet()) }) expectError( appSeven.get('/route-with-disabled-helmet', { helmet: 'trigger a typescript error' - }, function (request, reply) { + }, function (_request, reply) { expectType(reply.helmet()) }) ) @@ -116,7 +116,7 @@ expectError( const appEight = fastify() appEight.register(fastifyHelmet, { global: false }) -appEight.get('/disabled-helmet', function (request, reply) { +appEight.get('/disabled-helmet', function (_request, reply) { expectType(reply.helmet(helmetOptions)) }) @@ -150,7 +150,7 @@ const routeHelmetOptions = { } expectAssignable(routeHelmetOptions) -appEight.get('/enabled-helmet', routeHelmetOptions, function (request, reply) { +appEight.get('/enabled-helmet', routeHelmetOptions, function (_request, reply) { expectType(reply.helmet()) expectType<{ script: string; @@ -160,7 +160,7 @@ appEight.get('/enabled-helmet', routeHelmetOptions, function (request, reply) { appEight.get('/enable-framegard', { helmet: { frameguard: true } -}, function (request, reply) { +}, function (_request, reply) { expectType(reply.helmet()) expectType<{ script: string;