0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-25 08:19:38 +01:00
nodejs/test/parallel/test-fetch.mjs
Michaël Zasso 6ec2253926
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>
2022-02-01 08:17:50 +00:00

33 lines
993 B
JavaScript

// 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();