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;
|
|
|
|
|
2014-07-05 19:32:00 +00:00
|
|
|
assert(!!~res.text.indexOf('<title>StrongLoop API Explorer</title>'),
|
|
|
|
'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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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) {
|
2014-01-07 15:56:58 +00:00
|
|
|
var Product = loopback.Model.extend('product');
|
|
|
|
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
|
|
|
});
|