Skip to content

Commit

Permalink
fix: wildcard head return content-length header (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 committed Apr 4, 2024
1 parent 793188d commit 85f0831
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
9 changes: 7 additions & 2 deletions index.js
Expand Up @@ -111,8 +111,13 @@ async function fastifyStatic (fastify, opts) {
throw new Error('"wildcard" option must be a boolean')
}
if (opts.wildcard === undefined || opts.wildcard === true) {
fastify.get(prefix + '*', { ...routeOpts, exposeHeadRoute: true }, (req, reply) => {
pumpSendToReply(req, reply, '/' + req.params['*'], sendOptions.root)
fastify.route({
...routeOpts,
method: ['HEAD', 'GET'],
path: prefix + '*',
handler (req, reply) {
pumpSendToReply(req, reply, '/' + req.params['*'], sendOptions.root)
}
})
if (opts.redirect === true && prefix !== opts.prefix) {
fastify.get(opts.prefix, routeOpts, (req, reply) => {
Expand Down
44 changes: 39 additions & 5 deletions test/static.test.js
Expand Up @@ -1740,7 +1740,7 @@ t.test('with fastify-compress', t => {
})
})
t.test('register /static/ with schemaHide true', t => {
t.plan(4)
t.plan(3)

const pluginOptions = {
root: path.join(__dirname, '/static'),
Expand Down Expand Up @@ -1780,7 +1780,7 @@ t.test('register /static/ with schemaHide true', t => {
})

t.test('register /static/ with schemaHide false', t => {
t.plan(4)
t.plan(3)

const pluginOptions = {
root: path.join(__dirname, '/static'),
Expand Down Expand Up @@ -1820,7 +1820,7 @@ t.test('register /static/ with schemaHide false', t => {
})

t.test('register /static/ without schemaHide', t => {
t.plan(4)
t.plan(3)

const pluginOptions = {
root: path.join(__dirname, '/static'),
Expand Down Expand Up @@ -2957,7 +2957,7 @@ t.test('inject support', async (t) => {
})

t.test('routes should use custom errorHandler premature stream close', t => {
t.plan(4)
t.plan(3)

const pluginOptions = {
root: path.join(__dirname, '/static'),
Expand Down Expand Up @@ -2992,7 +2992,7 @@ t.test('routes should use custom errorHandler premature stream close', t => {
})

t.test('routes should fallback to default errorHandler', t => {
t.plan(4)
t.plan(3)

const pluginOptions = {
root: path.join(__dirname, '/static'),
Expand Down Expand Up @@ -3962,3 +3962,37 @@ t.test(
t.same(response2.body, aContent)
}
)

t.test('content-length in head route should not return zero when using wildcard', t => {
t.plan(6)

const pluginOptions = {
root: path.join(__dirname, '/static')
}
const fastify = Fastify()

fastify.register(fastifyStatic, pluginOptions)

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

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

fastify.server.unref()

const file = fs.readFileSync(path.join(__dirname, '/static/index.html'))
const contentLength = Buffer.byteLength(file).toString()

simple.concat({
method: 'HEAD',
url: 'http://localhost:' + fastify.server.address().port + '/index.html',
followRedirect: false
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(response.headers['content-type'], 'text/html; charset=UTF-8')
t.equal(response.headers['content-length'], contentLength)
t.equal(body.toString(), '')
})
})
})

0 comments on commit 85f0831

Please sign in to comment.