0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-http-response-multi-content-length.js
Rich Trott c2637847b3 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 <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
2020-03-29 22:46:39 -07:00

42 lines
1.1 KiB
JavaScript

'use strict';
const common = require('../common');
const http = require('http');
const assert = require('assert');
// 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.
function test(server) {
server.listen(0, common.mustCall(() => {
http.get(
{ port: server.address().port },
() => { assert.fail('Client allowed multiple content-length headers.'); }
).on('error', common.mustCall((err) => {
assert.ok(err.message.startsWith('Parse Error'), err.message);
assert.strictEqual(err.code, 'HPE_UNEXPECTED_CONTENT_LENGTH');
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);
}