http2: make early hints generic

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>
This commit is contained in:
Yagiz Nizipli 2022-10-06 18:03:47 +01:00 committed by GitHub
parent aacd742e91
commit 37f1e4bf4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 322 additions and 167 deletions

View file

@ -11,7 +11,9 @@ const testResBody = 'response content\n';
const server = http.createServer(common.mustCall((req, res) => {
debug('Server sending early hints...');
res.writeEarlyHints('</styles.css>; rel=preload; as=style');
res.writeEarlyHints({
link: '</styles.css>; rel=preload; as=style'
});
debug('Server sending full response...');
res.end(testResBody);
@ -53,10 +55,12 @@ const testResBody = 'response content\n';
const server = http.createServer(common.mustCall((req, res) => {
debug('Server sending early hints...');
res.writeEarlyHints([
'</styles.css>; rel=preload; as=style',
'</scripts.js>; rel=preload; as=script',
]);
res.writeEarlyHints({
link: [
'</styles.css>; rel=preload; as=style',
'</scripts.js>; rel=preload; as=script',
]
});
debug('Server sending full response...');
res.end(testResBody);
@ -100,7 +104,147 @@ const testResBody = 'response content\n';
const server = http.createServer(common.mustCall((req, res) => {
debug('Server sending early hints...');
res.writeEarlyHints([]);
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);