diff --git a/test/dir-list.test.js b/test/dir-list.test.js index 5c4da834..a6af123f 100644 --- a/test/dir-list.test.js +++ b/test/dir-list.test.js @@ -23,7 +23,7 @@ const helper = { try { fs.mkdirSync(path.join(__dirname, 'static/shallow/empty')) -} catch (error) {} +} catch {} test('throws when `root` is an array', t => { t.plan(2) @@ -249,7 +249,7 @@ test('dir list href nested structure', async t => { list: { format: 'html', names: ['index', 'index.htm'], - render (dirs, files) { + render (dirs) { return dirs[0].href } } @@ -298,8 +298,7 @@ test('dir list html format - stats', async t => { t.assert.ok(files.every(every)) function every (value) { - return value.stats && - value.stats.atime && + return value.stats?.atime && !value.extendedInfo } } @@ -327,7 +326,7 @@ test('dir list html format - extended info', async t => { list: { format: 'html', extendedFolderInfo: true, - render (dirs, files) { + render (dirs) { test('dirs', t => { t.plan(dirs.length * 7) @@ -425,7 +424,7 @@ test('json format with url parameter format', async t => { index: false, list: { format: 'json', - render (dirs, files) { + render () { return 'html' } } @@ -497,7 +496,7 @@ test('html format with url parameter format', async t => { index: false, list: { format: 'html', - render (dirs, files) { + render () { return 'html' } } @@ -718,12 +717,12 @@ test('dir list error', async t => { dirList.send = async () => { throw new Error(errorMessage) } t.beforeEach((ctx) => { - ctx['initialDirList'] = ctx['../lib/dirList.js'] + ctx.initialDirList = ctx['../lib/dirList.js'] ctx['../lib/dirList.js'] = dirList }) t.afterEach((ctx) => { - ctx['../lib/dirList.js'] = ctx['initialDirList'] + ctx['../lib/dirList.js'] = ctx.initialDirList }) const routes = ['/public/', '/public/index.htm'] diff --git a/test/static.test.js b/test/static.test.js index 2e6a7254..a425abc8 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -462,11 +462,11 @@ test('register /static and /static2', async (t) => { const fastify = Fastify() fastify.register(fastifyStatic, pluginOptions) - fastify.get('/foo', (req, rep) => { + fastify.get('/foo', (_req, rep) => { rep.sendFile('foo.html') }) - fastify.get('/bar', (req, rep) => { + fastify.get('/bar', (_req, rep) => { rep.sendFile('bar.html') }) @@ -575,7 +575,7 @@ test('payload.path is set', async (t) => { const fastify = Fastify() let gotFilename fastify.register(fastifyStatic, pluginOptions) - fastify.addHook('onSend', function (req, reply, payload, next) { + fastify.addHook('onSend', function (_req, _reply, payload, next) { gotFilename = payload.path next() }) @@ -617,7 +617,7 @@ test('error responses can be customized with fastify.setErrorHandler()', async t } const fastify = Fastify() - fastify.setErrorHandler(function errorHandler (err, request, reply) { + fastify.setErrorHandler(function errorHandler (err, _request, reply) { reply.code(403).type('text/plain').send(`${err.statusCode} Custom error message`) }) @@ -724,7 +724,7 @@ test('serving disabled', async (t) => { t.after(() => fastify.close()) - fastify.get('/foo/bar', (request, reply) => { + fastify.get('/foo/bar', (_request, reply) => { reply.sendFile('index.html') }) @@ -766,18 +766,18 @@ test('sendFile', async (t) => { t.after(() => fastify.close()) - fastify.get('/foo/bar', function (req, reply) { + fastify.get('/foo/bar', function (_req, reply) { reply.sendFile('/index.html') }) - fastify.get('/root/path/override/test', (request, reply) => { + fastify.get('/root/path/override/test', (_request, reply) => { reply.sendFile( '/foo.html', path.join(__dirname, 'static', 'deep', 'path', 'for', 'test', 'purpose') ) }) - fastify.get('/foo/bar/options/override/test', function (req, reply) { + fastify.get('/foo/bar/options/override/test', function (_req, reply) { reply.sendFile('/index.html', { maxAge }) }) @@ -838,7 +838,7 @@ test('sendFile disabled', async (t) => { t.after(() => fastify.close()) - fastify.get('/foo/bar', function (req, reply) { + fastify.get('/foo/bar', function (_req, reply) { if (reply.sendFile === undefined) { reply.send('pass') } else { @@ -898,7 +898,7 @@ test('allowedPath option - request', async (t) => { const pluginOptions = { root: path.join(__dirname, '/static'), - allowedPath: (pathName, root, request) => request.query.key === 'temporaryKey' + allowedPath: (_pathName, _root, request) => request.query.key === 'temporaryKey' } const fastify = Fastify() fastify.register(fastifyStatic, pluginOptions) @@ -939,26 +939,26 @@ test('download', async (t) => { t.after(() => fastify.close()) - fastify.get('/foo/bar', function (req, reply) { + fastify.get('/foo/bar', function (_req, reply) { reply.download('/index.html') }) - fastify.get('/foo/bar/change', function (req, reply) { + fastify.get('/foo/bar/change', function (_req, reply) { reply.download('/index.html', 'hello-world.html') }) - fastify.get('/foo/bar/override', function (req, reply) { + fastify.get('/foo/bar/override', function (_req, reply) { reply.download('/index.html', 'hello-world.html', { maxAge: '2 hours', immutable: true }) }) - fastify.get('/foo/bar/override/2', function (req, reply) { + fastify.get('/foo/bar/override/2', function (_req, reply) { reply.download('/index.html', { acceptRanges: false }) }) - fastify.get('/root/path/override/test', (request, reply) => { + fastify.get('/root/path/override/test', (_request, reply) => { reply.download('/foo.html', { root: path.join( __dirname, @@ -972,7 +972,7 @@ test('download', async (t) => { }) }) - fastify.get('/root/path/override/test/change', (request, reply) => { + fastify.get('/root/path/override/test/change', (_request, reply) => { reply.download('/foo.html', 'hello-world.html', { root: path.join( __dirname, @@ -1070,7 +1070,7 @@ test('download disabled', async (t) => { const fastify = Fastify() fastify.register(fastifyStatic, pluginOptions) - fastify.get('/foo/bar', function (req, reply) { + fastify.get('/foo/bar', function (_req, reply) { if (reply.download === undefined) { t.assert.deepStrictEqual(reply.download, undefined) reply.send('pass') @@ -1141,7 +1141,7 @@ test('send options', (t) => { const fastify = Fastify({ logger: false }) const { resolve, promise } = Promise.withResolvers() const fastifyStatic = require('proxyquire')('../', { - '@fastify/send': function sendStub (req, pathName, options) { + '@fastify/send': function sendStub (_req, pathName, options) { t.assert.deepStrictEqual(pathName, '/index.html') t.assert.deepStrictEqual(options.root, path.join(__dirname, '/static')) t.assert.deepStrictEqual(options.acceptRanges, 'acceptRanges') @@ -1306,7 +1306,7 @@ test('register no prefix', async (t) => { const fastify = Fastify() fastify.register(fastifyStatic, pluginOptions) - fastify.get('/', (request, reply) => { + fastify.get('/', (_request, reply) => { reply.send({ hello: 'world' }) }) @@ -1559,7 +1559,7 @@ test('register with wildcard false', async t => { const fastify = Fastify() fastify.register(fastifyStatic, pluginOptions) - fastify.get('/*', (request, reply) => { + fastify.get('/*', (_request, reply) => { reply.send({ hello: 'world' }) }) @@ -1661,7 +1661,7 @@ test('register with wildcard false (trailing slash in the root)', async t => { }) fastify.register(fastifyStatic, pluginOptions) - fastify.get('/*', (request, reply) => { + fastify.get('/*', (_request, reply) => { reply.send({ hello: 'world' }) }) @@ -1729,7 +1729,7 @@ test('register with wildcard string', async (t) => { const fastify = Fastify() fastify.register(fastifyStatic, pluginOptions) - fastify.get('/*', (request, reply) => { + fastify.get('/*', (_request, reply) => { reply.send({ hello: 'world' }) }) @@ -1746,7 +1746,7 @@ test('register with wildcard string on multiple root paths', async (t) => { const fastify = Fastify() fastify.register(fastifyStatic, pluginOptions) - fastify.get('/*', (request, reply) => { + fastify.get('/*', (_request, reply) => { reply.send({ hello: 'world' }) }) @@ -1766,7 +1766,7 @@ test('register with wildcard false and alternative index', async t => { const fastify = Fastify() fastify.register(fastifyStatic, pluginOptions) - fastify.get('/*', (request, reply) => { + fastify.get('/*', (_request, reply) => { reply.send({ hello: 'world' }) }) @@ -1890,7 +1890,7 @@ test('register /static with wildcard false and alternative index', async t => { const fastify = Fastify() fastify.register(fastifyStatic, pluginOptions) - fastify.get('/*', (request, reply) => { + fastify.get('/*', (_request, reply) => { reply.send({ hello: 'world' }) }) @@ -2389,7 +2389,7 @@ test('if dotfiles are properly served according to plugin options', async (t) => test('register with failing glob handler', async (t) => { const fastifyStatic = proxyquire.noCallThru()('../', { - glob: function globStub (pattern, options, cb) { + glob: function globStub (_pattern, _options, cb) { process.nextTick(function () { return cb(new Error('mock glob error')) }) @@ -2414,7 +2414,7 @@ test( async (t) => { const fastifyStatic = proxyquire('../', { 'node:fs': { - statSync: function statSyncStub (path) { + statSync: function statSyncStub () { throw new Error({ code: 'MOCK' }) } } @@ -2463,7 +2463,7 @@ test('routes should use custom errorHandler premature stream close', async t => fastify.addHook('onRoute', function (routeOptions) { t.assert.ok(routeOptions.errorHandler instanceof Function) - routeOptions.onRequest = (request, reply, done) => { + routeOptions.onRequest = (_request, _reply, done) => { const fakeError = new Error() fakeError.code = 'ERR_STREAM_PREMATURE_CLOSE' done(fakeError) @@ -2489,7 +2489,7 @@ test('routes should fallback to default errorHandler', async t => { fastify.addHook('onRoute', function (routeOptions) { t.assert.ok(routeOptions.errorHandler instanceof Function) - routeOptions.preHandler = (request, reply, done) => { + routeOptions.preHandler = (_request, _reply, done) => { const fakeError = new Error() fakeError.code = 'SOMETHING_ELSE' done(fakeError) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index b91705da..f762b73d 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -62,7 +62,7 @@ const options: FastifyStaticOptions = { expectType(stat) }, preCompressed: false, - allowedPath: (pathName: string, root: string, request: FastifyRequest) => { + allowedPath: (_pathName: string, _root: string, _request: FastifyRequest) => { return true }, constraints: { @@ -121,7 +121,7 @@ expectAssignable({ appWithImplicitHttp .register(fastifyStatic, options) .after(() => { - appWithImplicitHttp.get('/', (request, reply) => { + appWithImplicitHttp.get('/', (_request, reply) => { reply.sendFile('some-file-name') }) }) @@ -131,23 +131,23 @@ const appWithHttp2 = fastify({ http2: true }) appWithHttp2 .register(fastifyStatic, options) .after(() => { - appWithHttp2.get('/', (request, reply) => { + appWithHttp2.get('/', (_request, reply) => { reply.sendFile('some-file-name') }) - appWithHttp2.get('/download', (request, reply) => { + appWithHttp2.get('/download', (_request, reply) => { reply.download('some-file-name') }) - appWithHttp2.get('/download/1', (request, reply) => { + appWithHttp2.get('/download/1', (_request, reply) => { reply.download('some-file-name', { maxAge: '2 days' }) }) - appWithHttp2.get('/download/2', (request, reply) => { + appWithHttp2.get('/download/2', (_request, reply) => { reply.download('some-file-name', 'some-filename', { cacheControl: false, acceptRanges: true }) }) - appWithHttp2.get('/download/3', (request, reply) => { + appWithHttp2.get('/download/3', (_request, reply) => { reply.download('some-file-name', 'some-filename', { contentType: false }) }) }) @@ -158,35 +158,35 @@ options.root = [''] multiRootAppWithImplicitHttp .register(fastifyStatic, options) .after(() => { - multiRootAppWithImplicitHttp.get('/', (request, reply) => { + multiRootAppWithImplicitHttp.get('/', (_request, reply) => { reply.sendFile('some-file-name') }) - multiRootAppWithImplicitHttp.get('/', (request, reply) => { + multiRootAppWithImplicitHttp.get('/', (_request, reply) => { reply.sendFile('some-file-name', { cacheControl: false, acceptRanges: true }) }) - multiRootAppWithImplicitHttp.get('/', (request, reply) => { + multiRootAppWithImplicitHttp.get('/', (_request, reply) => { reply.sendFile('some-file-name', 'some-root-name', { cacheControl: false, acceptRanges: true }) }) - multiRootAppWithImplicitHttp.get('/', (request, reply) => { + multiRootAppWithImplicitHttp.get('/', (_request, reply) => { reply.sendFile('some-file-name', 'some-root-name-2', { contentType: false }) }) - multiRootAppWithImplicitHttp.get('/download', (request, reply) => { + multiRootAppWithImplicitHttp.get('/download', (_request, reply) => { reply.download('some-file-name') }) - multiRootAppWithImplicitHttp.get('/download/1', (request, reply) => { + multiRootAppWithImplicitHttp.get('/download/1', (_request, reply) => { reply.download('some-file-name', { maxAge: '2 days' }) }) - multiRootAppWithImplicitHttp.get('/download/2', (request, reply) => { + multiRootAppWithImplicitHttp.get('/download/2', (_request, reply) => { reply.download('some-file-name', 'some-filename', { cacheControl: false, acceptRanges: true }) }) - multiRootAppWithImplicitHttp.get('/download/3', (request, reply) => { + multiRootAppWithImplicitHttp.get('/download/3', (_request, reply) => { reply.download('some-file-name', 'some-filename', { contentType: false }) }) }) @@ -198,7 +198,7 @@ options.index = false noIndexApp .register(fastifyStatic, options) .after(() => { - noIndexApp.get('/', (request, reply) => { + noIndexApp.get('/', (_request, reply) => { reply.send('

fastify-static

') }) }) @@ -208,7 +208,7 @@ options.root = new URL('') const URLRootApp = fastify() URLRootApp.register(fastifyStatic, options) .after(() => { - URLRootApp.get('/', (request, reply) => { + URLRootApp.get('/', (_request, reply) => { reply.send('

fastify-static

') }) }) @@ -219,7 +219,7 @@ options.index = 'index.html' defaultIndexApp .register(fastifyStatic, options) .after(() => { - defaultIndexApp.get('/', (request, reply) => { + defaultIndexApp.get('/', (_request, reply) => { reply.send('

fastify-static

') }) })