Skip to content

Commit 185ee01

Browse files
committed
update tests and comments
1 parent 0357bb8 commit 185ee01

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

lib/internal/validators.js

+8
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,14 @@ function validateUnion(value, name, union) {
459459
}
460460
}
461461

462+
/*
463+
The rules for the Link header field are described here:
464+
https://www.rfc-editor.org/rfc/rfc8288.html#section-3
465+
466+
This regex validates any string surrounded by angle brackets
467+
(not necessarily a valid URI reference) followed by zero or more
468+
link-params separated by semicolons.
469+
*/
462470
const linkValueRegExp = /^(?:<[^>]*>)(?:\s*;\s*[^;"\s]+(?:=(")?[^;"\s]*\1)?)*$/;
463471

464472
/**

test/parallel/test-http-early-hints-invalid-argument.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ const testResBody = 'response content\n';
1313
res.writeEarlyHints('bad argument type');
1414
}, (err) => err.code === 'ERR_INVALID_ARG_TYPE');
1515

16+
assert.throws(() => {
17+
res.writeEarlyHints({
18+
link: '</>; '
19+
});
20+
}, (err) => err.code === 'ERR_INVALID_ARG_VALUE');
21+
22+
assert.throws(() => {
23+
res.writeEarlyHints({
24+
link: 'rel=preload; </scripts.js>'
25+
});
26+
}, (err) => err.code === 'ERR_INVALID_ARG_VALUE');
27+
28+
assert.throws(() => {
29+
res.writeEarlyHints({
30+
link: 'invalid string'
31+
});
32+
}, (err) => err.code === 'ERR_INVALID_ARG_VALUE');
33+
1634
debug('Server sending full response...');
1735
res.end(testResBody);
1836
server.close();
@@ -33,11 +51,6 @@ const testResBody = 'response content\n';
3351
{
3452
const server = http.createServer(common.mustCall((req, res) => {
3553
debug('Server sending early hints...');
36-
assert.throws(() => {
37-
res.writeEarlyHints({
38-
link: '</>; '
39-
});
40-
}, (err) => err.code === 'ERR_INVALID_ARG_VALUE');
4154

4255
debug('Server sending full response...');
4356
res.end(testResBody);

test/parallel/test-http-early-hints.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const testResBody = 'response content\n';
5858
res.writeEarlyHints({
5959
link: [
6060
'</styles.css>; rel=preload; as=style',
61+
'</scripts.js>; crossorigin; rel=preload; as=script',
6162
'</scripts.js>; rel=preload; as=script; crossorigin',
6263
]
6364
});
@@ -75,7 +76,7 @@ const testResBody = 'response content\n';
7576
req.on('information', common.mustCall((res) => {
7677
assert.strictEqual(
7778
res.headers.link,
78-
'</styles.css>; rel=preload; as=style, </scripts.js>; rel=preload; as=script; crossorigin'
79+
'</styles.css>; rel=preload; as=style, </scripts.js>; crossorigin; rel=preload; as=script, </scripts.js>; rel=preload; as=script; crossorigin'
7980
);
8081
}));
8182

0 commit comments

Comments
 (0)