From 185ee018f8903506969b53f263dbc5f955748ba7 Mon Sep 17 00:00:00 2001 From: Steve Herzog Date: Mon, 6 Feb 2023 11:14:16 -0600 Subject: [PATCH] update tests and comments --- lib/internal/validators.js | 8 +++++++ .../test-http-early-hints-invalid-argument.js | 23 +++++++++++++++---- test/parallel/test-http-early-hints.js | 3 ++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/internal/validators.js b/lib/internal/validators.js index c2f4cfca7366ac..c24d9cb3674cd9 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -459,6 +459,14 @@ function validateUnion(value, name, union) { } } +/* + The rules for the Link header field are described here: + https://www.rfc-editor.org/rfc/rfc8288.html#section-3 + + This regex validates any string surrounded by angle brackets + (not necessarily a valid URI reference) followed by zero or more + link-params separated by semicolons. +*/ const linkValueRegExp = /^(?:<[^>]*>)(?:\s*;\s*[^;"\s]+(?:=(")?[^;"\s]*\1)?)*$/; /** diff --git a/test/parallel/test-http-early-hints-invalid-argument.js b/test/parallel/test-http-early-hints-invalid-argument.js index 81fbf02820ff67..ffcda11891c5c0 100644 --- a/test/parallel/test-http-early-hints-invalid-argument.js +++ b/test/parallel/test-http-early-hints-invalid-argument.js @@ -13,6 +13,24 @@ const testResBody = 'response content\n'; 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; ' + }); + }, (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(); @@ -33,11 +51,6 @@ const testResBody = 'response content\n'; { const server = http.createServer(common.mustCall((req, res) => { debug('Server sending early hints...'); - assert.throws(() => { - res.writeEarlyHints({ - link: '; ' - }); - }, (err) => err.code === 'ERR_INVALID_ARG_VALUE'); debug('Server sending full response...'); res.end(testResBody); diff --git a/test/parallel/test-http-early-hints.js b/test/parallel/test-http-early-hints.js index 88a3d663b38fe8..bb631a05a6fb3f 100644 --- a/test/parallel/test-http-early-hints.js +++ b/test/parallel/test-http-early-hints.js @@ -58,6 +58,7 @@ const testResBody = 'response content\n'; res.writeEarlyHints({ link: [ '; rel=preload; as=style', + '; crossorigin; rel=preload; as=script', '; rel=preload; as=script; crossorigin', ] }); @@ -75,7 +76,7 @@ const testResBody = 'response content\n'; req.on('information', common.mustCall((res) => { assert.strictEqual( res.headers.link, - '; rel=preload; as=style, ; rel=preload; as=script; crossorigin' + '; rel=preload; as=style, ; crossorigin; rel=preload; as=script, ; rel=preload; as=script; crossorigin' ); }));