From e2545ea99916e7781c1d198976e4e0272e03160c Mon Sep 17 00:00:00 2001 From: Jelenkee Date: Wed, 14 Apr 2021 17:48:49 +0200 Subject: [PATCH] -fixed decorator for multiple roots --- index.js | 2 +- test/static.test.js | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index b22a4f99..2b1c239b 100644 --- a/index.js +++ b/index.js @@ -162,7 +162,7 @@ async function fastifyStatic (fastify, opts) { if (opts.decorateReply !== false) { fastify.decorateReply('sendFile', function (filePath, rootPath) { - pumpSendToReply(this.request, this, filePath, rootPath) + pumpSendToReply(this.request, this, filePath, rootPath || sendOptions.root) return this }) diff --git a/test/static.test.js b/test/static.test.js index 9d5808c3..18c44828 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -22,6 +22,7 @@ const index2Content = fs.readFileSync('./test/static2/index.html').toString('utf const foobarContent = fs.readFileSync('./test/static/foobar.html').toString('utf8') const deepContent = fs.readFileSync('./test/static/deep/path/for/test/purpose/foo.html').toString('utf8') const innerIndex = fs.readFileSync('./test/static/deep/path/for/test/index.html').toString('utf8') +const fooContent = fs.readFileSync('./test/static/foo.html').toString('utf8') const barContent = fs.readFileSync('./test/static2/bar.html').toString('utf8') const GENERIC_RESPONSE_CHECK_COUNT = 5 @@ -491,7 +492,7 @@ t.test('register /static/', t => { }) t.test('register /static and /static2', t => { - t.plan(3) + t.plan(5) const pluginOptions = { root: [path.join(__dirname, '/static'), path.join(__dirname, '/static2')], @@ -500,6 +501,14 @@ t.test('register /static and /static2', t => { const fastify = Fastify() fastify.register(fastifyStatic, pluginOptions) + fastify.get('/foo', (req, rep) => { + rep.sendFile('foo.html') + }) + + fastify.get('/bar', (req, rep) => { + rep.sendFile('bar.html') + }) + t.tearDown(fastify.close.bind(fastify)) fastify.listen(0, err => { @@ -533,6 +542,32 @@ t.test('register /static and /static2', t => { genericResponseChecks(t, response) }) }) + + t.test('sendFile foo.html', t => { + t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT) + simple.concat({ + method: 'GET', + url: 'http://localhost:' + fastify.server.address().port + '/foo' + }, (err, response, body) => { + t.error(err) + t.strictEqual(response.statusCode, 200) + t.strictEqual(body.toString(), fooContent) + genericResponseChecks(t, response) + }) + }) + + t.test('sendFile bar.html', t => { + t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT) + simple.concat({ + method: 'GET', + url: 'http://localhost:' + fastify.server.address().port + '/bar' + }, (err, response, body) => { + t.error(err) + t.strictEqual(response.statusCode, 200) + t.strictEqual(body.toString(), barContent) + genericResponseChecks(t, response) + }) + }) }) })