Files
node/test/parallel/test-http-early-hints-invalid-argument.js
Filip Skokan cb78f66ab3 test: avoid idle HTTP/HTTPS connections
Single-use requests otherwise wait for the keep-alive timeout. Use
nonpersistent agents where agent selection is unrelated to coverage.
For default-agent tests, close the server after consuming the response.

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
Assisted-by: Codex
PR-URL: https://github.com/nodejs/node/pull/65980
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
2026-09-13 14:01:14 +02:00

91 lines
2.4 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('node:assert');
const http = require('node:http');
const debug = require('node:util').debuglog('test');
const testResBody = 'response content\n';
{
const server = http.createServer(common.mustCall((req, res) => {
debug('Server sending early hints...');
assert.throws(() => {
res.writeEarlyHints('bad argument type');
}, (err) => err.code === 'ERR_INVALID_ARG_TYPE');
assert.throws(() => {
res.writeEarlyHints({
link: '</>; '
});
}, (err) => err.code === 'ERR_INVALID_ARG_VALUE');
assert.throws(() => {
res.writeEarlyHints({
link: 'rel=preload; </scripts.js>'
});
}, (err) => err.code === 'ERR_INVALID_ARG_VALUE');
assert.throws(() => {
res.writeEarlyHints({
link: 'invalid string'
});
}, (err) => err.code === 'ERR_INVALID_ARG_VALUE');
debug('Server sending full response...');
res.end(testResBody);
server.close();
}));
server.listen(0, common.mustCall(() => {
const req = http.request({
port: server.address().port, path: '/', agent: false
});
req.end();
debug('Client sending request...');
req.on('information', common.mustNotCall());
}));
}
{
const server = http.createServer(common.mustCall((req, res) => {
debug('Server sending early hints with CRLF injection...');
assert.throws(() => {
res.writeEarlyHints({
'link': '</styles.css>; rel=preload; as=style',
'X-Custom': 'valid\r\nSet-Cookie: session=evil',
});
}, (err) => err.code === 'ERR_INVALID_CHAR');
assert.throws(() => {
res.writeEarlyHints({
'link': '</styles.css>; rel=preload; as=style',
'X-Custom\r\nSet-Cookie: session=evil': 'value',
});
}, (err) => err.code === 'ERR_INVALID_HTTP_TOKEN');
assert.throws(() => {
res.writeEarlyHints({
link: '</styles.css\r\nSet-Cookie: session=evil>; rel=preload; as=style',
});
}, (err) => err.code === 'ERR_INVALID_ARG_VALUE');
debug('Server sending full response...');
res.end(testResBody);
server.close();
}));
server.listen(0, common.mustCall(() => {
const req = http.request({
port: server.address().port, path: '/', agent: false
});
req.end();
debug('Client sending request...');
req.on('information', common.mustNotCall());
}));
}