Commit e5693d2b authored by Charlie McConnell's avatar Charlie McConnell

Merge pull request #110 from nodejitsu/gh-107

#107: Set x-forwarded-for header (amongst others)
parents 787370ee 1f33943b
...@@ -128,9 +128,10 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) { ...@@ -128,9 +128,10 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
// * `x-forwarded-proto`: Protocol of the original request // * `x-forwarded-proto`: Protocol of the original request
// * `x-forwarded-port`: Port of the original request. // * `x-forwarded-port`: Port of the original request.
// //
if (this.enable.xforward && req.connection && req.connection.socket) {
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.connection.socket.remoteAddress; if (this.enable.xforward && req.connection && req.socket) {
req.headers['x-forwarded-port'] = req.connection.remotePort || req.connection.socket.remotePort; req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.socket.remoteAddress;
req.headers['x-forwarded-port'] = req.connection.remotePort || req.socket.remotePort;
req.headers['x-forwarded-proto'] = req.connection.pair ? 'https' : 'http'; req.headers['x-forwarded-proto'] = req.connection.pair ? 'https' : 'http';
} }
...@@ -763,4 +764,4 @@ HttpProxy.prototype._forwardRequest = function (req) { ...@@ -763,4 +764,4 @@ HttpProxy.prototype._forwardRequest = function (req) {
req.on('end', function () { req.on('end', function () {
forwardProxy.end(); forwardProxy.end();
}); });
}; };
\ No newline at end of file
...@@ -128,6 +128,40 @@ TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, creat ...@@ -128,6 +128,40 @@ TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, creat
return test; return test;
}; };
// A test helper to check and see if the http headers were set properly.
TestRunner.prototype.assertHeaders = function (proxyPort, headerName, createProxy) {
var assertion = "should receive http header \"" + headerName + "\"",
protocol = this.source.protocols.http;
var test = {
topic: function () {
var that = this, options = {
method: 'GET',
uri: protocol + '://localhost:' + proxyPort,
headers: {
host: 'unknown.com'
}
};
if (createProxy) {
return createProxy(function () {
request(options, that.callback);
});
}
request(options, this.callback);
}
};
test[assertion] = function (err, res, body) {
assert.isNull(err);
assert.isNotNull(res.headers[headerName]);
};
return test;
};
// //
// WebSocketTest // WebSocketTest
// //
...@@ -368,4 +402,4 @@ function merge (target) { ...@@ -368,4 +402,4 @@ function merge (target) {
}); });
}); });
return target; return target;
} }
\ No newline at end of file
...@@ -74,8 +74,11 @@ vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({ ...@@ -74,8 +74,11 @@ vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({
"and a valid target server": runner.assertProxied('localhost', 8120, 8121, function (callback) { "and a valid target server": runner.assertProxied('localhost', 8120, 8121, function (callback) {
runner.startProxyServerWithForwarding(8120, 8121, 'localhost', forwardOptions, callback); runner.startProxyServerWithForwarding(8120, 8121, 'localhost', forwardOptions, callback);
}), }),
"and without a valid forward server": runner.assertProxied('localhost', 8122, 8123, function (callback) { "and also a valid target server": runner.assertHeaders(8122, "x-forwarded-for", function (callback) {
runner.startProxyServerWithForwarding(8122, 8123, 'localhost', badForwardOptions, callback); runner.startProxyServerWithForwarding(8122, 8123, 'localhost', forwardOptions, callback);
}),
"and without a valid forward server": runner.assertProxied('localhost', 8124, 8125, function (callback) {
runner.startProxyServerWithForwarding(8124, 8125, 'localhost', badForwardOptions, callback);
}) })
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment