diff --git a/index.js b/index.js index 7249882..8dec5d9 100644 --- a/index.js +++ b/index.js @@ -429,11 +429,11 @@ function findIndexFile (pathname, root, indexFiles = ['index.html']) { }) } -// Adapted from https://github.com/fastify/fastify-compress/blob/fa5c12a5394285c86d9f438cb39ff44f3d5cde79/index.js#L442 +// Adapted from https://github.com/fastify/fastify-compress/blob/665e132fa63d3bf05ad37df3c20346660b71a857/index.js#L451 function getEncodingHeader (headers, checked) { if (!('accept-encoding' in headers)) return - const header = headers['accept-encoding'].toLowerCase().replace('*', 'gzip') + const header = headers['accept-encoding'].toLowerCase().replace(/\*/g, 'gzip') return encodingNegotiator.negotiate( header, supportedEncodings.filter((enc) => !checked.has(enc)) diff --git a/test/static.test.js b/test/static.test.js index 67335ed..078fc1f 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -35,6 +35,9 @@ const innerIndex = fs const allThreeBr = fs.readFileSync( './test/static-pre-compressed/all-three.html.br' ) +const allThreeGzip = fs.readFileSync( + './test/static-pre-compressed/all-three.html.gz' +) const gzipOnly = fs.readFileSync( './test/static-pre-compressed/gzip-only.html.gz' ) @@ -3015,6 +3018,66 @@ t.test( } ) +t.test( + 'will serve pre-compressed files with .gzip if * directive used', + async (t) => { + const pluginOptions = { + root: path.join(__dirname, '/static-pre-compressed'), + prefix: '/static-pre-compressed/', + preCompressed: true + } + + const fastify = Fastify() + + fastify.register(fastifyStatic, pluginOptions) + t.teardown(fastify.close.bind(fastify)) + + const response = await fastify.inject({ + method: 'GET', + url: '/static-pre-compressed/all-three.html', + headers: { + 'accept-encoding': '*' + } + }) + + genericResponseChecks(t, response) + t.equal(response.headers['content-encoding'], 'gzip') + t.equal(response.statusCode, 200) + t.same(response.rawPayload, allThreeGzip) + t.end() + } +) + +t.test( + 'will serve pre-compressed files with .gzip if multiple * directives used', + async (t) => { + const pluginOptions = { + root: path.join(__dirname, '/static-pre-compressed'), + prefix: '/static-pre-compressed/', + preCompressed: true + } + + const fastify = Fastify() + + fastify.register(fastifyStatic, pluginOptions) + t.teardown(fastify.close.bind(fastify)) + + const response = await fastify.inject({ + method: 'GET', + url: '/static-pre-compressed/all-three.html', + headers: { + 'accept-encoding': '*, *' + } + }) + + genericResponseChecks(t, response) + t.equal(response.headers['content-encoding'], 'gzip') + t.equal(response.statusCode, 200) + t.same(response.rawPayload, allThreeGzip) + t.end() + } +) + t.test( 'will serve uncompressed files if there are no compressed variants on disk', async (t) => { @@ -3077,7 +3140,7 @@ t.test( ) t.test( - 'will serve pre-compressed files and fallback to .gz if .br is not on disk (with wildcard: false) ', + 'will serve pre-compressed files and fallback to .gz if .br is not on disk (with wildcard: false)', async (t) => { const pluginOptions = { root: path.join(__dirname, '/static-pre-compressed'), @@ -3108,7 +3171,69 @@ t.test( ) t.test( - 'will serve uncompressed files if there are no compressed variants on disk (with wildcard: false)', + 'will serve pre-compressed files with .gzip if * directive used (with wildcard: false)', + async (t) => { + const pluginOptions = { + root: path.join(__dirname, '/static-pre-compressed'), + prefix: '/static-pre-compressed/', + preCompressed: true, + wildcard: false + } + + const fastify = Fastify() + + fastify.register(fastifyStatic, pluginOptions) + t.teardown(fastify.close.bind(fastify)) + + const response = await fastify.inject({ + method: 'GET', + url: '/static-pre-compressed/all-three.html', + headers: { + 'accept-encoding': '*' + } + }) + + genericResponseChecks(t, response) + t.equal(response.headers['content-encoding'], 'gzip') + t.equal(response.statusCode, 200) + t.same(response.rawPayload, allThreeGzip) + t.end() + } +) + +t.test( + 'will serve pre-compressed files with .gzip if multiple * directives used (with wildcard: false)', + async (t) => { + const pluginOptions = { + root: path.join(__dirname, '/static-pre-compressed'), + prefix: '/static-pre-compressed/', + preCompressed: true, + wildcard: false + } + + const fastify = Fastify() + + fastify.register(fastifyStatic, pluginOptions) + t.teardown(fastify.close.bind(fastify)) + + const response = await fastify.inject({ + method: 'GET', + url: '/static-pre-compressed/all-three.html', + headers: { + 'accept-encoding': '*, *' + } + }) + + genericResponseChecks(t, response) + t.equal(response.headers['content-encoding'], 'gzip') + t.equal(response.statusCode, 200) + t.same(response.rawPayload, allThreeGzip) + t.end() + } +) + +t.test( + 'will serve uncompressed files if there are no compressed variants on disk (with wildcard: false)', async (t) => { const pluginOptions = { root: path.join(__dirname, '/static-pre-compressed'),