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');
|
2013-11-29 15:17:59 +00:00
|
|
|
var expect = require('chai').expect;
|
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;
|
|
|
|
|
|
|
|
assert(!!~res.text.indexOf('<title>StrongLoop API Explorer</title>'), '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();
|
|
|
|
});
|
|
|
|
});
|
2014-01-07 15:56:58 +00:00
|
|
|
});
|
2013-11-05 19:16:59 +00:00
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
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
|
|
|
|
.have.property('discoveryUrl', '/rest-api-root/swagger/resources');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
function givenLoopBackAppWithExplorer(restUrlBase) {
|
|
|
|
return function(done) {
|
|
|
|
var app = this.app = loopback();
|
2014-01-07 15:56:58 +00:00
|
|
|
configureRestApiAndExplorer(app, restUrlBase);
|
2013-11-29 15:17:59 +00:00
|
|
|
done();
|
2014-01-07 15:56:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function configureRestApiAndExplorer(app, restUrlBase) {
|
|
|
|
var Product = loopback.Model.extend('product');
|
|
|
|
Product.attachTo(loopback.memory());
|
|
|
|
app.model(Product);
|
|
|
|
|
|
|
|
if (restUrlBase) {
|
|
|
|
app.use(restUrlBase, loopback.rest());
|
|
|
|
app.use('/explorer', explorer(app, { basePath: restUrlBase }));
|
|
|
|
} else {
|
|
|
|
// LoopBack REST adapter owns the whole URL space and does not
|
|
|
|
// let other middleware handle same URLs.
|
|
|
|
// It's possible to circumvent this measure by installing
|
|
|
|
// the explorer middleware before the REST middleware.
|
|
|
|
// This way we can acess `/explorer` even when REST is mounted at `/`
|
|
|
|
app.use('/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
|
|
|
});
|