mirror of
https://github.com/nodejs/node.git
synced 2025-04-29 22:40:57 +00:00

PR-URL: https://github.com/nodejs/node/pull/44820 Fixes: https://github.com/nodejs/node/issues/44816 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
280 lines
6.7 KiB
JavaScript
280 lines
6.7 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';
|
|
|
|
{
|
|
// Happy flow - string argument
|
|
|
|
const server = http.createServer(common.mustCall((req, res) => {
|
|
debug('Server sending early hints...');
|
|
res.writeEarlyHints({
|
|
link: '</styles.css>; rel=preload; as=style'
|
|
});
|
|
|
|
debug('Server sending full response...');
|
|
res.end(testResBody);
|
|
}));
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
const req = http.request({
|
|
port: server.address().port, path: '/'
|
|
});
|
|
|
|
debug('Client sending request...');
|
|
|
|
req.on('information', common.mustCall((res) => {
|
|
assert.strictEqual(res.headers.link, '</styles.css>; rel=preload; as=style');
|
|
}));
|
|
|
|
req.on('response', common.mustCall((res) => {
|
|
let body = '';
|
|
|
|
assert.strictEqual(res.statusCode, 200, `Final status code was ${res.statusCode}, not 200.`);
|
|
|
|
res.on('data', (chunk) => {
|
|
body += chunk;
|
|
});
|
|
|
|
res.on('end', common.mustCall(() => {
|
|
debug('Got full response.');
|
|
assert.strictEqual(body, testResBody);
|
|
server.close();
|
|
}));
|
|
}));
|
|
|
|
req.end();
|
|
}));
|
|
}
|
|
|
|
{
|
|
// Happy flow - array argument
|
|
|
|
const server = http.createServer(common.mustCall((req, res) => {
|
|
debug('Server sending early hints...');
|
|
res.writeEarlyHints({
|
|
link: [
|
|
'</styles.css>; rel=preload; as=style',
|
|
'</scripts.js>; rel=preload; as=script',
|
|
]
|
|
});
|
|
|
|
debug('Server sending full response...');
|
|
res.end(testResBody);
|
|
}));
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
const req = http.request({
|
|
port: server.address().port, path: '/'
|
|
});
|
|
debug('Client sending request...');
|
|
|
|
req.on('information', common.mustCall((res) => {
|
|
assert.strictEqual(
|
|
res.headers.link,
|
|
'</styles.css>; rel=preload; as=style, </scripts.js>; rel=preload; as=script'
|
|
);
|
|
}));
|
|
|
|
req.on('response', common.mustCall((res) => {
|
|
let body = '';
|
|
|
|
assert.strictEqual(res.statusCode, 200, `Final status code was ${res.statusCode}, not 200.`);
|
|
|
|
res.on('data', (chunk) => {
|
|
body += chunk;
|
|
});
|
|
|
|
res.on('end', common.mustCall(() => {
|
|
debug('Got full response.');
|
|
assert.strictEqual(body, testResBody);
|
|
server.close();
|
|
}));
|
|
}));
|
|
|
|
req.end();
|
|
}));
|
|
}
|
|
|
|
{
|
|
// Happy flow - empty array
|
|
|
|
const server = http.createServer(common.mustCall((req, res) => {
|
|
debug('Server sending early hints...');
|
|
res.writeEarlyHints({
|
|
link: []
|
|
});
|
|
|
|
debug('Server sending full response...');
|
|
res.end(testResBody);
|
|
}));
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
const req = http.request({
|
|
port: server.address().port, path: '/'
|
|
});
|
|
debug('Client sending request...');
|
|
|
|
req.on('information', common.mustNotCall());
|
|
|
|
req.on('response', common.mustCall((res) => {
|
|
let body = '';
|
|
|
|
assert.strictEqual(res.statusCode, 200, `Final status code was ${res.statusCode}, not 200.`);
|
|
|
|
res.on('data', (chunk) => {
|
|
body += chunk;
|
|
});
|
|
|
|
res.on('end', common.mustCall(() => {
|
|
debug('Got full response.');
|
|
assert.strictEqual(body, testResBody);
|
|
server.close();
|
|
}));
|
|
}));
|
|
|
|
req.end();
|
|
}));
|
|
}
|
|
|
|
{
|
|
// Happy flow - object argument with string
|
|
|
|
const server = http.createServer(common.mustCall((req, res) => {
|
|
debug('Server sending early hints...');
|
|
res.writeEarlyHints({
|
|
'link': '</styles.css>; rel=preload; as=style',
|
|
'x-trace-id': 'id for diagnostics'
|
|
});
|
|
|
|
debug('Server sending full response...');
|
|
res.end(testResBody);
|
|
}));
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
const req = http.request({
|
|
port: server.address().port, path: '/'
|
|
});
|
|
debug('Client sending request...');
|
|
|
|
req.on('information', common.mustCall((res) => {
|
|
assert.strictEqual(
|
|
res.headers.link,
|
|
'</styles.css>; rel=preload; as=style'
|
|
);
|
|
assert.strictEqual(res.headers['x-trace-id'], 'id for diagnostics');
|
|
}));
|
|
|
|
req.on('response', common.mustCall((res) => {
|
|
let body = '';
|
|
|
|
assert.strictEqual(res.statusCode, 200, `Final status code was ${res.statusCode}, not 200.`);
|
|
|
|
res.on('data', (chunk) => {
|
|
body += chunk;
|
|
});
|
|
|
|
res.on('end', common.mustCall(() => {
|
|
debug('Got full response.');
|
|
assert.strictEqual(body, testResBody);
|
|
server.close();
|
|
}));
|
|
}));
|
|
|
|
req.end();
|
|
}));
|
|
}
|
|
|
|
{
|
|
// Happy flow - object argument with array of strings
|
|
|
|
const server = http.createServer(common.mustCall((req, res) => {
|
|
debug('Server sending early hints...');
|
|
res.writeEarlyHints({
|
|
'link': [
|
|
'</styles.css>; rel=preload; as=style',
|
|
'</scripts.js>; rel=preload; as=script',
|
|
],
|
|
'x-trace-id': 'id for diagnostics'
|
|
});
|
|
|
|
debug('Server sending full response...');
|
|
res.end(testResBody);
|
|
}));
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
const req = http.request({
|
|
port: server.address().port, path: '/'
|
|
});
|
|
debug('Client sending request...');
|
|
|
|
req.on('information', common.mustCall((res) => {
|
|
assert.strictEqual(
|
|
res.headers.link,
|
|
'</styles.css>; rel=preload; as=style, </scripts.js>; rel=preload; as=script'
|
|
);
|
|
assert.strictEqual(res.headers['x-trace-id'], 'id for diagnostics');
|
|
}));
|
|
|
|
req.on('response', common.mustCall((res) => {
|
|
let body = '';
|
|
|
|
assert.strictEqual(res.statusCode, 200, `Final status code was ${res.statusCode}, not 200.`);
|
|
|
|
res.on('data', (chunk) => {
|
|
body += chunk;
|
|
});
|
|
|
|
res.on('end', common.mustCall(() => {
|
|
debug('Got full response.');
|
|
assert.strictEqual(body, testResBody);
|
|
server.close();
|
|
}));
|
|
}));
|
|
|
|
req.end();
|
|
}));
|
|
}
|
|
|
|
{
|
|
// Happy flow - empty object
|
|
|
|
const server = http.createServer(common.mustCall((req, res) => {
|
|
debug('Server sending early hints...');
|
|
res.writeEarlyHints({});
|
|
|
|
debug('Server sending full response...');
|
|
res.end(testResBody);
|
|
}));
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
const req = http.request({
|
|
port: server.address().port, path: '/'
|
|
});
|
|
debug('Client sending request...');
|
|
|
|
req.on('information', common.mustNotCall());
|
|
|
|
req.on('response', common.mustCall((res) => {
|
|
let body = '';
|
|
|
|
assert.strictEqual(res.statusCode, 200, `Final status code was ${res.statusCode}, not 200.`);
|
|
|
|
res.on('data', (chunk) => {
|
|
body += chunk;
|
|
});
|
|
|
|
res.on('end', common.mustCall(() => {
|
|
debug('Got full response.');
|
|
assert.strictEqual(body, testResBody);
|
|
server.close();
|
|
}));
|
|
}));
|
|
|
|
req.end();
|
|
}));
|
|
}
|