Fix config.json URL when running from /index.html

This commit is contained in:
Jonathan Prince 2018-08-14 15:29:51 +02:00 committed by Miroslav Bajtoš
parent 1f28c60792
commit 09bf982a21
No known key found for this signature in database
GPG Key ID: 6F2304BA9361C7E3
2 changed files with 39 additions and 0 deletions

View File

@ -69,6 +69,10 @@ function routes(loopbackApplication, options) {
// Get the path we're mounted at. It's best to get this from the referer // Get the path we're mounted at. It's best to get this from the referer
// in case we're proxied at a deep path. // in case we're proxied at a deep path.
var source = url.parse(req.headers.referer || '').pathname; var source = url.parse(req.headers.referer || '').pathname;
// strip index.html if present in referer
if (source && /\/index\.html$/.test(source)) {
source = source.replace(/\/index\.html$/, '');
}
// If no referer is available, use the incoming url. // If no referer is available, use the incoming url.
if (!source) { if (!source) {
source = req.originalUrl.replace(/\/config.json(\?.*)?$/, ''); source = req.originalUrl.replace(/\/config.json(\?.*)?$/, '');

View File

@ -33,6 +33,40 @@ describe('explorer', function() {
.get('/explorer/') .get('/explorer/')
.expect('Content-Type', /html/) .expect('Content-Type', /html/)
.expect(200) .expect(200)
.end(function(err, res) {
if (err) return done(err);
assert(!!~res.text.indexOf('<title>LoopBack 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('url', '/explorer/swagger.json');
done();
});
});
});
describe('when filename is included in url', function() {
beforeEach(givenLoopBackAppWithExplorer());
it('should serve the explorer at /explorer/index.html', function(done) {
request(this.app)
.get('/explorer/index.html')
.expect('Content-Type', /html/)
.expect(200)
.end(function(err, res) { .end(function(err, res) {
if (err) throw err; if (err) throw err;
@ -46,6 +80,7 @@ describe('explorer', function() {
it('should serve correct swagger-ui config', function(done) { it('should serve correct swagger-ui config', function(done) {
request(this.app) request(this.app)
.get('/explorer/config.json') .get('/explorer/config.json')
.set('Referer', 'http://example.com/explorer/index.html')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
.end(function(err, res) { .end(function(err, res) {