Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

root made optional if serve is false #475

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ send.mime.default_type = 'application/octet-stream'

async function fastifyStatic (fastify, opts) {
opts.root = normalizeRoot(opts.root)
checkRootPathForErrors(fastify, opts.root)

if (opts.serve !== false) {
checkRootPathForErrors(fastify, opts.root)
}

const setHeaders = opts.setHeaders
if (setHeaders !== undefined && typeof setHeaders !== 'function') {
Expand Down
48 changes: 48 additions & 0 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,54 @@
})
})

t.test('root optional if serving disabled', (t) => {
t.plan(3)

const pluginOptions = {
prefix: '/static/',
serve: false
}
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)

fastify.get('/foo/bar', (request, reply) => {
const file = path.join(__dirname, '/static/index.html');

Check failure on line 915 in test/static.test.js

View workflow job for this annotation

GitHub Actions / test / Lint Code

Extra semicolon
reply.sendFile(file)
})

t.teardown(fastify.close.bind(fastify))

fastify.listen({ port: 0 }, (err) => {
t.error(err)

fastify.server.unref()

t.test('/static/index.html not found', (t) => {
t.plan(2)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 404)
})
})

t.test('/static/index.html via sendFile found', (t) => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/foo/bar'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), indexContent)
genericResponseChecks(t, response)
})
})
})
})

t.test('sendFile', (t) => {
t.plan(5)

Expand Down
Loading