From 4a3a40e91cca2da476d5f26a507de3e60af1eb98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Thu, 1 Feb 2024 20:20:40 +0100 Subject: [PATCH] add tests --- index.js | 4 +-- test/static.test.js | 87 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index fd09a17..a3e154e 100644 --- a/index.js +++ b/index.js @@ -440,9 +440,7 @@ async function fastifyStatic (fastify, opts) { function getHashedAssetPath (unhashedRelativePath) { const hashedRelativePath = fileHashes.get(unhashedRelativePath) - if (hashedRelativePath) { - return path.posix.join(prefix, hashedRelativePath) - } + return path.posix.join(prefix, hashedRelativePath) } } diff --git a/test/static.test.js b/test/static.test.js index a42d9dd..cfa3c53 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -374,6 +374,93 @@ t.test('register /static', (t) => { }) }) +t.test('register /static with hash', async (t) => { + const pluginOptions = { + root: path.join(__dirname, '/static'), + prefix: '/static/', + hash: true, + // hashSkip: ['foo.html'], + wildcard: false + } + const fastify = Fastify() + await fastify.register(fastifyStatic, pluginOptions) + + t.teardown(fastify.close.bind(fastify)) + + fastify.listen({ port: 0 }, async (err) => { + t.error(err) + + fastify.server.unref() + + await t.test('/static/index.html', async (t) => { + t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT) + simple.concat({ + method: 'GET', + url: 'http://localhost:' + fastify.server.address().port + fastify.getHashedAssetPath('index.html') + }, (err, response, body) => { + t.error(err) + t.equal(response.statusCode, 200) + t.equal(body.toString(), indexContent) + genericResponseChecks(t, response) + }) + }) + }) +}) + +t.test('register /static with hash', async (t) => { + const pluginOptions = { + root: [path.join(__dirname, '/static/')], + prefix: '/static/', + hash: true, + hashSkip: ['foo.html'], + wildcard: false + } + const fastify = Fastify() + await fastify.register(fastifyStatic, pluginOptions) + + t.teardown(fastify.close.bind(fastify)) + + fastify.listen({ port: 0 }, async (err) => { + t.error(err) + + fastify.server.unref() + + await t.test('/static/index.html', async (t) => { + t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT) + simple.concat({ + method: 'GET', + url: 'http://localhost:' + fastify.server.address().port + fastify.getHashedAssetPath('index.html') + }, (err, response, body) => { + t.error(err) + t.equal(response.statusCode, 200) + t.equal(body.toString(), indexContent) + genericResponseChecks(t, response) + }) + }) + }) +}) + +t.test('register /static with hash (incorrect)', async (t) => { + const pluginOptions = { + root: path.join(__dirname, '/static'), + prefix: '/static/', + hash: true + } + const fastify = Fastify() + try { + await fastify.register(fastifyStatic, pluginOptions) + } catch (error) { + t.ok(error instanceof Error) + } + + t.teardown(fastify.close.bind(fastify)) + + fastify.listen({ port: 0 }, async (err) => { + t.error(err) + fastify.server.unref() + }) +}) + t.test('register /static/', t => { t.plan(12)