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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
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
|
|
|
|
|
|
|
function givenLoopBackAppWithExplorer(restUrlBase) {
|
|
|
|
return function(done) {
|
|
|
|
var app = this.app = loopback();
|
|
|
|
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 {
|
2013-12-09 10:44:46 +00:00
|
|
|
// 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.
|
2013-11-29 15:17:59 +00:00
|
|
|
app.use('/explorer', explorer(app));
|
2013-12-09 10:44:46 +00:00
|
|
|
app.use(loopback.rest());
|
2013-11-29 15:17:59 +00:00
|
|
|
}
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
}
|
2013-11-05 19:16:59 +00:00
|
|
|
});
|