diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index 87de4f5a2..e8a337424 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -113,8 +113,7 @@ exports.setMaxSockets = function (value) { exports.createServer = function () { var args = Array.prototype.slice.call(arguments), callback = typeof args[0] === 'function' && args.shift(), - options = {}, - port, host, forward, silent, proxy, server; + options = {}, port, host, forward, silent, proxy, server; if (args.length >= 2) { port = args[0]; @@ -221,8 +220,10 @@ var HttpProxy = exports.HttpProxy = function (options) { var self = this; options = options || {}; + options.target = options.target || {}; + this.forward = options.forward; - this.https = options.https; + this.target = options.target; this.changeOrigin = options.changeOrigin || false; if (options.router) { @@ -303,8 +304,14 @@ HttpProxy.prototype.close = function () { HttpProxy.prototype.proxyRequest = function (req, res, options) { var self = this, errState = false, location, outgoing, protocol, reverseProxy; + // // Create an empty options hash if none is passed. - options = options || {}; + // If default options have been passed to the constructor + // of this instance, use them by default. + // + options = options || {}; + options.host = options.host || this.target.host; + options.port = options.port || this.target.port; // // Check the proxy table for this instance to see if we need @@ -375,7 +382,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) { outgoing = { host: options.host, port: options.port, - agent: _getAgent(options.host, options.port, options.https || this.https), + agent: _getAgent(options.host, options.port, options.https || this.target.https), method: req.method, path: req.url, headers: req.headers @@ -385,7 +392,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) { // node.js core re-implements 'keep-alive'. outgoing.headers['connection'] = 'close'; - protocol = _getProtocol(options.https || this.https, outgoing); + protocol = _getProtocol(options.https || this.target.https, outgoing); // Open new HTTP request to internal resource with will act as a reverse proxy pass reverseProxy = protocol.request(outgoing, function (response) { @@ -594,8 +601,8 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options _socket(socket); // Remote host address - var protocolName = options.https || this.https ? 'https' : 'http', - agent = _getAgent(options.host, options.port, options.https || this.https), + var protocolName = options.https || this.target.https ? 'https' : 'http', + agent = _getAgent(options.host, options.port, options.https || this.target.https), remoteHost = options.host + (options.port - 80 === 0 ? '' : ':' + options.port); // Change headers diff --git a/test/helpers.js b/test/helpers.js index 42834da67..0c326fa53 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -33,14 +33,22 @@ var loadHttps = exports.loadHttps = function () { }; }; -var TestRunner = exports.TestRunner = function (protocol) { - this.options = {}; - this.protocol = protocol; - this.testServers = []; +var TestRunner = exports.TestRunner = function (protocol, target) { + this.options = {}; + this.options.target = {}; + this.protocol = protocol; + this.target = target; + this.testServers = []; if (protocol === 'https') { this.options.https = loadHttps(); } + + if (target === 'https') { + this.options.target = { + https: loadHttps() + }; + } }; TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProxy) { @@ -213,8 +221,8 @@ TestRunner.prototype.startTargetServer = function (port, output, callback) { res.end(); }; - targetServer = this.options.https - ? https.createServer(this.options.https, handler) + targetServer = this.options.target.https + ? https.createServer(this.options.target.https, handler) : http.createServer(handler); targetServer.listen(port, function () { diff --git a/test/node-http-proxy-test.js b/test/node-http-proxy-test.js index 536b3aba4..4da2429dc 100644 --- a/test/node-http-proxy-test.js +++ b/test/node-http-proxy-test.js @@ -46,7 +46,8 @@ var badForwardOptions = { }; var protocol = argv.https ? 'https' : 'http', - runner = new helpers.TestRunner(protocol); + target = argv.target ? argv.target : 'http', + runner = new helpers.TestRunner(protocol, target); vows.describe('node-http-proxy/' + protocol).addBatch({ "When using server created by httpProxy.createServer()": {