Add `opts.host` to customize host of resource URLs

This commit is contained in:
cndreiter 2015-05-27 00:04:40 +02:00 committed by Miroslav Bajtoš
parent 8f2dc9966e
commit c47d8c9189
2 changed files with 12 additions and 1 deletions

View File

@ -193,7 +193,7 @@ function addRoute(app, uri, doc, opts) {
// NOTE header names (keys) are always all-lowercase // NOTE header names (keys) are always all-lowercase
var proto = headers['x-forwarded-proto'] || opts.protocol || req.protocol; var proto = headers['x-forwarded-proto'] || opts.protocol || req.protocol;
var prefix = opts.omitProtocolInBaseUrl ? '//' : proto + '://'; var prefix = opts.omitProtocolInBaseUrl ? '//' : proto + '://';
var host = headers['x-forwarded-host'] || headers.host; var host = headers['x-forwarded-host'] || opts.host || headers.host;
doc.basePath = prefix + host + initialPath; doc.basePath = prefix + host + initialPath;
} }
res.status(200).send(doc); res.status(200).send(doc);

View File

@ -119,6 +119,17 @@ describe('swagger definition', function() {
done(); done();
}); });
}); });
it('supports opts.header', function(done) {
var app = givenAppWithSwagger({ host: 'example.com:8080' });
getAPIDeclaration(app, 'products')
.end(function(err, res) {
if (err) return done(err);
var baseUrl = url.parse(res.body.basePath);
expect(baseUrl.host).to.equal('example.com:8080');
done();
});
});
}); });
describe('Model definition attributes', function() { describe('Model definition attributes', function() {