diff options
author | Michaƫl Zasso <targos@protonmail.com> | 2022-02-01 11:17:50 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-01 11:17:50 +0300 |
commit | 6ec225392675c92b102d3caad02ee3a157c9d1b7 (patch) | |
tree | 6836c71b09d64c848e0496eec6d94e0f28a46e6e /test | |
parent | 7123a00b03a908621255dc35f8d75c112901a42c (diff) |
lib: add fetch
Fixes: https://github.com/nodejs/node/issues/19393
PR-URL: https://github.com/nodejs/node/pull/41749
Refs: https://github.com/nodejs/undici/pull/1183
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/common/index.js | 15 | ||||
-rw-r--r-- | test/parallel/test-fetch.mjs | 32 |
2 files changed, 44 insertions, 3 deletions
diff --git a/test/common/index.js b/test/common/index.js index a1744836887..fdffecb9af1 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -68,12 +68,12 @@ if (process.argv.length === 2 && !process.env.NODE_SKIP_FLAG_CHECK && isMainThread && hasCrypto && - require.main && - require('cluster').isPrimary) { + require('cluster').isPrimary && + fs.existsSync(process.argv[1])) { // The copyright notice is relatively big and the flags could come afterwards. const bytesToRead = 1500; const buffer = Buffer.allocUnsafe(bytesToRead); - const fd = fs.openSync(require.main.filename, 'r'); + const fd = fs.openSync(process.argv[1], 'r'); const bytesRead = fs.readSync(fd, buffer, 0, bytesToRead); fs.closeSync(fd); const source = buffer.toString('utf8', 0, bytesRead); @@ -300,6 +300,15 @@ if (global.structuredClone) { knownGlobals.push(global.structuredClone); } +if (global.fetch) { + knownGlobals.push( + global.fetch, + global.Request, + global.Response, + global.Headers, + ); +} + function allowGlobals(...allowlist) { knownGlobals = knownGlobals.concat(allowlist); } diff --git a/test/parallel/test-fetch.mjs b/test/parallel/test-fetch.mjs new file mode 100644 index 00000000000..fa433c78fc7 --- /dev/null +++ b/test/parallel/test-fetch.mjs @@ -0,0 +1,32 @@ +// Flags: --experimental-fetch --no-warnings + +import '../common/index.mjs'; + +import assert from 'assert'; +import events from 'events'; +import http from 'http'; + +assert.strictEqual(typeof globalThis.fetch, 'function'); +assert.strictEqual(typeof globalThis.Headers, 'function'); +assert.strictEqual(typeof globalThis.Request, 'function'); +assert.strictEqual(typeof globalThis.Response, 'function'); + +const server = http.createServer((req, res) => { + // TODO: Remove this once keep-alive behavior can be disabled from the client + // side. + res.setHeader('Keep-Alive', 'timeout=0, max=0'); + res.end('Hello world'); +}); +server.listen(0); +await events.once(server, 'listening'); +const port = server.address().port; + +const response = await fetch(`http://localhost:${port}`); + +assert(response instanceof Response); +assert.strictEqual(response.status, 200); +assert.strictEqual(response.statusText, 'OK'); +const body = await response.text(); +assert.strictEqual(body, 'Hello world'); + +server.close(); |