Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand Down
37 changes: 36 additions & 1 deletion test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')],
Expand All @@ -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 => {
Expand Down Expand Up @@ -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)
})
})
})
})

Expand Down