Add tests for headers bug fixes

This commit is contained in:
Gilad Oren 2012-07-31 14:43:00 +03:00 committed by indexzero
parent ffe74ed299
commit ecb547223f
3 changed files with 61 additions and 7 deletions

View File

@ -61,6 +61,12 @@ exports.createServer = function (options, callback) {
}); });
} }
if (options.outputHeaders){
Object.keys(options.outputHeaders).forEach(function(header){
res.setHeader(header, options.outputHeaders[header]);
});
}
res.writeHead(200, { 'Content-Type': 'text/plain' }); res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write(options.output || 'hello proxy'); res.write(options.output || 'hello proxy');
res.end(); res.end();
@ -114,6 +120,11 @@ exports.createProxyServer = function (options, callback) {
function requestHandler(req, res) { function requestHandler(req, res) {
var buffer = httpProxy.buffer(req); var buffer = httpProxy.buffer(req);
if (options.outputHeaders){
Object.keys(options.outputHeaders).forEach(function(header){
res.setHeader(header, options.outputHeaders[header]);
});
}
setTimeout(function () { setTimeout(function () {
// //
// Setup options dynamically for `RoutingProxy.prototype.proxyRequest` // Setup options dynamically for `RoutingProxy.prototype.proxyRequest`

View File

@ -42,11 +42,41 @@ vows.describe(helpers.describe()).addBatch({
"and headers": macros.http.assertProxied({ "and headers": macros.http.assertProxied({
request: { headers: { host: 'unknown.com' } } request: { headers: { host: 'unknown.com' } }
}), }),
"and request close connection header": macros.http.assertProxied({
request: { headers: { connection: "close" } },
outputHeaders: { connection: "close" }
}),
"and request keep alive connection header": macros.http.assertProxied({
request: { headers: { connection: "keep-alive" } },
outputHeaders: { connection: "keep-alive" }
}),
"and response close connection header": macros.http.assertProxied({
request: { headers: { connection: "" } }, // Must explicitly set to "" because otherwise node will automatically add a "connection: keep-alive" header
targetHeaders: { connection: "close" },
outputHeaders: { connection: "close" }
}),
"and response keep-alive connection header": macros.http.assertProxied({
request: { headers: { connection: "" } }, // Must explicitly set to "" because otherwise node will automatically add a "connection: keep-alive" header
targetHeaders: { connection: "keep-alive" },
outputHeaders: { connection: "keep-alive" }
}),
"and no connection header": macros.http.assertProxied({
request: { headers: { connection: "" } }, // Must explicitly set to "" because otherwise node will automatically add a "connection: keep-alive" header
outputHeaders: { connection: "keep-alive" }
}),
"and forwarding enabled": macros.http.assertForwardProxied() "and forwarding enabled": macros.http.assertForwardProxied()
}, },
"and latency": macros.http.assertProxied({ "and latency": {
"and no headers": macros.http.assertProxied({
latency: 2000 latency: 2000
}),
"and response headers": macros.http.assertProxied({
targetHeaders: { "x-testheader": "target" },
proxyHeaders: { "X-TestHeader": "proxy" },
outputHeaders: { "x-testheader": "target" },
latency: 1000
}) })
}
}, },
"With a no valid target server": { "With a no valid target server": {
"and no latency": macros.http.assertInvalidProxy(), "and no latency": macros.http.assertInvalidProxy(),

View File

@ -32,6 +32,12 @@ exports.assertRequest = function (options) {
}, },
"should succeed": function (err, res, body) { "should succeed": function (err, res, body) {
assert.isNull(err); assert.isNull(err);
if (options.assert.headers) {
Object.keys(options.assert.headers).forEach(function(header){
assert.equal(res.headers[header], options.assert.headers[header]);
});
}
if (options.assert.body) { if (options.assert.body) {
assert.equal(body, options.assert.body); assert.equal(body, options.assert.body);
} }
@ -59,9 +65,13 @@ exports.assertProxied = function (options) {
var ports = options.ports || helpers.nextPortPair, var ports = options.ports || helpers.nextPortPair,
output = options.output || 'hello world from ' + ports.target, output = options.output || 'hello world from ' + ports.target,
outputHeaders = options.outputHeaders,
targetHeaders = options.targetHeaders,
proxyHeaders = options.proxyHeaders,
protocol = helpers.protocols.proxy, protocol = helpers.protocols.proxy,
req = options.request || {}; req = options.request || {};
req.uri = req.uri || protocol + '://127.0.0.1:' + ports.proxy; req.uri = req.uri || protocol + '://127.0.0.1:' + ports.proxy;
return { return {
@ -73,12 +83,14 @@ exports.assertProxied = function (options) {
helpers.http.createServerPair({ helpers.http.createServerPair({
target: { target: {
output: output, output: output,
outputHeaders: targetHeaders,
port: ports.target, port: ports.target,
headers: req.headers headers: req.headers
}, },
proxy: { proxy: {
latency: options.latency, latency: options.latency,
port: ports.proxy, port: ports.proxy,
outputHeaders: proxyHeaders,
proxy: { proxy: {
forward: options.forward, forward: options.forward,
target: { target: {
@ -93,6 +105,7 @@ exports.assertProxied = function (options) {
"the proxy request": exports.assertRequest({ "the proxy request": exports.assertRequest({
request: req, request: req,
assert: { assert: {
headers: outputHeaders,
body: output body: output
} }
}) })