Skip to content

Commit 4429035

Browse files
authored
Merge pull request #59 from chapuletta/master
schema hide true added to route *
2 parents 67eb63c + 67741fc commit 4429035

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ Default: `'/'`
3838

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

41+
#### `schemaHide`
42+
43+
Default: `true`
44+
45+
A flag that define if the fastify route hide-schema attribute is hidden or not
46+
4147
#### `setHeaders`
4248

4349
Default: `undefined`

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ function fastifyStatic (fastify, opts, next) {
7979

8080
if (opts.prefix === undefined) opts.prefix = '/'
8181
const prefix = opts.prefix[opts.prefix.length - 1] === '/' ? opts.prefix : (opts.prefix + '/')
82+
// Set the schema hide property if defined in opts or true by default
83+
const schema = { schema: { hide: typeof opts.schemaHide !== 'undefined' ? opts.schemaHide : true } }
8284

83-
fastify.get(prefix + '*', function (req, reply) {
85+
fastify.get(prefix + '*', schema, function (req, reply) {
8486
pumpSendToReply(req, reply, '/' + req.params['*'])
8587
})
8688

test/static.test.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,3 +715,122 @@ t.test('with fastify-compress', t => {
715715
})
716716
})
717717
})
718+
719+
t.test('register /static/ with schemaHide true', t => {
720+
t.plan(3)
721+
722+
const pluginOptions = {
723+
root: path.join(__dirname, '/static'),
724+
prefix: '/static/',
725+
schemaHide: true
726+
}
727+
728+
const fastify = Fastify()
729+
730+
fastify.addHook('onRoute', function (routeOptions) {
731+
t.deepEqual(routeOptions.schema, { hide: true })
732+
})
733+
734+
fastify.register(fastifyStatic, pluginOptions)
735+
736+
t.tearDown(fastify.close.bind(fastify))
737+
738+
fastify.listen(0, err => {
739+
t.error(err)
740+
741+
fastify.server.unref()
742+
743+
t.test('/static/index.html', t => {
744+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
745+
746+
simple.concat({
747+
method: 'GET',
748+
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
749+
}, (err, response, body) => {
750+
t.error(err)
751+
t.strictEqual(response.statusCode, 200)
752+
t.strictEqual(body.toString(), indexContent)
753+
genericResponseChecks(t, response)
754+
})
755+
})
756+
})
757+
})
758+
759+
t.test('register /static/ with schemaHide false', t => {
760+
t.plan(3)
761+
762+
const pluginOptions = {
763+
root: path.join(__dirname, '/static'),
764+
prefix: '/static/',
765+
schemaHide: false
766+
}
767+
768+
const fastify = Fastify()
769+
770+
fastify.addHook('onRoute', function (routeOptions) {
771+
t.deepEqual(routeOptions.schema, { hide: false })
772+
})
773+
774+
fastify.register(fastifyStatic, pluginOptions)
775+
776+
t.tearDown(fastify.close.bind(fastify))
777+
778+
fastify.listen(0, err => {
779+
t.error(err)
780+
781+
fastify.server.unref()
782+
783+
t.test('/static/index.html', t => {
784+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
785+
786+
simple.concat({
787+
method: 'GET',
788+
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
789+
}, (err, response, body) => {
790+
t.error(err)
791+
t.strictEqual(response.statusCode, 200)
792+
t.strictEqual(body.toString(), indexContent)
793+
genericResponseChecks(t, response)
794+
})
795+
})
796+
})
797+
})
798+
799+
t.test('register /static/ without schemaHide', t => {
800+
t.plan(3)
801+
802+
const pluginOptions = {
803+
root: path.join(__dirname, '/static'),
804+
prefix: '/static/'
805+
}
806+
807+
const fastify = Fastify()
808+
809+
fastify.addHook('onRoute', function (routeOptions) {
810+
t.deepEqual(routeOptions.schema, { hide: true })
811+
})
812+
813+
fastify.register(fastifyStatic, pluginOptions)
814+
815+
t.tearDown(fastify.close.bind(fastify))
816+
817+
fastify.listen(0, err => {
818+
t.error(err)
819+
820+
fastify.server.unref()
821+
822+
t.test('/static/index.html', t => {
823+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
824+
825+
simple.concat({
826+
method: 'GET',
827+
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
828+
}, (err, response, body) => {
829+
t.error(err)
830+
t.strictEqual(response.statusCode, 200)
831+
t.strictEqual(body.toString(), indexContent)
832+
genericResponseChecks(t, response)
833+
})
834+
})
835+
})
836+
})

0 commit comments

Comments
 (0)