From c47d8c9189087f3404c0af867a286d2037786159 Mon Sep 17 00:00:00 2001 From: cndreiter Date: Wed, 27 May 2015 00:04:40 +0200 Subject: [PATCH] Add `opts.host` to customize host of resource URLs --- lib/swagger.js | 2 +- test/swagger.test.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/swagger.js b/lib/swagger.js index 92d0d90..51e4755 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -193,7 +193,7 @@ function addRoute(app, uri, doc, opts) { // 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; + var host = headers['x-forwarded-host'] || opts.host || headers.host; doc.basePath = prefix + host + initialPath; } res.status(200).send(doc); diff --git a/test/swagger.test.js b/test/swagger.test.js index 120f08e..18e3430 100644 --- a/test/swagger.test.js +++ b/test/swagger.test.js @@ -119,6 +119,17 @@ describe('swagger definition', function() { 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() {