mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 18:38:55 +00:00
83 lines
2.5 KiB
JavaScript
83 lines
2.5 KiB
JavaScript
import fetch2, { fetch, Response, Request, Headers } from "node-fetch";
|
|
import * as iso from "isomorphic-fetch";
|
|
import * as vercelFetch from "@vercel/fetch";
|
|
import * as stream from "stream";
|
|
|
|
import { test, expect } from "bun:test";
|
|
|
|
test("node-fetch", () => {
|
|
expect(Response.prototype).toBeInstanceOf(globalThis.Response);
|
|
expect(Request.prototype).toBeInstanceOf(globalThis.Request);
|
|
expect(Headers.prototype).toBeInstanceOf(globalThis.Headers);
|
|
expect(fetch2.default).toBe(fetch2);
|
|
expect(fetch2.Response).toBe(Response);
|
|
});
|
|
|
|
test("node-fetch Headers.raw()", () => {
|
|
const headers = new Headers({ "a": "1" });
|
|
headers.append("Set-Cookie", "b=1");
|
|
headers.append("Set-Cookie", "c=1");
|
|
|
|
expect(headers.raw()).toEqual({
|
|
"set-cookie": ["b=1", "c=1"],
|
|
"a": ["1"],
|
|
});
|
|
});
|
|
|
|
for (const [impl, name] of [
|
|
[fetch, "node-fetch.fetch"],
|
|
[fetch2, "node-fetch.default"],
|
|
[fetch2.default, "node-fetch.default.default"],
|
|
[iso.fetch, "isomorphic-fetch.fetch"],
|
|
[iso.default.fetch, "isomorphic-fetch.default.fetch"],
|
|
[iso.default, "isomorphic-fetch.default"],
|
|
[vercelFetch.default(fetch), "@vercel/fetch.default"],
|
|
]) {
|
|
test(name + " fetches", async () => {
|
|
using server = Bun.serve({
|
|
port: 0,
|
|
fetch(req, server) {
|
|
server.stop();
|
|
return new Response("it works");
|
|
},
|
|
});
|
|
expect(await impl("http://" + server.hostname + ":" + server.port)).toBeInstanceOf(globalThis.Response);
|
|
});
|
|
}
|
|
|
|
test("node-fetch uses node streams instead of web streams", async () => {
|
|
using server = Bun.serve({
|
|
port: 0,
|
|
async fetch(req, server) {
|
|
const body = await req.text();
|
|
expect(body).toBe("the input text");
|
|
return new Response("hello world");
|
|
},
|
|
});
|
|
|
|
{
|
|
const result = await fetch2("http://" + server.hostname + ":" + server.port, {
|
|
body: new stream.Readable({
|
|
read() {
|
|
this.push("the input text");
|
|
this.push(null);
|
|
},
|
|
}),
|
|
method: "POST",
|
|
});
|
|
expect(result.body).toBeInstanceOf(stream.Readable);
|
|
expect(result.body === result.body).toBe(true); // cached lazy getter
|
|
const headersJSON = result.headers.toJSON();
|
|
for (const key of Object.keys(headersJSON)) {
|
|
const value = headersJSON[key];
|
|
headersJSON[key] = Array.isArray(value) ? value : [value];
|
|
}
|
|
expect(result.headers.raw()).toEqual(headersJSON);
|
|
const chunks = [];
|
|
for await (const chunk of result.body) {
|
|
chunks.push(chunk);
|
|
}
|
|
expect(Buffer.concat(chunks).toString()).toBe("hello world");
|
|
}
|
|
});
|