Skip to content

Commit

Permalink
[doc test api] Improve node-http-proxy API to allow for HTTPS to HTTP…
Browse files Browse the repository at this point in the history
… proxying scenarios. Update tests accordingly.
  • Loading branch information
indexzero committed May 19, 2011
1 parent d9fa261 commit 895f577
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
23 changes: 15 additions & 8 deletions lib/node-http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
20 changes: 14 additions & 6 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 () {
Expand Down
3 changes: 2 additions & 1 deletion test/node-http-proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()": {
Expand Down

0 comments on commit 895f577

Please sign in to comment.