Skip to content

Commit 895f577

Browse files
committed
[doc test api] Improve node-http-proxy API to allow for HTTPS to HTTP proxying scenarios. Update tests accordingly.
1 parent d9fa261 commit 895f577

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

lib/node-http-proxy.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ exports.setMaxSockets = function (value) {
113113
exports.createServer = function () {
114114
var args = Array.prototype.slice.call(arguments),
115115
callback = typeof args[0] === 'function' && args.shift(),
116-
options = {},
117-
port, host, forward, silent, proxy, server;
116+
options = {}, port, host, forward, silent, proxy, server;
118117

119118
if (args.length >= 2) {
120119
port = args[0];
@@ -221,8 +220,10 @@ var HttpProxy = exports.HttpProxy = function (options) {
221220

222221
var self = this;
223222
options = options || {};
223+
options.target = options.target || {};
224+
224225
this.forward = options.forward;
225-
this.https = options.https;
226+
this.target = options.target;
226227
this.changeOrigin = options.changeOrigin || false;
227228

228229
if (options.router) {
@@ -303,8 +304,14 @@ HttpProxy.prototype.close = function () {
303304
HttpProxy.prototype.proxyRequest = function (req, res, options) {
304305
var self = this, errState = false, location, outgoing, protocol, reverseProxy;
305306

307+
//
306308
// Create an empty options hash if none is passed.
307-
options = options || {};
309+
// If default options have been passed to the constructor
310+
// of this instance, use them by default.
311+
//
312+
options = options || {};
313+
options.host = options.host || this.target.host;
314+
options.port = options.port || this.target.port;
308315

309316
//
310317
// Check the proxy table for this instance to see if we need
@@ -375,7 +382,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
375382
outgoing = {
376383
host: options.host,
377384
port: options.port,
378-
agent: _getAgent(options.host, options.port, options.https || this.https),
385+
agent: _getAgent(options.host, options.port, options.https || this.target.https),
379386
method: req.method,
380387
path: req.url,
381388
headers: req.headers
@@ -385,7 +392,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
385392
// node.js core re-implements 'keep-alive'.
386393
outgoing.headers['connection'] = 'close';
387394

388-
protocol = _getProtocol(options.https || this.https, outgoing);
395+
protocol = _getProtocol(options.https || this.target.https, outgoing);
389396

390397
// Open new HTTP request to internal resource with will act as a reverse proxy pass
391398
reverseProxy = protocol.request(outgoing, function (response) {
@@ -594,8 +601,8 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
594601
_socket(socket);
595602

596603
// Remote host address
597-
var protocolName = options.https || this.https ? 'https' : 'http',
598-
agent = _getAgent(options.host, options.port, options.https || this.https),
604+
var protocolName = options.https || this.target.https ? 'https' : 'http',
605+
agent = _getAgent(options.host, options.port, options.https || this.target.https),
599606
remoteHost = options.host + (options.port - 80 === 0 ? '' : ':' + options.port);
600607

601608
// Change headers

test/helpers.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,22 @@ var loadHttps = exports.loadHttps = function () {
3333
};
3434
};
3535

36-
var TestRunner = exports.TestRunner = function (protocol) {
37-
this.options = {};
38-
this.protocol = protocol;
39-
this.testServers = [];
36+
var TestRunner = exports.TestRunner = function (protocol, target) {
37+
this.options = {};
38+
this.options.target = {};
39+
this.protocol = protocol;
40+
this.target = target;
41+
this.testServers = [];
4042

4143
if (protocol === 'https') {
4244
this.options.https = loadHttps();
4345
}
46+
47+
if (target === 'https') {
48+
this.options.target = {
49+
https: loadHttps()
50+
};
51+
}
4452
};
4553

4654
TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProxy) {
@@ -213,8 +221,8 @@ TestRunner.prototype.startTargetServer = function (port, output, callback) {
213221
res.end();
214222
};
215223

216-
targetServer = this.options.https
217-
? https.createServer(this.options.https, handler)
224+
targetServer = this.options.target.https
225+
? https.createServer(this.options.target.https, handler)
218226
: http.createServer(handler);
219227

220228
targetServer.listen(port, function () {

test/node-http-proxy-test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ var badForwardOptions = {
4646
};
4747

4848
var protocol = argv.https ? 'https' : 'http',
49-
runner = new helpers.TestRunner(protocol);
49+
target = argv.target ? argv.target : 'http',
50+
runner = new helpers.TestRunner(protocol, target);
5051

5152
vows.describe('node-http-proxy/' + protocol).addBatch({
5253
"When using server created by httpProxy.createServer()": {

0 commit comments

Comments
 (0)