Skip to content

Commit

Permalink
test: test more http response splitting scenarios
Browse files Browse the repository at this point in the history
The test verified the output of http.OutgoingMessage#writeHead() but
not http.OutgoingMessage#setHeader().  Also check the response body.

PR-URL: #2945
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Rod Vagg <r@va.gg>
  • Loading branch information
bnoordhuis authored and Fishrock123 committed Sep 20, 2015
1 parent fa08d1d commit 2084f52
Showing 1 changed file with 52 additions and 21 deletions.
73 changes: 52 additions & 21 deletions test/parallel/test-http-header-response-splitting.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,75 @@ var common = require('../common'),
assert = require('assert'),
http = require('http');

var testIndex = 0,
responses = 0;
var testIndex = 0;
const testCount = 4 * 6;
const responseBody = 'Hi mars!';

var server = http.createServer(function(req, res) {
switch (testIndex++) {
function reply(header) {
switch (testIndex % 4) {
case 0:
res.writeHead(200, { test: 'foo \r\ninvalid: bar' });
res.writeHead(200, { a: header, b: header });
break;
case 1:
res.writeHead(200, { test: 'foo \ninvalid: bar' });
res.setHeader('a', header);
res.setHeader('b', header);
res.writeHead(200);
break;
case 2:
res.writeHead(200, { test: 'foo \rinvalid: bar' });
res.setHeader('a', header);
res.writeHead(200, { b: header });
break;
case 3:
res.writeHead(200, { test: 'foo \n\n\ninvalid: bar' });
res.setHeader('a', [header]);
res.writeHead(200, { b: header });
break;
default:
assert.fail('unreachable');
}
res.end(responseBody);
}
switch ((testIndex / 4) | 0) {
case 0:
reply('foo \r\ninvalid: bar');
break;
case 1:
reply('foo \ninvalid: bar');
break;
case 2:
reply('foo \rinvalid: bar');
break;
case 3:
reply('foo \n\n\ninvalid: bar');
break;
case 4:
res.writeHead(200, { test: 'foo \r\n \r\n \r\ninvalid: bar' });
server.close();
reply('foo \r\n \r\n \r\ninvalid: bar');
break;
case 5:
reply('foo \r \n \r \n \r \ninvalid: bar');
break;
default:
assert(false);
}
res.end('Hi mars!');
if (++testIndex === testCount) {
server.close();
}
});

server.listen(common.PORT, function() {
for (var i = 0; i < 5; i++) {
var req = http.get({ port: common.PORT, path: '/' }, function(res) {
assert.strictEqual(res.headers.test, 'foo invalid: bar');
server.listen(common.PORT, common.mustCall(function() {
for (var i = 0; i < testCount; i++) {
http.get({ port: common.PORT, path: '/' }, common.mustCall(function(res) {
assert.strictEqual(res.headers.a, 'foo invalid: bar');
assert.strictEqual(res.headers.b, 'foo invalid: bar');
assert.strictEqual(res.headers.foo, undefined);
assert.strictEqual(res.headers.invalid, undefined);
responses++;
var data = '';
res.setEncoding('utf8');
res.on('data', function(s) { data += s; });
res.on('end', common.mustCall(function() {
assert.equal(data, responseBody);
}));
res.resume();
});
}));
}
});

process.on('exit', function() {
assert.strictEqual(responses, 5);
});
}));

0 comments on commit 2084f52

Please sign in to comment.