From bd4caaf4f61727a1e6a77f90bf55e8d16dde6289 Mon Sep 17 00:00:00 2001 From: Salman Date: Thu, 15 Oct 2020 19:48:21 +0530 Subject: [PATCH] test: cover errorHandler --- index.js | 15 ++++++--- test/static.test.js | 76 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index c1294477..2ed079cb 100644 --- a/index.js +++ b/index.js @@ -127,14 +127,19 @@ function fastifyStatic (fastify, opts, next) { prefix = opts.prefix[opts.prefix.length - 1] === '/' ? opts.prefix : (opts.prefix + '/') } + const errorHandler = (error, request, reply) => { + if (error && error.code === 'ERR_STREAM_PREMATURE_CLOSE') { + reply.request.raw.destroy() + return + } + + fastify.errorHandler(error, request, reply) + } + // Set the schema hide property if defined in opts or true by default const routeOpts = { schema: { hide: typeof opts.schemaHide !== 'undefined' ? opts.schemaHide : true }, - errorHandler: fastify.errorHandler ? (error, request, reply) => { - if (error && error.code !== 'ERR_STREAM_PREMATURE_CLOSE') { - fastify.errorHandler(error, request, reply) - } - } : undefined + errorHandler: fastify.errorHandler ? errorHandler : undefined } if (opts.decorateReply !== false) { diff --git a/test/static.test.js b/test/static.test.js index 31381015..a3204e11 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -2175,3 +2175,79 @@ t.test('inject support', async (t) => { t.strictEqual(response.statusCode, 200) t.strictEqual(response.body.toString(), indexContent) }) + +t.test('routes should use custom errorHandler premature stream close', t => { + t.plan(3) + + const pluginOptions = { + root: path.join(__dirname, '/static'), + prefix: '/static/' + } + + const fastify = Fastify() + + if (!fastify.errorHandler) { + fastify.errorHandler = (_, __, reply) => { + reply.send('default-error-obj') + } + } + + fastify.addHook('onRoute', function (routeOptions) { + t.ok(routeOptions.errorHandler instanceof Function) + + routeOptions.onRequest = (request, reply, done) => { + const fakeError = new Error() + fakeError.code = 'ERR_STREAM_PREMATURE_CLOSE' + done(fakeError) + } + }) + + fastify.register(fastifyStatic, pluginOptions) + t.tearDown(fastify.close.bind(fastify)) + + fastify.inject({ + method: 'GET', + url: '/static/index.html' + }, (err, response) => { + t.error(err) + t.equal(response, null) + }) +}) + +t.test('routes should fallback to default errorHandler', t => { + t.plan(3) + + const pluginOptions = { + root: path.join(__dirname, '/static'), + prefix: '/static/' + } + + const fastify = Fastify() + + if (!fastify.errorHandler) { + fastify.errorHandler = (_, __, reply) => { + reply.send({ default: 'error-obj' }) + } + } + + fastify.addHook('onRoute', function (routeOptions) { + t.ok(routeOptions.errorHandler instanceof Function) + + routeOptions.preHandler = (request, reply, done) => { + const fakeError = new Error() + fakeError.code = 'SOMETHING_ELSE' + done(fakeError) + } + }) + + fastify.register(fastifyStatic, pluginOptions) + t.tearDown(fastify.close.bind(fastify)) + + fastify.inject({ + method: 'GET', + url: '/static/index.html' + }, (err, response) => { + t.error(err) + t.deepEqual(JSON.parse(response.payload), { default: 'error-obj' }) + }) +})