diff --git a/index.js b/index.js index cbbb913..645fd5c 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ var path = require('path'); var loopback = require('loopback'); var swagger = require('loopback/node_modules/strong-remoting/ext/swagger'); +var express = require('loopback/node_modules/express'); var STATIC_ROOT = path.join(__dirname, 'public'); module.exports = explorer; @@ -16,7 +17,16 @@ module.exports = explorer; */ function explorer(loopbackApplication, options) { + var options = options || {}; var remotes = loopbackApplication.remotes(); swagger(remotes, options); - return loopback.static(STATIC_ROOT); + + var app = express(); + app.get('/config.json', function(req, res) { + res.send({ + discoveryUrl: (options.basePath || '') + '/swagger/resources' + }); + }); + app.use(loopback.static(STATIC_ROOT)); + return app; } diff --git a/package.json b/package.json index 69a85e0..eea3bba 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "devDependencies": { "loopback": "1.x.x", "mocha": "~1.14.0", - "supertest": "~0.8.1" + "supertest": "~0.8.1", + "chai": "~1.8.1" } } diff --git a/public/index.html b/public/index.html index 0b2a25c..03bd2c3 100644 --- a/public/index.html +++ b/public/index.html @@ -17,23 +17,30 @@ @@ -53,8 +59,11 @@
-
+
+ +
Explore
diff --git a/test/explorer.test.js b/test/explorer.test.js index 6ef0103..05cb851 100644 --- a/test/explorer.test.js +++ b/test/explorer.test.js @@ -1,38 +1,80 @@ +var loopback = require('loopback'); +var explorer = require('../'); var request = require('supertest'); var assert = require('assert'); +var expect = require('chai').expect; -describe('explorer', function () { - beforeEach(function (done) { - var loopback = require('loopback'); - var app = this.app = loopback(); - var explorer = require('../'); - var Product = loopback.Model.extend('product'); - Product.attachTo(loopback.memory()); - app.model(Product); +describe('explorer', function() { - app.use(loopback.rest()); - app.use('/explorer', explorer(app)); + describe('with default config', function() { + beforeEach(givenLoopBackAppWithExplorer()); - done(); + it('should redirect to /explorer/', function(done) { + request(this.app) + .get('/explorer') + .expect(303) + .end(done); + }); + + it('should serve the explorer at /explorer/', function(done) { + request(this.app) + .get('/explorer/') + .expect('Content-Type', /html/) + .expect(200) + .end(function(err, res) { + if (err) throw err; + + assert(!!~res.text.indexOf('StrongLoop 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('discoveryUrl', '/swagger/resources'); + done(); + }); + }); + }) + + describe('with custom baseUrl', function() { + beforeEach(givenLoopBackAppWithExplorer('/api')); + + 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('discoveryUrl', '/api/swagger/resources'); + done(); + }); + }); }); - it('should redirect to /explorer/', function (done) { - request(this.app) - .get('/explorer') - .expect(303) - .end(done); - }); + function givenLoopBackAppWithExplorer(restUrlBase) { + return function(done) { + var app = this.app = loopback(); + var Product = loopback.Model.extend('product'); + Product.attachTo(loopback.memory()); + app.model(Product); - it('should serve the explorer at /explorer/', function (done) { - request(this.app) - .get('/explorer/') - .expect('Content-Type', /html/) - .expect(200) - .end(function(err, res) { - if(err) throw err; - - assert(!!~res.text.indexOf('StrongLoop API Explorer'), 'text does not contain expected string'); - done(); - }); - }); + if (restUrlBase) { + app.use(restUrlBase, loopback.rest()); + app.use('/explorer', explorer(app, { basePath: restUrlBase })); + } else { + app.use(loopback.rest()); + app.use('/explorer', explorer(app)); + } + done(); + } + } });