diff --git a/lib/swagger.js b/lib/swagger.js index b25d517..3e9aacf 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -176,8 +176,9 @@ function addRoute(app, uri, doc, opts) { var headers = req.headers; // NOTE header names (keys) are always all-lowercase var proto = headers['x-forwarded-proto'] || opts.protocol || req.protocol; + var prefix = opts.omitProtocolInBaseUrl ? '//' : proto + '://'; var host = headers['x-forwarded-host'] || headers.host; - doc.basePath = proto + '://' + host + initialPath; + doc.basePath = prefix + host + initialPath; } res.status(200).send(doc); }); diff --git a/test/swagger.test.js b/test/swagger.test.js index f70c9b0..120f08e 100644 --- a/test/swagger.test.js +++ b/test/swagger.test.js @@ -105,6 +105,20 @@ describe('swagger definition', function() { done(); }); }); + + it('supports options.omitProtocolInBaseUrl', function(done) { + var app = givenAppWithSwagger({ omitProtocolInBaseUrl: true }); + + var getReq = getAPIDeclaration(app, 'products'); + getReq.end(function(err, res) { + if (err) return done(err); + var basePath = res.body.basePath; + expect(basePath).to.match(/^\/\//); + var parsed = url.parse(res.body.basePath); + expect(parsed.protocol).to.equal(null); + done(); + }); + }); }); describe('Model definition attributes', function() {