Skip to content

Commit ecb5472

Browse files
gilad61indexzero
authored andcommitted
Add tests for headers bug fixes
1 parent ffe74ed commit ecb5472

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

test/helpers/http.js

+11
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ exports.createServer = function (options, callback) {
6161
});
6262
}
6363

64+
if (options.outputHeaders){
65+
Object.keys(options.outputHeaders).forEach(function(header){
66+
res.setHeader(header, options.outputHeaders[header]);
67+
});
68+
}
69+
6470
res.writeHead(200, { 'Content-Type': 'text/plain' });
6571
res.write(options.output || 'hello proxy');
6672
res.end();
@@ -114,6 +120,11 @@ exports.createProxyServer = function (options, callback) {
114120
function requestHandler(req, res) {
115121
var buffer = httpProxy.buffer(req);
116122

123+
if (options.outputHeaders){
124+
Object.keys(options.outputHeaders).forEach(function(header){
125+
res.setHeader(header, options.outputHeaders[header]);
126+
});
127+
}
117128
setTimeout(function () {
118129
//
119130
// Setup options dynamically for `RoutingProxy.prototype.proxyRequest`

test/http/http-test.js

+33-3
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,41 @@ vows.describe(helpers.describe()).addBatch({
4242
"and headers": macros.http.assertProxied({
4343
request: { headers: { host: 'unknown.com' } }
4444
}),
45+
"and request close connection header": macros.http.assertProxied({
46+
request: { headers: { connection: "close" } },
47+
outputHeaders: { connection: "close" }
48+
}),
49+
"and request keep alive connection header": macros.http.assertProxied({
50+
request: { headers: { connection: "keep-alive" } },
51+
outputHeaders: { connection: "keep-alive" }
52+
}),
53+
"and response close connection header": macros.http.assertProxied({
54+
request: { headers: { connection: "" } }, // Must explicitly set to "" because otherwise node will automatically add a "connection: keep-alive" header
55+
targetHeaders: { connection: "close" },
56+
outputHeaders: { connection: "close" }
57+
}),
58+
"and response keep-alive connection header": macros.http.assertProxied({
59+
request: { headers: { connection: "" } }, // Must explicitly set to "" because otherwise node will automatically add a "connection: keep-alive" header
60+
targetHeaders: { connection: "keep-alive" },
61+
outputHeaders: { connection: "keep-alive" }
62+
}),
63+
"and no connection header": macros.http.assertProxied({
64+
request: { headers: { connection: "" } }, // Must explicitly set to "" because otherwise node will automatically add a "connection: keep-alive" header
65+
outputHeaders: { connection: "keep-alive" }
66+
}),
4567
"and forwarding enabled": macros.http.assertForwardProxied()
4668
},
47-
"and latency": macros.http.assertProxied({
48-
latency: 2000
49-
})
69+
"and latency": {
70+
"and no headers": macros.http.assertProxied({
71+
latency: 2000
72+
}),
73+
"and response headers": macros.http.assertProxied({
74+
targetHeaders: { "x-testheader": "target" },
75+
proxyHeaders: { "X-TestHeader": "proxy" },
76+
outputHeaders: { "x-testheader": "target" },
77+
latency: 1000
78+
})
79+
}
5080
},
5181
"With a no valid target server": {
5282
"and no latency": macros.http.assertInvalidProxy(),

test/macros/http.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ exports.assertRequest = function (options) {
3232
},
3333
"should succeed": function (err, res, body) {
3434
assert.isNull(err);
35+
if (options.assert.headers) {
36+
Object.keys(options.assert.headers).forEach(function(header){
37+
assert.equal(res.headers[header], options.assert.headers[header]);
38+
});
39+
}
40+
3541
if (options.assert.body) {
3642
assert.equal(body, options.assert.body);
3743
}
@@ -57,10 +63,14 @@ exports.assertRequest = function (options) {
5763
exports.assertProxied = function (options) {
5864
options = options || {};
5965

60-
var ports = options.ports || helpers.nextPortPair,
61-
output = options.output || 'hello world from ' + ports.target,
62-
protocol = helpers.protocols.proxy,
63-
req = options.request || {};
66+
var ports = options.ports || helpers.nextPortPair,
67+
output = options.output || 'hello world from ' + ports.target,
68+
outputHeaders = options.outputHeaders,
69+
targetHeaders = options.targetHeaders,
70+
proxyHeaders = options.proxyHeaders,
71+
protocol = helpers.protocols.proxy,
72+
req = options.request || {};
73+
6474

6575
req.uri = req.uri || protocol + '://127.0.0.1:' + ports.proxy;
6676

@@ -73,12 +83,14 @@ exports.assertProxied = function (options) {
7383
helpers.http.createServerPair({
7484
target: {
7585
output: output,
86+
outputHeaders: targetHeaders,
7687
port: ports.target,
7788
headers: req.headers
7889
},
7990
proxy: {
8091
latency: options.latency,
8192
port: ports.proxy,
93+
outputHeaders: proxyHeaders,
8294
proxy: {
8395
forward: options.forward,
8496
target: {
@@ -93,6 +105,7 @@ exports.assertProxied = function (options) {
93105
"the proxy request": exports.assertRequest({
94106
request: req,
95107
assert: {
108+
headers: outputHeaders,
96109
body: output
97110
}
98111
})

0 commit comments

Comments
 (0)