diff --git a/index.js b/index.js index b588f7c..0fd184b 100644 --- a/index.js +++ b/index.js @@ -69,6 +69,10 @@ function routes(loopbackApplication, options) { // Get the path we're mounted at. It's best to get this from the referer // in case we're proxied at a deep path. var source = url.parse(req.headers.referer || '').pathname; + // strip index.html if present in referer + if (source && /\/index\.html$/.test(source)) { + source = source.replace(/\/index\.html$/, ''); + } // If no referer is available, use the incoming url. if (!source) { source = req.originalUrl.replace(/\/config.json(\?.*)?$/, ''); diff --git a/test/explorer.test.js b/test/explorer.test.js index 72ff1ee..e9ddf12 100644 --- a/test/explorer.test.js +++ b/test/explorer.test.js @@ -33,6 +33,40 @@ describe('explorer', function() { .get('/explorer/') .expect('Content-Type', /html/) .expect(200) + .end(function(err, res) { + if (err) return done(err); + + assert(!!~res.text.indexOf('LoopBack API Explorer'), + 'text does not contain expected string'); + + done(); + }); + }); + + it('should serve correct swagger-ui config', function(done) { + request(this.app) + .get('/explorer/config.json') + .expect('Content-Type', /json/) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to + .have.property('url', '/explorer/swagger.json'); + + done(); + }); + }); + }); + + describe('when filename is included in url', function() { + beforeEach(givenLoopBackAppWithExplorer()); + + it('should serve the explorer at /explorer/index.html', function(done) { + request(this.app) + .get('/explorer/index.html') + .expect('Content-Type', /html/) + .expect(200) .end(function(err, res) { if (err) throw err; @@ -46,6 +80,7 @@ describe('explorer', function() { it('should serve correct swagger-ui config', function(done) { request(this.app) .get('/explorer/config.json') + .set('Referer', 'http://example.com/explorer/index.html') .expect('Content-Type', /json/) .expect(200) .end(function(err, res) {