Skip to content

Commit

Permalink
changed the object schema paramter to boolean flag schemaHide
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Balduzzi committed May 3, 2018
1 parent 572bfa9 commit b01d4db
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ Default: `'/'`

A URL path prefix used to create a virtual mount path for the static directory.

#### `schema`
#### `schemaHide`

Default: `{}`
Default: `true`

An object that spececify the fastify schema route for all routes.
A flag that define if the fastify route hide-schema attribute is hidden or not

#### `setHeaders`

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ 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
const schema = { schema: { hide: typeof opts.schemaHide !== 'undefined' ? opts.schemaHide : true } } // Set the schema hide property if defined in opts or true by default

fastify.get(prefix + '*', schema, function (req, reply) {
pumpSendToReply(req, reply, '/' + req.params['*'])
Expand Down
47 changes: 43 additions & 4 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,13 +716,13 @@ t.test('with fastify-compress', t => {
})
})

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

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

const fastify = Fastify()
Expand Down Expand Up @@ -755,7 +755,46 @@ t.test('register /static/ with schema', t => {
})
})

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

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

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

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

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 schemaHide', t => {
t.plan(3)

const pluginOptions = {
Expand All @@ -767,7 +806,7 @@ t.test('register /static/ without schema', t => {
fastify.register(fastifyStatic, pluginOptions)

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

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

0 comments on commit b01d4db

Please sign in to comment.