From bfc28e90a8b342535190fd85fa4a4f415dc7fe39 Mon Sep 17 00:00:00 2001 From: Carter Himmel Date: Fri, 15 Jul 2022 12:27:27 -0600 Subject: [PATCH 1/5] esm: expose `User-Agent` header --- lib/internal/modules/esm/fetch_module.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/internal/modules/esm/fetch_module.js b/lib/internal/modules/esm/fetch_module.js index 7638f94b3fe525..b9898bafbb7d7e 100644 --- a/lib/internal/modules/esm/fetch_module.js +++ b/lib/internal/modules/esm/fetch_module.js @@ -52,6 +52,9 @@ function HTTPSGet(url, opts) { }); return https.get(url, { agent: HTTPSAgent, + headers: { + 'User-Agent': `Node.js/${process.version}`, + }, ...opts, }); } @@ -64,6 +67,9 @@ function HTTPGet(url, opts) { }); return http.get(url, { agent: HTTPAgent, + headers: { + 'User-Agent': `Node.js/${process.version}`, + }, ...opts, }); } From b4367093a61a78b8b675c2a6b4b9f67f442cbb93 Mon Sep 17 00:00:00 2001 From: Carter Date: Thu, 15 Sep 2022 16:54:34 -0600 Subject: [PATCH 2/5] chore: fix test --- test/parallel/test-fetch.mjs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/parallel/test-fetch.mjs b/test/parallel/test-fetch.mjs index 2ec4a63e70f032..48929ca8b81b65 100644 --- a/test/parallel/test-fetch.mjs +++ b/test/parallel/test-fetch.mjs @@ -30,6 +30,7 @@ const response = await fetch(`http://localhost:${port}`); assert(response instanceof Response); assert.strictEqual(response.status, 200); assert.strictEqual(response.statusText, 'OK'); +assert.strictEqual(response.headers.get('user-agent'), `Node.js/${process.version}`); const body = await response.text(); assert.strictEqual(body, 'Hello world'); From cef0770bfdc778e75cf4eb1b2fa9dbd7a4f7870e Mon Sep 17 00:00:00 2001 From: Carter Date: Sun, 18 Sep 2022 12:07:30 -0600 Subject: [PATCH 3/5] test: edit casing for header get --- test/parallel/test-fetch.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-fetch.mjs b/test/parallel/test-fetch.mjs index 48929ca8b81b65..260bbff8a7eb93 100644 --- a/test/parallel/test-fetch.mjs +++ b/test/parallel/test-fetch.mjs @@ -30,7 +30,7 @@ const response = await fetch(`http://localhost:${port}`); assert(response instanceof Response); assert.strictEqual(response.status, 200); assert.strictEqual(response.statusText, 'OK'); -assert.strictEqual(response.headers.get('user-agent'), `Node.js/${process.version}`); +assert.strictEqual(response.headers.get('User-Agent'), `Node.js/${process.version}`); const body = await response.text(); assert.strictEqual(body, 'Hello world'); From 75c70157a573c5283de1b0e1e5bc04ec8951e52b Mon Sep 17 00:00:00 2001 From: Carter Date: Sun, 18 Sep 2022 12:13:59 -0600 Subject: [PATCH 4/5] refactor: standardize lowercase header --- lib/internal/modules/esm/fetch_module.js | 4 ++-- test/parallel/test-fetch.mjs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/modules/esm/fetch_module.js b/lib/internal/modules/esm/fetch_module.js index b9898bafbb7d7e..aa7c7ce655cdc1 100644 --- a/lib/internal/modules/esm/fetch_module.js +++ b/lib/internal/modules/esm/fetch_module.js @@ -53,7 +53,7 @@ function HTTPSGet(url, opts) { return https.get(url, { agent: HTTPSAgent, headers: { - 'User-Agent': `Node.js/${process.version}`, + 'user-agent': `Node.js/${process.version}`, }, ...opts, }); @@ -68,7 +68,7 @@ function HTTPGet(url, opts) { return http.get(url, { agent: HTTPAgent, headers: { - 'User-Agent': `Node.js/${process.version}`, + 'user-agent': `Node.js/${process.version}`, }, ...opts, }); diff --git a/test/parallel/test-fetch.mjs b/test/parallel/test-fetch.mjs index 260bbff8a7eb93..48929ca8b81b65 100644 --- a/test/parallel/test-fetch.mjs +++ b/test/parallel/test-fetch.mjs @@ -30,7 +30,7 @@ const response = await fetch(`http://localhost:${port}`); assert(response instanceof Response); assert.strictEqual(response.status, 200); assert.strictEqual(response.statusText, 'OK'); -assert.strictEqual(response.headers.get('User-Agent'), `Node.js/${process.version}`); +assert.strictEqual(response.headers.get('user-agent'), `Node.js/${process.version}`); const body = await response.text(); assert.strictEqual(body, 'Hello world'); From dc621647d0ec4d0d8e4bc36165674d09dd27fc55 Mon Sep 17 00:00:00 2001 From: Carter Date: Sun, 18 Sep 2022 12:58:30 -0600 Subject: [PATCH 5/5] test: finally fix tests The issue was that on L133, the headers object being passed through was overwriting the user-agent header. I've reformatted the `opts` spread so the only way to over-write the header is to explicitily define it --- lib/internal/modules/esm/fetch_module.js | 6 ++++-- test/es-module/test-http-imports.mjs | 2 ++ test/parallel/test-fetch.mjs | 1 - 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/internal/modules/esm/fetch_module.js b/lib/internal/modules/esm/fetch_module.js index aa7c7ce655cdc1..182dfd2a0f6657 100644 --- a/lib/internal/modules/esm/fetch_module.js +++ b/lib/internal/modules/esm/fetch_module.js @@ -52,10 +52,11 @@ function HTTPSGet(url, opts) { }); return https.get(url, { agent: HTTPSAgent, + ...opts, headers: { 'user-agent': `Node.js/${process.version}`, + ...opts?.headers, }, - ...opts, }); } @@ -67,10 +68,11 @@ function HTTPGet(url, opts) { }); return http.get(url, { agent: HTTPAgent, + ...opts, headers: { 'user-agent': `Node.js/${process.version}`, + ...opts?.headers, }, - ...opts, }); } diff --git a/test/es-module/test-http-imports.mjs b/test/es-module/test-http-imports.mjs index 235d142d3555e3..15ef089df338c7 100644 --- a/test/es-module/test-http-imports.mjs +++ b/test/es-module/test-http-imports.mjs @@ -67,6 +67,8 @@ for (const { protocol, createServer } of [ // ?body sets the body, string const server = createServer(function(_req, res) { const url = new URL(_req.url, host); + assert.strictEqual(_req.headers['user-agent'], `Node.js/${process.version}`); + const redirect = url.searchParams.get('redirect'); if (url.pathname === '/not-found') { res.writeHead(404); diff --git a/test/parallel/test-fetch.mjs b/test/parallel/test-fetch.mjs index 48929ca8b81b65..2ec4a63e70f032 100644 --- a/test/parallel/test-fetch.mjs +++ b/test/parallel/test-fetch.mjs @@ -30,7 +30,6 @@ const response = await fetch(`http://localhost:${port}`); assert(response instanceof Response); assert.strictEqual(response.status, 200); assert.strictEqual(response.statusText, 'OK'); -assert.strictEqual(response.headers.get('user-agent'), `Node.js/${process.version}`); const body = await response.text(); assert.strictEqual(body, 'Hello world');