Skip to content

Commit 6735951

Browse files
committed
test: verify symlinks and wildcard
1 parent 0b5cff9 commit 6735951

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

test/static.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3906,3 +3906,78 @@ test('register with wildcard false and globIgnore', async t => {
39063906
await response.text()
39073907
})
39083908
})
3909+
3910+
test('should serve file from a symlinked directory', async (t) => {
3911+
t.plan(1)
3912+
3913+
const tempDir = fs.mkdtempSync(path.join(__dirname, 'symlink-test-'))
3914+
const realDir = path.join(tempDir, 'real')
3915+
const symlinkedDir = path.join(tempDir, 'symlinked')
3916+
const testFilePath = path.join(realDir, 'test.txt')
3917+
const fileContent = 'symlink test file'
3918+
3919+
fs.mkdirSync(realDir)
3920+
fs.writeFileSync(testFilePath, fileContent)
3921+
fs.symlinkSync(realDir, symlinkedDir, 'dir')
3922+
3923+
const fastify = Fastify()
3924+
fastify.register(fastifyStatic, {
3925+
root: tempDir,
3926+
prefix: '/',
3927+
followSymlinks: true
3928+
})
3929+
3930+
t.after(async () => {
3931+
await fastify.close()
3932+
fs.rmSync(tempDir, { recursive: true, force: true })
3933+
})
3934+
3935+
await fastify.listen({ port: 0 })
3936+
fastify.server.unref()
3937+
3938+
await t.test('request file via symlink', async (t) => {
3939+
t.plan(3)
3940+
const response = await fetch(`http://localhost:${fastify.server.address().port}/symlinked/test.txt`)
3941+
t.assert.ok(response.ok)
3942+
t.assert.deepStrictEqual(response.status, 200)
3943+
t.assert.deepStrictEqual(await response.text(), fileContent)
3944+
})
3945+
})
3946+
3947+
test('should serve file with user-defined wildcard route', async (t) => {
3948+
t.plan(1)
3949+
3950+
const tempDir = fs.mkdtempSync(path.join(__dirname, 'wildcard-sendFile-test-'))
3951+
const testFilePath = path.join(tempDir, 'foo', 'bar.txt')
3952+
const fileContent = 'wildcard sendFile test'
3953+
3954+
fs.mkdirSync(path.dirname(testFilePath), { recursive: true })
3955+
fs.writeFileSync(testFilePath, fileContent)
3956+
3957+
const fastify = Fastify()
3958+
// The root needs to be specified for reply.sendFile to resolve relative paths
3959+
fastify.register(fastifyStatic, {
3960+
root: tempDir,
3961+
serve: false // We don't want the plugin to serve files, just decorate reply.sendFile
3962+
})
3963+
3964+
fastify.get('/files/*', (req, reply) => {
3965+
reply.sendFile(req.params['*'])
3966+
})
3967+
3968+
t.after(async () => {
3969+
await fastify.close()
3970+
fs.rmSync(tempDir, { recursive: true, force: true })
3971+
})
3972+
3973+
await fastify.listen({ port: 0 })
3974+
fastify.server.unref()
3975+
3976+
await t.test('request file via wildcard route', async (t) => {
3977+
t.plan(3)
3978+
const response = await fetch(`http://localhost:${fastify.server.address().port}/files/foo/bar.txt`)
3979+
t.assert.ok(response.ok)
3980+
t.assert.deepStrictEqual(response.status, 200)
3981+
t.assert.deepStrictEqual(await response.text(), fileContent)
3982+
})
3983+
})

0 commit comments

Comments
 (0)