Skip to content

Commit

Permalink
test: revise test-http-response-multi-content-length
Browse files Browse the repository at this point in the history
Isolate the two test cases here a bit more and refactor code.

PR-URL: #32526
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
Trott committed Mar 30, 2020
1 parent 768b0f5 commit c263784
Showing 1 changed file with 30 additions and 36 deletions.
66 changes: 30 additions & 36 deletions test/parallel/test-http-response-multi-content-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,39 @@
const common = require('../common');
const http = require('http');
const assert = require('assert');
const Countdown = require('../common/countdown');

const MAX_COUNT = 2;
// TODO(@jasnell) At some point this should be refactored as the API should not
// be allowing users to set multiple content-length values in the first place.

const server = http.createServer((req, res) => {
const num = req.headers['x-num'];
// TODO(@jasnell) At some point this should be refactored as the API
// should not be allowing users to set multiple content-length values
// in the first place.
switch (num) {
case '1':
res.setHeader('content-length', [2, 1]);
break;
case '2':
res.writeHead(200, { 'content-length': [1, 2] });
break;
default:
assert.fail('should never get here');
}
res.end('ok');
});

const countdown = new Countdown(MAX_COUNT, () => server.close());

server.listen(0, common.mustCall(() => {
for (let n = 1; n <= MAX_COUNT; n++) {
// This runs twice, the first time, the server will use
// setHeader, the second time it uses writeHead. In either
// case, the error handler must be called because the client
// is not allowed to accept multiple content-length headers.
function test(server) {
server.listen(0, common.mustCall(() => {
http.get(
{ port: server.address().port, headers: { 'x-num': n } },
(res) => {
assert.fail('client allowed multiple content-length headers.');
}
{ port: server.address().port },
() => { assert.fail('Client allowed multiple content-length headers.'); }
).on('error', common.mustCall((err) => {
assert(/^Parse Error/.test(err.message));
assert.ok(err.message.startsWith('Parse Error'), err.message);
assert.strictEqual(err.code, 'HPE_UNEXPECTED_CONTENT_LENGTH');
countdown.dec();
server.close();
}));
}
}));
}));
}

// Test adding an extra content-length header using setHeader().
{
const server = http.createServer((req, res) => {
res.setHeader('content-length', [2, 1]);
res.end('ok');
});

test(server);
}

// Test adding an extra content-length header using writeHead().
{
const server = http.createServer((req, res) => {
res.writeHead(200, { 'content-length': [1, 2] });
res.end('ok');
});

test(server);
}

0 comments on commit c263784

Please sign in to comment.