Skip to content

Commit

Permalink
[test] Added a test for the "x-forwarded-for" header
Browse files Browse the repository at this point in the history
  • Loading branch information
jfhbrook committed Sep 20, 2011
1 parent 2677bb6 commit 66e9820
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
36 changes: 35 additions & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,40 @@ TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, creat
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
//
Expand Down Expand Up @@ -368,4 +402,4 @@ function merge (target) {
});
});
return target;
}
}
7 changes: 5 additions & 2 deletions test/http/http-proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
runner.startProxyServerWithForwarding(8120, 8121, 'localhost', forwardOptions, callback);
}),
"and without a valid forward server": runner.assertProxied('localhost', 8122, 8123, function (callback) {
runner.startProxyServerWithForwarding(8122, 8123, 'localhost', badForwardOptions, callback);
"and also a valid target server": runner.assertHeaders(8122, "x-forwarded-for", function (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);
})
}
}
Expand Down

0 comments on commit 66e9820

Please sign in to comment.