Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Feb 1, 2024
1 parent f09d85a commit 4a3a40e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
87 changes: 87 additions & 0 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 4a3a40e

Please sign in to comment.