/
test-fetch.mjs
33 lines (26 loc) · 1.03 KB
/
test-fetch.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 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.FormData, '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();