2013-11-29 15:17:59 +00:00
|
|
|
var loopback = require('loopback');
|
|
|
|
var explorer = require('../');
|
2013-11-05 19:16:59 +00:00
|
|
|
var request = require('supertest');
|
|
|
|
var assert = require('assert');
|
2014-10-22 08:55:25 +00:00
|
|
|
var path = require('path');
|
2013-11-29 15:17:59 +00:00
|
|
|
var expect = require('chai').expect;
|
2015-01-09 13:00:22 +00:00
|
|
|
var urlJoin = require('../lib/url-join');
|
2013-11-05 19:16:59 +00:00
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
describe('explorer', function() {
|
2013-11-05 19:16:59 +00:00
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
describe('with default config', function() {
|
|
|
|
beforeEach(givenLoopBackAppWithExplorer());
|
2013-11-05 19:16:59 +00:00
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
it('should redirect to /explorer/', function(done) {
|
|
|
|
request(this.app)
|
|
|
|
.get('/explorer')
|
|
|
|
.expect(303)
|
|
|
|
.end(done);
|
|
|
|
});
|
2013-11-05 19:16:59 +00:00
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
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;
|
|
|
|
|
2014-10-22 08:55:25 +00:00
|
|
|
assert(!!~res.text.indexOf('<title>StrongLoop API Explorer</title>'),
|
2014-07-05 19:32:00 +00:00
|
|
|
'text does not contain expected string');
|
2013-11-29 15:17:59 +00:00
|
|
|
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
|
2014-07-09 22:38:05 +00:00
|
|
|
.have.property('url', '/explorer/resources');
|
2013-11-29 15:17:59 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-01-07 15:56:58 +00:00
|
|
|
});
|
2013-11-05 19:16:59 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
describe('with custom explorer base', function() {
|
|
|
|
beforeEach(givenLoopBackAppWithExplorer('/swagger'));
|
2013-11-29 15:17:59 +00:00
|
|
|
|
|
|
|
it('should serve correct swagger-ui config', function(done) {
|
|
|
|
request(this.app)
|
2014-07-09 22:38:05 +00:00
|
|
|
.get('/swagger/config.json')
|
2013-11-29 15:17:59 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body).to
|
2014-07-09 22:38:05 +00:00
|
|
|
.have.property('url', '/swagger/resources');
|
2013-11-29 15:17:59 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-11-05 19:16:59 +00:00
|
|
|
});
|
2013-11-29 15:17:59 +00:00
|
|
|
|
2014-01-07 15:56:58 +00:00
|
|
|
describe('with custom app.restApiRoot', function() {
|
|
|
|
it('should serve correct swagger-ui config', function(done) {
|
|
|
|
var app = loopback();
|
|
|
|
app.set('restApiRoot', '/rest-api-root');
|
|
|
|
configureRestApiAndExplorer(app);
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.get('/explorer/config.json')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body).to
|
2014-07-09 22:38:05 +00:00
|
|
|
.have.property('url', '/explorer/resources');
|
2014-01-07 15:56:58 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-01-09 13:00:22 +00:00
|
|
|
|
|
|
|
it('removes trailing slash from baseUrl', function(done) {
|
|
|
|
// SwaggerUI builds resource URL by concatenating basePath + resourcePath
|
|
|
|
// Since the resource paths are always startign with a slash,
|
|
|
|
// if the basePath ends with a slash too, an incorrect URL is produced
|
|
|
|
var app = loopback();
|
|
|
|
app.set('restApiRoot', '/');
|
|
|
|
configureRestApiAndExplorer(app);
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.get('/explorer/resources/products')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var baseUrl = res.body.basePath;
|
|
|
|
var apiPath = res.body.apis[0].path;
|
|
|
|
expect(baseUrl + apiPath).to.match(/http:\/\/[^\/]+\/products/);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-01-07 15:56:58 +00:00
|
|
|
});
|
|
|
|
|
2014-10-22 08:55:25 +00:00
|
|
|
describe('with custom front-end files', function() {
|
|
|
|
var app;
|
|
|
|
beforeEach(function setupExplorerWithUiDirs() {
|
|
|
|
app = loopback();
|
|
|
|
app.use('/explorer', explorer(app, {
|
|
|
|
uiDirs: [ path.resolve(__dirname, 'fixtures', 'dummy-swagger-ui') ]
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('overrides swagger-ui files', function(done) {
|
|
|
|
request(app).get('/explorer/swagger-ui.js')
|
|
|
|
.expect(200)
|
|
|
|
// expect the content of `dummy-swagger-ui/swagger-ui.js`
|
|
|
|
.expect('/* custom swagger-ui file */\n')
|
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('overrides strongloop overrides', function(done) {
|
|
|
|
request(app).get('/explorer/')
|
|
|
|
.expect(200)
|
|
|
|
// expect the content of `dummy-swagger-ui/index.html`
|
|
|
|
.expect('custom index.html\n')
|
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-12-31 21:49:06 +00:00
|
|
|
describe('when specifying custom static file root directories', function() {
|
|
|
|
var app;
|
|
|
|
beforeEach(function() {
|
|
|
|
app = loopback();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow `uiDirs` to be defined as an Array', function(done) {
|
|
|
|
app.use('/explorer', explorer(app, {
|
|
|
|
uiDirs: [ path.resolve(__dirname, 'fixtures', 'dummy-swagger-ui') ]
|
|
|
|
}));
|
|
|
|
|
|
|
|
request(app).get('/explorer/')
|
|
|
|
.expect(200)
|
|
|
|
// expect the content of `dummy-swagger-ui/index.html`
|
|
|
|
.expect('custom index.html\n')
|
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow `uiDirs` to be defined as an String', function(done) {
|
|
|
|
app.use('/explorer', explorer(app, {
|
|
|
|
uiDirs: path.resolve(__dirname, 'fixtures', 'dummy-swagger-ui')
|
|
|
|
}));
|
|
|
|
|
|
|
|
request(app).get('/explorer/')
|
|
|
|
.expect(200)
|
|
|
|
// expect the content of `dummy-swagger-ui/index.html`
|
|
|
|
.expect('custom index.html\n')
|
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
function givenLoopBackAppWithExplorer(explorerBase) {
|
2013-11-29 15:17:59 +00:00
|
|
|
return function(done) {
|
|
|
|
var app = this.app = loopback();
|
2014-07-09 22:38:05 +00:00
|
|
|
configureRestApiAndExplorer(app, explorerBase);
|
2013-11-29 15:17:59 +00:00
|
|
|
done();
|
2014-01-07 15:56:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
function configureRestApiAndExplorer(app, explorerBase) {
|
2015-01-09 13:00:22 +00:00
|
|
|
var Product = loopback.PersistedModel.extend('product');
|
2014-01-07 15:56:58 +00:00
|
|
|
Product.attachTo(loopback.memory());
|
|
|
|
app.model(Product);
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
app.use(explorerBase || '/explorer', explorer(app));
|
|
|
|
app.use(app.get('restApiRoot') || '/', loopback.rest());
|
2013-11-29 15:17:59 +00:00
|
|
|
}
|
2013-11-05 19:16:59 +00:00
|
|
|
});
|