Skip to content

Commit

Permalink
Fix: redirect to index (#67)
Browse files Browse the repository at this point in the history
* Fix #65

* Add pre-commit

* fix prefix

* Fix for nodejs6

* Fix for nodejs6 #2
  • Loading branch information
allevo authored May 9, 2018
1 parent 11f5546 commit 1d27e86
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"devDependencies": {
"fastify": "^1.3.1",
"fs-extra": "^6.0.0",
"pre-commit": "^1.2.2",
"standard": "^11.0.0",
"swagger-parser": "^4.1.0",
"swagger-ui-dist": "3.14.0",
Expand Down
2 changes: 1 addition & 1 deletion routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function fastifySwagger (fastify, opts, next) {
url: '/',
method: 'GET',
schema: { hide: true },
handler: (request, reply) => reply.redirect(`.${opts.prefix}/`)
handler: (request, reply) => reply.redirect(`${fastify.basePath}/index.html`)
})

fastify.route({
Expand Down
59 changes: 57 additions & 2 deletions test/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ test('fastify.swagger should return a valid swagger yaml', t => {
})
})

test('/documentation should redirect to /documentation/', t => {
test('/documentation should redirect to /documentation/index.html', t => {
t.plan(4)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerInfo)
Expand All @@ -183,7 +183,62 @@ test('/documentation should redirect to /documentation/', t => {
}, (err, res) => {
t.error(err)
t.strictEqual(res.statusCode, 302)
t.strictEqual(res.headers['location'], './documentation/')
t.strictEqual(res.headers['location'], '/documentation/index.html')
t.is(typeof res.payload, 'string')
})
})

test('/v1/documentation should redirect to /v1/documentation/index.html', t => {
t.plan(4)
const fastify = Fastify()
const opts = JSON.parse(JSON.stringify(swaggerInfo))
opts.routePrefix = '/v1/documentation'
fastify.register(fastifySwagger, opts)

fastify.get('/', () => {})
fastify.post('/', () => {})
fastify.get('/example', opts1, () => {})
fastify.post('/example', opts2, () => {})
fastify.get('/parameters/:id', opts3, () => {})
fastify.get('/example1', opts4, () => {})

fastify.inject({
method: 'GET',
url: '/v1/documentation'
}, (err, res) => {
t.error(err)
t.strictEqual(res.statusCode, 302)
t.strictEqual(res.headers['location'], '/v1/documentation/index.html')
t.is(typeof res.payload, 'string')
})
})

test('/v1/foobar should redirect to /v1/foobar/index.html - in plugin', t => {
t.plan(4)
const fastify = Fastify()

fastify.register(function (fastify, options, next) {
const opts = JSON.parse(JSON.stringify(swaggerInfo))
opts.routePrefix = '/foobar'
fastify.register(fastifySwagger, opts)

fastify.get('/', () => {})
fastify.post('/', () => {})
fastify.get('/example', opts1, () => {})
fastify.post('/example', opts2, () => {})
fastify.get('/parameters/:id', opts3, () => {})
fastify.get('/example1', opts4, () => {})

next()
}, { prefix: '/v1' })

fastify.inject({
method: 'GET',
url: '/v1/foobar'
}, (err, res) => {
t.error(err)
t.strictEqual(res.statusCode, 302)
t.strictEqual(res.headers['location'], '/v1/foobar/index.html')
t.is(typeof res.payload, 'string')
})
})
Expand Down

0 comments on commit 1d27e86

Please sign in to comment.