Skip to content

Commit 66e9820

Browse files
committed
[test] Added a test for the "x-forwarded-for" header
1 parent 2677bb6 commit 66e9820

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

test/helpers.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,40 @@ TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, creat
128128
return test;
129129
};
130130

131+
// A test helper to check and see if the http headers were set properly.
132+
TestRunner.prototype.assertHeaders = function (proxyPort, headerName, createProxy) {
133+
var assertion = "should receive http header \"" + headerName + "\"",
134+
protocol = this.source.protocols.http;
135+
136+
var test = {
137+
topic: function () {
138+
var that = this, options = {
139+
method: 'GET',
140+
uri: protocol + '://localhost:' + proxyPort,
141+
headers: {
142+
host: 'unknown.com'
143+
}
144+
};
145+
146+
if (createProxy) {
147+
return createProxy(function () {
148+
request(options, that.callback);
149+
});
150+
}
151+
152+
request(options, this.callback);
153+
}
154+
};
155+
156+
test[assertion] = function (err, res, body) {
157+
assert.isNull(err);
158+
assert.isNotNull(res.headers[headerName]);
159+
};
160+
161+
return test;
162+
};
163+
164+
131165
//
132166
// WebSocketTest
133167
//
@@ -368,4 +402,4 @@ function merge (target) {
368402
});
369403
});
370404
return target;
371-
}
405+
}

test/http/http-proxy-test.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({
7474
"and a valid target server": runner.assertProxied('localhost', 8120, 8121, function (callback) {
7575
runner.startProxyServerWithForwarding(8120, 8121, 'localhost', forwardOptions, callback);
7676
}),
77-
"and without a valid forward server": runner.assertProxied('localhost', 8122, 8123, function (callback) {
78-
runner.startProxyServerWithForwarding(8122, 8123, 'localhost', badForwardOptions, callback);
77+
"and also a valid target server": runner.assertHeaders(8122, "x-forwarded-for", function (callback) {
78+
runner.startProxyServerWithForwarding(8122, 8123, 'localhost', forwardOptions, callback);
79+
}),
80+
"and without a valid forward server": runner.assertProxied('localhost', 8124, 8125, function (callback) {
81+
runner.startProxyServerWithForwarding(8124, 8125, 'localhost', badForwardOptions, callback);
7982
})
8083
}
8184
}

0 commit comments

Comments
 (0)