From 1d27e867ffec01248c31df1611e02b53871461b4 Mon Sep 17 00:00:00 2001
From: Tommaso Allevi <tomallevi@gmail.com>
Date: Thu, 10 May 2018 01:00:54 +0200
Subject: [PATCH] Fix: redirect to index (#67)

* Fix #65

* Add pre-commit

* fix prefix

* Fix for nodejs6

* Fix for nodejs6 #2
---
 package.json  |  1 +
 routes.js     |  2 +-
 test/route.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/package.json b/package.json
index 79f34907..189d3c68 100644
--- a/package.json
+++ b/package.json
@@ -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",
diff --git a/routes.js b/routes.js
index d631cd06..e0339895 100644
--- a/routes.js
+++ b/routes.js
@@ -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({
diff --git a/test/route.js b/test/route.js
index 9aeb4fef..a3ec8a3d 100644
--- a/test/route.js
+++ b/test/route.js
@@ -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)
@@ -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')
   })
 })