From afc0c708a2d1049c70c3dadcdee21ac31519acf0 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 27 Mar 2020 16:46:33 -0700 Subject: [PATCH] test: revise test-http-response-multi-content-length Isolate the two test cases here a bit more and refactor code. PR-URL: https://github.com/nodejs/node/pull/32526 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- ...test-http-response-multi-content-length.js | 66 +++++++++---------- 1 file changed, 30 insertions(+), 36 deletions(-) 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); +}