diff --git a/test/parallel/test-http-response-multi-content-length.js b/test/parallel/test-http-response-multi-content-length.js index b2d28b7a9bcec1..0ef45a027fb64d 100644 --- a/test/parallel/test-http-response-multi-content-length.js +++ b/test/parallel/test-http-response-multi-content-length.js @@ -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); +}