Skip to content

Commit

Permalink
test added
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Balduzzi committed May 3, 2018
1 parent 565b1f8 commit 1158525
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ function fastifyStatic (fastify, opts, next) {

if (opts.prefix === undefined) opts.prefix = '/'
const prefix = opts.prefix[opts.prefix.length - 1] === '/' ? opts.prefix : (opts.prefix + '/')
const schema = opts.schema ? { schema: opts.schema } : {} // Perform the check if set a schema or as an empty object

fastify.get(prefix + '*', { schema: { hide: true } }, function (req, reply) {
fastify.get(prefix + '*', schema, function (req, reply) {
pumpSendToReply(req, reply, '/' + req.params['*'])
})

Expand Down
77 changes: 77 additions & 0 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -715,3 +715,80 @@ t.test('with fastify-compress', t => {
})
})
})

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

const pluginOptions = {
root: path.join(__dirname, '/static'),
prefix: '/static/',
schema: { hide: true }
}

const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)

fastify.addHook('onRoute', function (routeOptions) {
t.deepEqual(routeOptions.schema, { hide: true })
})

t.tearDown(fastify.close.bind(fastify))

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

fastify.server.unref()

t.test('/static/index.html', t => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)

simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body.toString(), indexContent)
genericResponseChecks(t, response)
})
})
})
})

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

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

const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)

fastify.addHook('onRoute', function (routeOptions) {
t.deepEqual(routeOptions.schema, null)
})

t.tearDown(fastify.close.bind(fastify))

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

fastify.server.unref()

t.test('/static/index.html', t => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)

simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body.toString(), indexContent)
genericResponseChecks(t, response)
})
})
})
})

0 comments on commit 1158525

Please sign in to comment.