// @ts-expect-error no such export import { AnyFunction, serve, ServeOptions, Server, sleep, TCPSocketListener } from "bun"; import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "bun:test"; import { chmodSync, closeSync, ftruncateSync, openSync, rmSync, writeFileSync } from "fs"; import { bunEnv, bunExe, emptyProcessMaxRSS, exampleSite, exampleHtml as fixture, gc, isASAN, isBroken, isDebug, isFlaky, isMacOS, isRoot, isWindows, rss, runFixtureMaxRSS, tempDir, tls, tmpdirSync, withoutAggressiveGC, } from "harness"; import { once } from "events"; import { mkfifo } from "mkfifo"; import type { AddressInfo } from "net"; import net from "net"; import { join } from "path"; import { Readable } from "stream"; import { gzipSync } from "zlib"; import { deadPort } from "../../bun/http/proxy-stress-helpers"; const tmp_dir = tmpdirSync(); const fetchFixture3 = join(import.meta.dir, "fetch-leak-test-fixture-3.js"); const fetchFixture4 = join(import.meta.dir, "fetch-leak-test-fixture-4.js"); let server: Server; function startServer({ fetch, ...options }: ServeOptions) { server = serve({ idleTimeout: 0, ...options, fetch, port: 0, } as ServeOptions); return server; } let httpServer = exampleSite("http"); let httpsServer = exampleSite("https"); afterEach(() => { server?.stop?.(true); }); afterAll(() => { rmSync(tmp_dir, { force: true, recursive: true }); httpServer.stop(); httpsServer.stop(); }); const payload = new Uint8Array(1024 * 1024 * 2); crypto.getRandomValues(payload); it("new Request(invalid url) throws", () => { expect(() => new Request("http")).toThrow(); expect(() => new Request("")).toThrow(); expect(() => new Request("http://[::1")).toThrow(); expect(() => new Request("https://[::1")).toThrow(); expect(() => new Request("!")).toThrow(); }); describe("fetch data urls", () => { it("basic", async () => { var url = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; var res = await fetch(url); expect(res.status).toBe(200); expect(res.statusText).toBe("OK"); expect(res.ok).toBe(true); var blob = await res.blob(); expect(blob.size).toBe(85); expect(blob.type).toBe("image/png"); }); it("percent encoded", async () => { var url = "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D"; var res = await fetch(url); expect(res.status).toBe(200); expect(res.statusText).toBe("OK"); expect(res.ok).toBe(true); var blob = await res.blob(); expect(blob.size).toBe(13); expect(blob.type).toBe("text/plain;charset=utf-8"); expect(blob.text()).resolves.toBe("Hello, World!"); }); it("percent encoded (invalid)", async () => { var url = "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3"; expect(async () => { await fetch(url); }).toThrow("failed to fetch the data URL"); }); it("plain text", async () => { var url = "data:,Hello%2C%20World!"; var res = await fetch(url); expect(res.status).toBe(200); expect(res.statusText).toBe("OK"); expect(res.ok).toBe(true); var blob = await res.blob(); expect(blob.size).toBe(13); expect(blob.type).toBe("text/plain;charset=utf-8"); expect(blob.text()).resolves.toBe("Hello, World!"); url = "data:,helloworld!"; res = await fetch(url); expect(res.status).toBe(200); expect(res.statusText).toBe("OK"); expect(res.ok).toBe(true); blob = await res.blob(); expect(blob.size).toBe(11); expect(blob.type).toBe("text/plain;charset=utf-8"); expect(blob.text()).resolves.toBe("helloworld!"); }); it("unstrict parsing of invalid URL characters", async () => { var url = "data:application/json,{%7B%7D}"; var res = await fetch(url); expect(res.status).toBe(200); expect(res.statusText).toBe("OK"); expect(res.ok).toBe(true); var blob = await res.blob(); expect(blob.size).toBe(4); expect(blob.type).toBe("application/json;charset=utf-8"); expect(blob.text()).resolves.toBe("{{}}"); }); it("unstrict parsing of double percent characters", async () => { var url = "data:application/json,{%%7B%7D%%}%%"; var res = await fetch(url); expect(res.status).toBe(200); expect(res.statusText).toBe("OK"); expect(res.ok).toBe(true); var blob = await res.blob(); expect(blob.size).toBe(9); expect(blob.type).toBe("application/json;charset=utf-8"); expect(blob.text()).resolves.toBe("{%{}%%}%%"); }); it("data url (invalid)", async () => { var url = "data:Hello%2C%20World!"; expect(async () => { await fetch(url); }).toThrow("failed to fetch the data URL"); }); it("emoji", async () => { var url = "data:,πŸ˜€"; var res = await fetch(url); expect(res.status).toBe(200); expect(res.statusText).toBe("OK"); expect(res.ok).toBe(true); var blob = await res.blob(); expect(blob.size).toBe(4); expect(blob.type).toBe("text/plain;charset=utf-8"); expect(blob.text()).resolves.toBe("πŸ˜€"); }); it("should work with Request", async () => { var req = new Request("data:,Hello%2C%20World!"); var res = await fetch(req); expect(res.status).toBe(200); expect(res.statusText).toBe("OK"); expect(res.ok).toBe(true); var blob = await res.blob(); expect(blob.size).toBe(13); expect(blob.type).toBe("text/plain;charset=utf-8"); expect(blob.text()).resolves.toBe("Hello, World!"); req = new Request("data:,πŸ˜€"); res = await fetch(req); expect(res.status).toBe(200); expect(res.statusText).toBe("OK"); expect(res.ok).toBe(true); blob = await res.blob(); expect(blob.size).toBe(4); expect(blob.type).toBe("text/plain;charset=utf-8"); expect(blob.text()).resolves.toBe("πŸ˜€"); }); it("should work with Request (invalid)", async () => { var req = new Request("data:Hello%2C%20World!"); expect(async () => { await fetch(req); }).toThrow("failed to fetch the data URL"); req = new Request("data:Hello%345632"); expect(async () => { await fetch(req); }).toThrow("failed to fetch the data URL"); }); }); describe("AbortSignal", () => { beforeEach(() => { startServer({ async fetch(request) { if (request.url.endsWith("/nodelay")) { return new Response("Hello"); } if (request.url.endsWith("/stream")) { const reader = request.body!.getReader(); const body = new ReadableStream({ async pull(controller) { if (!reader) controller.close(); const { done, value } = await reader.read(); // When no more data needs to be consumed, close the stream if (done) { controller.close(); return; } // Enqueue the next data chunk into our target stream controller.enqueue(value); }, }); return new Response(body); } if (request.method.toUpperCase() === "POST") { const body = await request.text(); return new Response(body); } await sleep(15); return new Response("Hello"); }, }); }); afterEach(() => { server?.stop?.(true); }); it("AbortError", async () => { const controller = new AbortController(); const signal = controller.signal; expect(async () => { async function manualAbort() { await sleep(1); controller.abort(); } await Promise.all([fetch(server.url, { signal: signal }).then(res => res.text()), manualAbort()]); }).toThrow(new DOMException("The operation was aborted.")); }); it("AbortAfterFinish", async () => { const controller = new AbortController(); const signal = controller.signal; await fetch(`http://127.0.0.1:${server.port}/nodelay`, { signal: signal }).then(async res => expect(await res.text()).toBe("Hello"), ); controller.abort(); }); it("AbortErrorWithReason", async () => { const controller = new AbortController(); const signal = controller.signal; expect(async () => { async function manualAbort() { await sleep(10); controller.abort(new Error("My Reason")); } await Promise.all([fetch(server.url, { signal: signal }).then(res => res.text()), manualAbort()]); }).toThrow("My Reason"); }); it("AbortErrorEventListener", async () => { const controller = new AbortController(); const signal = controller.signal; signal.addEventListener("abort", ev => { const target = ev.currentTarget! as AbortSignal; expect(target).toBeDefined(); expect(target.aborted).toBe(true); expect(target.reason).toBeDefined(); expect(target.reason!.name).toBe("AbortError"); }); async function manualAbort() { await sleep(10); controller.abort(); } try { await Promise.all([fetch(server.url, { signal: signal }).then(res => res.text()), manualAbort()]); expect.unreachable(); } catch (e: any) { expect(e?.message).toEqual("The operation was aborted."); expect(e?.name).toEqual("AbortError"); expect(e?.constructor.name).toEqual("DOMException"); } }); it("AbortErrorWhileUploading", async () => { const controller = new AbortController(); try { await fetch(`http://localhost:${server.port}`, { method: "POST", body: new ReadableStream({ pull(event_controller) { console.count("pull"); event_controller.enqueue(new Uint8Array([1, 2, 3, 4])); //this will abort immediately should abort before connected controller.abort(); }, cancel(reason) { console.log("cancel", reason); }, }), signal: controller.signal, }); expect.unreachable(); } catch (ex: any) { expect(ex?.message).toEqual("The operation was aborted."); expect(ex?.name).toEqual("AbortError"); expect(ex?.constructor.name).toEqual("DOMException"); } }); it("abort while uploading prevents pull() from being called", async () => { const controller = new AbortController(); await fetch(`http://localhost:${server.port}`, { method: "POST", body: new Blob(["a"]), }); try { await fetch(`http://localhost:${server.port}`, { method: "POST", body: new ReadableStream({ async pull(event_controller) { expect(controller.signal.aborted).toBeFalse(); const chunk = Buffer.alloc(256 * 1024, "abc"); for (let i = 0; i < 64; i++) { event_controller.enqueue(chunk); } //this will abort immediately should abort before connected controller.abort(); }, }), signal: controller.signal, }); expect.unreachable(); } catch (ex: any) { expect(ex?.message).toEqual("The operation was aborted."); expect(ex?.name).toEqual("AbortError"); expect(ex?.constructor.name).toEqual("DOMException"); } }); it("TimeoutError", async () => { const signal = AbortSignal.timeout(10); try { using server = Bun.serve({ port: 0, async fetch() { await Bun.sleep(100); return new Response("Hello"); }, }); await fetch(server.url, { signal: signal }).then(res => res.text()); expect.unreachable(); } catch (ex: any) { expect(ex.name).toBe("TimeoutError"); expect(ex.message).toBe("The operation timed out."); expect(ex.constructor.name).toBe("DOMException"); } }); it("Request", async () => { const controller = new AbortController(); const signal = controller.signal; async function manualAbort() { await sleep(10); controller.abort(); } try { const request = new Request(server.url as any, { signal }); await Promise.all([fetch(request).then(res => res.text()), manualAbort()]); expect(() => {}).toThrow(); } catch (ex: any) { expect(ex.name).toBe("AbortError"); } }); it("already-aborted signal returns an already-rejected promise", async () => { // Fetch spec step 11: when the signal is already aborted, fetch() must // return an already-rejected promise (not a pending one that settles after // a round-trip to the HTTP thread). { const controller = new AbortController(); const reason = new Error("pre-aborted"); controller.abort(reason); const p = fetch("http://127.0.0.1:1/", { signal: controller.signal }); expect(Bun.peek.status(p)).toBe("rejected"); expect(Bun.peek(p)).toBe(reason); await p.catch(() => {}); } { // default reason β†’ DOMException AbortError, identical to signal.reason const controller = new AbortController(); controller.abort(); const p = fetch("http://127.0.0.1:1/", { signal: controller.signal }); expect(Bun.peek.status(p)).toBe("rejected"); const err: unknown = Bun.peek(p); expect(err).toBeInstanceOf(DOMException); expect((err as DOMException).name).toBe("AbortError"); expect(err).toBe(controller.signal.reason); await p.catch(() => {}); } { // via Request input const controller = new AbortController(); const reason = new Error("pre-aborted-req"); controller.abort(reason); const req = new Request("http://127.0.0.1:1/", { signal: controller.signal }); const p = fetch(req); expect(Bun.peek.status(p)).toBe("rejected"); expect(Bun.peek(p)).toBe(reason); await p.catch(() => {}); } { // AbortSignal.abort() static const reason = new Error("pre-aborted-static"); const p = fetch("http://127.0.0.1:1/", { signal: AbortSignal.abort(reason) }); expect(Bun.peek.status(p)).toBe("rejected"); expect(Bun.peek(p)).toBe(reason); await p.catch(() => {}); } { // plain-object first argument (request_init_object branch) const controller = new AbortController(); const reason = new Error("pre-aborted-init"); controller.abort(reason); const p = fetch({ url: "http://127.0.0.1:1/", signal: controller.signal } as any); expect(Bun.peek.status(p)).toBe("rejected"); expect(Bun.peek(p)).toBe(reason); await p.catch(() => {}); } { // Request-constructor errors (spec step 4) still win over the abort: // GET with a body rejects with TypeError, not the abort reason. const reason = new Error("should-not-see-this"); const p = fetch("http://127.0.0.1:1/", { method: "GET", body: "x", signal: AbortSignal.abort(reason), } as any); expect(Bun.peek.status(p)).toBe("rejected"); expect(Bun.peek(p)).toBeInstanceOf(TypeError); expect(Bun.peek(p)).not.toBe(reason); await p.catch(() => {}); } { // Request input body is consumed (step 4) before the abort (step 11). const controller = new AbortController(); const reason = new Error("pre-aborted-bodyused"); controller.abort(reason); const req = new Request("http://127.0.0.1:1/", { method: "POST", body: "hello", signal: controller.signal, }); const p = fetch(req); expect(Bun.peek.status(p)).toBe("rejected"); expect(Bun.peek(p)).toBe(reason); expect(req.bodyUsed).toBe(true); await p.catch(() => {}); } { // ReadableStream body is cancelled with the abort reason (abort-a-fetch // step: "cancel request's body with error"). let cancelReason: unknown = "not called"; const stream = new ReadableStream({ cancel(r) { cancelReason = r; }, }); const reason = new Error("pre-aborted-stream"); const p = fetch("http://127.0.0.1:1/", { method: "POST", body: stream, signal: AbortSignal.abort(reason), }); expect(Bun.peek.status(p)).toBe("rejected"); expect(Bun.peek(p)).toBe(reason); await p.catch(() => {}); expect(cancelReason).toBe(reason); expect(stream.locked).toBe(false); } }); }); describe("Headers", () => { it(".toJSON", () => { const headers = new Headers({ "content-length": "123", "content-type": "text/plain", "x-another-custom-header": "Hello World", "x-custom-header": "Hello World", }); expect(JSON.stringify(headers.toJSON(), null, 2)).toBe( JSON.stringify(Object.fromEntries(headers.entries()), null, 2), ); }); it(".getSetCookie() with object", () => { const headers = new Headers({ "content-length": "123", "content-type": "text/plain", "x-another-custom-header": "Hello World", "x-custom-header": "Hello World", "Set-Cookie": "foo=bar; Path=/; HttpOnly", }); expect(headers.count).toBe(5); expect(headers.getAll("set-cookie")).toEqual(["foo=bar; Path=/; HttpOnly"]); }); it("presence of content-encoding header(issue #5668)", async () => { startServer({ fetch(req) { const content = gzipSync(JSON.stringify({ message: "Hello world" })); return new Response(content, { status: 200, headers: { "content-encoding": "gzip", "content-type": "application/json", }, }); }, }); const result = await fetch(`http://${server.hostname}:${server.port}/`); const value = result.headers.get("content-encoding"); const body: any = await result.json(); expect(value).toBe("gzip"); expect(body).toBeDefined(); expect(body.message).toBe("Hello world"); }); it(".getSetCookie() with array", () => { const headers = new Headers([ ["content-length", "123"], ["content-type", "text/plain"], ["x-another-custom-header", "Hello World"], ["x-custom-header", "Hello World"], ["Set-Cookie", "foo=bar; Path=/; HttpOnly"], ["Set-Cookie", "foo2=bar2; Path=/; HttpOnly"], ]); expect(headers.count).toBe(6); expect(headers.getAll("set-cookie")).toEqual(["foo=bar; Path=/; HttpOnly", "foo2=bar2; Path=/; HttpOnly"]); }); it("Set-Cookies init", () => { const headers = new Headers([ ["Set-Cookie", "foo=bar"], ["Set-Cookie", "bar=baz"], ["X-bun", "abc"], ["X-bun", "def"], ]); const actual = [...headers]; expect(actual).toEqual([ ["x-bun", "abc, def"], ["set-cookie", "foo=bar"], ["set-cookie", "bar=baz"], ]); expect([...headers.values()]).toEqual(["abc, def", "foo=bar", "bar=baz"]); }); it("Set-Cookies toJSON", () => { const headers = new Headers([ ["Set-Cookie", "foo=bar"], ["Set-Cookie", "bar=baz"], ["X-bun", "abc"], ["X-bun", "def"], ]).toJSON(); expect(headers).toEqual({ "x-bun": "abc, def", "set-cookie": ["foo=bar", "bar=baz"], }); }); it("Headers append multiple", () => { const headers = new Headers([ ["Set-Cookie", "foo=bar"], ["X-bun", "foo"], ]); headers.append("Set-Cookie", "bar=baz"); headers.append("x-bun", "bar"); const actual = [...headers]; // we do not preserve the order // which is kind of bad expect(actual).toEqual([ ["x-bun", "foo, bar"], ["set-cookie", "foo=bar"], ["set-cookie", "bar=baz"], ]); }); it("append duplicate set cookie key", () => { const headers = new Headers([["Set-Cookie", "foo=bar"]]); headers.append("set-Cookie", "foo=baz"); headers.append("Set-cookie", "baz=bar"); const actual = [...headers]; expect(actual).toEqual([ ["set-cookie", "foo=bar"], ["set-cookie", "foo=baz"], ["set-cookie", "baz=bar"], ]); }); it("set duplicate cookie key", () => { const headers = new Headers([["Set-Cookie", "foo=bar"]]); headers.set("set-Cookie", "foo=baz"); headers.set("set-cookie", "bar=qat"); const actual = [...headers]; expect(actual).toEqual([["set-cookie", "bar=qat"]]); }); it("should include set-cookie headers in array", () => { const headers = new Headers(); headers.append("Set-Cookie", "foo=bar"); headers.append("Content-Type", "text/plain"); const actual = [...headers]; expect(actual).toEqual([ ["content-type", "text/plain"], ["set-cookie", "foo=bar"], ]); }); }); describe("fetch", () => { const urls = [ { url: httpsServer.url.href, tls: { ca: httpsServer.ca } }, { url: httpServer.url.href, tls: undefined }, { url: httpsServer.url, tls: { ca: httpsServer.ca } }, { url: new Request({ url: httpsServer.url.href }), tls: { ca: httpsServer.ca } }, { url: { toString: () => httpServer.url.href } as string, tls: undefined }, ]; for (let { url, tls } of urls) { gc(); let name: string; if (url instanceof URL) { name = "URL: " + url; } else if (url instanceof Request) { name = "Request: " + url.url; } else if (url.hasOwnProperty("toString")) { name = "Object: " + url.toString(); } else { name = url as string; } it.concurrent(name, async () => { gc(); const response = await fetch(url, { verbose: true, tls }); gc(); const text = await response.text(); gc(); expect(fixture).toBe(text); }); } it.concurrent('redirect: "manual"', async () => { using server = Bun.serve({ port: 0, fetch(req) { return new Response(null, { status: 302, headers: { Location: "https://example.com", }, }); }, }); const response = await fetch(`http://${server.hostname}:${server.port}`, { redirect: "manual", }); expect(response.status).toBe(302); expect(response.headers.get("location")).toBe("https://example.com"); expect(response.redirected).toBe(false); // not redirected }); it.concurrent('redirect: "follow"', async () => { using target = Bun.serve({ port: 0, tls, fetch() { return new Response("redirected!"); }, }); using server = Bun.serve({ port: 0, fetch(req) { return new Response(null, { status: 302, headers: { Location: target.url.href, }, }); }, }); const response = await fetch(`http://${server.hostname}:${server.port}`, { redirect: "follow", tls: { ca: tls.cert }, }); expect(response.status).toBe(200); expect(response.headers.get("location")).toBe(null); expect(response.redirected).toBe(true); }); it.concurrent('redirect: "error" #2819', async () => { using server = Bun.serve({ port: 0, fetch(req) { return new Response(null, { status: 302, headers: { Location: "https://example.com", }, }); }, }); try { const response = await fetch(`http://${server.hostname}:${server.port}`, { redirect: "error", }); expect(response).toBeUndefined(); } catch (err: any) { expect(err.code).toBe("UnexpectedRedirect"); } }); it.concurrent("should properly redirect to another port #7793", async () => { var socket: net.Server | null = null; try { using server = Bun.serve({ port: 0, tls, fetch() { return new Response("Hello, world!"); }, }); socket = net.createServer(socket => { socket.on("data", () => { // we redirect and close the connection here socket.end(`HTTP/1.1 301 Moved Permanently\r\nLocation: ${server?.url}\r\nConnection: close\r\n\r\n`); }); }); const { promise, resolve, reject } = Promise.withResolvers(); socket.on("error", reject); socket.listen(0, "localhost", async () => { const url = server?.url.href; const http_url = server?.url.href.replace("https://", "http://"); try { await fetch(http_url, { tls: { rejectUnauthorized: false } }); } catch {} const response = await fetch(url, { tls: { rejectUnauthorized: false } }).then(res => res.text()); resolve(response); }); expect(await promise).toBe("Hello, world!"); } finally { socket?.close(); } }); it.concurrent("provide body", async () => { using server = Bun.serve({ port: 0, fetch(req) { return new Response(req.body); }, hostname: "localhost", }); // POST with body const url = `http://${server.hostname}:${server.port}`; const response = await fetch(url, { method: "POST", body: "buntastic" }); expect(response.status).toBe(200); expect(await response.text()).toBe("buntastic"); }); ["GET", "HEAD", "TRACE"].forEach(method => it.concurrent(`fail on ${method} with body`, async () => { // The request is rejected before any network I/O, so the URL is irrelevant. expect(async () => { await fetch("http://example.invalid/", { method, body: "buntastic" }); }).toThrow("fetch() request with GET/HEAD method cannot have body."); }), ); // WHATWG Fetch only forbids a body for GET and HEAD. OPTIONS with content is // legal HTTP (RFC 9110 Β§9.3.7) and must be sent, not rejected. it.concurrent("OPTIONS with body is sent and delivered", async () => { using server = Bun.serve({ port: 0, async fetch(req) { return Response.json({ method: req.method, cl: req.headers.get("content-length"), body: await req.text(), }); }, }); const res = await fetch(server.url, { method: "OPTIONS", body: "PAYLOAD", headers: { "content-type": "text/plain" }, }); expect(await res.json()).toEqual({ method: "OPTIONS", cl: "7", body: "PAYLOAD" }); expect(res.status).toBe(200); }); it.concurrent("content length is inferred", async () => { using server = Bun.serve({ port: 0, fetch(req) { return new Response(req.headers.get("content-length")); }, hostname: "localhost", }); // POST with body const url = `http://${server.hostname}:${server.port}`; const response = await fetch(url, { method: "POST", body: "buntastic" }); expect(response.status).toBe(200); expect(await response.text()).toBe("9"); const response2 = await fetch(url, { method: "POST", body: "" }); expect(response2.status).toBe(200); expect(await response2.text()).toBe("0"); }); it.concurrent("should work with ipv6 localhost", async () => { using server = Bun.serve({ port: 0, fetch(req) { return new Response("Pass!"); }, }); let res = await fetch(`http://[::1]:${server.port}`); expect(await res.text()).toBe("Pass!"); res = await fetch(`http://[::]:${server.port}/`); expect(await res.text()).toBe("Pass!"); res = await fetch(`http://[0:0:0:0:0:0:0:1]:${server.port}/`); expect(await res.text()).toBe("Pass!"); res = await fetch(`http://[0000:0000:0000:0000:0000:0000:0000:0001]:${server.port}/`); expect(await res.text()).toBe("Pass!"); }); }); it.concurrent("simultaneous HTTPS fetch", async () => { const urls = [httpsServer.url.href, httpsServer.url.href]; for (let batch = 0; batch < 4; batch++) { const promises = new Array(20); for (let i = 0; i < 20; i++) { promises[i] = fetch(urls[i % 2], { tls: { ca: httpsServer.ca } }); } const result = await Promise.all(promises); expect(result.length).toBe(20); for (let i = 0; i < 20; i++) { expect(result[i].status).toBe(200); expect(await result[i].text()).toBe(fixture); } } }); it.concurrent("website with tlsextname", async () => { using server = Bun.serve({ port: 0, tls, fetch() { return new Response("OK"); }, }); const resp = await fetch(server.url, { method: "HEAD", tls: { ca: tls.cert } }); expect(resp.status).toBe(200); }); function testBlobInterface(blobbyConstructor: { (..._: any[]): any }, hasBlobFn?: boolean) { for (let withGC of [false, true]) { for (let jsonObject of [ { hello: true }, { hello: "πŸ˜€ πŸ˜ƒ πŸ˜„ 😁 πŸ˜† πŸ˜… πŸ˜‚ 🀣 πŸ₯² ☺️ 😊 πŸ˜‡ πŸ™‚ πŸ™ƒ πŸ˜‰ 😌 😍 πŸ₯° 😘 πŸ˜— πŸ˜™ 😚 πŸ˜‹ πŸ˜› 😝 😜 πŸ€ͺ 🀨 🧐 πŸ€“ 😎 πŸ₯Έ 🀩 πŸ₯³", }, ]) { it.concurrent( `${jsonObject.hello === true ? "latin1" : "utf16"} json${withGC ? " (with gc) " : ""}`, async () => { if (withGC) gc(); var response = blobbyConstructor(JSON.stringify(jsonObject)); if (withGC) gc(); expect(JSON.stringify(await response.json())).toBe(JSON.stringify(jsonObject)); if (withGC) gc(); }, ); it.concurrent( `${jsonObject.hello === true ? "latin1" : "utf16"} arrayBuffer -> json${withGC ? " (with gc) " : ""}`, async () => { if (withGC) gc(); var response = blobbyConstructor(new TextEncoder().encode(JSON.stringify(jsonObject))); if (withGC) gc(); expect(JSON.stringify(await response.json())).toBe(JSON.stringify(jsonObject)); if (withGC) gc(); }, ); it(`${jsonObject.hello === true ? "latin1" : "utf16"} arrayBuffer -> invalid json${ withGC ? " (with gc) " : "" }`, async () => { if (withGC) gc(); var response = blobbyConstructor( new TextEncoder().encode(JSON.stringify(jsonObject) + " NOW WE ARE INVALID JSON"), ); if (withGC) gc(); var failed = false; try { await response.json(); } catch (e) { failed = true; } expect(failed).toBe(true); if (withGC) gc(); }); it(`${jsonObject.hello === true ? "latin1" : "utf16"} text${withGC ? " (with gc) " : ""}`, async () => { if (withGC) gc(); var response = blobbyConstructor(JSON.stringify(jsonObject)); if (withGC) gc(); expect(await response.text()).toBe(JSON.stringify(jsonObject)); if (withGC) gc(); }); it(`${jsonObject.hello === true ? "latin1" : "utf16"} arrayBuffer -> text${ withGC ? " (with gc) " : "" }`, async () => { if (withGC) gc(); var response = blobbyConstructor(new TextEncoder().encode(JSON.stringify(jsonObject))); if (withGC) gc(); expect(await response.text()).toBe(JSON.stringify(jsonObject)); if (withGC) gc(); }); it(`${jsonObject.hello === true ? "latin1" : "utf16"} arrayBuffer${withGC ? " (with gc) " : ""}`, async () => { if (withGC) gc(); var response = blobbyConstructor(JSON.stringify(jsonObject)); if (withGC) gc(); const bytes = new TextEncoder().encode(JSON.stringify(jsonObject)); if (withGC) gc(); const compare = new Uint8Array(await response.arrayBuffer()); if (withGC) gc(); withoutAggressiveGC(() => { for (let i = 0; i < compare.length; i++) { if (withGC) gc(); expect(compare[i]).toBe(bytes[i]); if (withGC) gc(); } }); if (withGC) gc(); }); it(`${jsonObject.hello === true ? "latin1" : "utf16"} bytes${withGC ? " (with gc) " : ""}`, async () => { if (withGC) gc(); var response = blobbyConstructor(JSON.stringify(jsonObject)); if (withGC) gc(); const bytes = new TextEncoder().encode(JSON.stringify(jsonObject)); if (withGC) gc(); const compare = await response.bytes(); if (withGC) gc(); withoutAggressiveGC(() => { for (let i = 0; i < compare.length; i++) { if (withGC) gc(); expect(compare[i]).toBe(bytes[i]); if (withGC) gc(); } }); if (withGC) gc(); }); it(`${jsonObject.hello === true ? "latin1" : "utf16"} arrayBuffer -> arrayBuffer${ withGC ? " (with gc) " : "" }`, async () => { if (withGC) gc(); var response = blobbyConstructor(new TextEncoder().encode(JSON.stringify(jsonObject))); if (withGC) gc(); const bytes = new TextEncoder().encode(JSON.stringify(jsonObject)); if (withGC) gc(); const compare = new Uint8Array(await response.arrayBuffer()); if (withGC) gc(); withoutAggressiveGC(() => { for (let i = 0; i < compare.length; i++) { if (withGC) gc(); expect(compare[i]).toBe(bytes[i]); if (withGC) gc(); } }); if (withGC) gc(); }); it(`${jsonObject.hello === true ? "latin1" : "utf16"} arrayBuffer -> bytes${ withGC ? " (with gc) " : "" }`, async () => { if (withGC) gc(); var response = blobbyConstructor(new TextEncoder().encode(JSON.stringify(jsonObject))); if (withGC) gc(); const bytes = new TextEncoder().encode(JSON.stringify(jsonObject)); if (withGC) gc(); const compare = await response.bytes(); if (withGC) gc(); withoutAggressiveGC(() => { for (let i = 0; i < compare.length; i++) { if (withGC) gc(); expect(compare[i]).toBe(bytes[i]); if (withGC) gc(); } }); if (withGC) gc(); }); hasBlobFn && it(`${jsonObject.hello === true ? "latin1" : "utf16"} blob${withGC ? " (with gc) " : ""}`, async () => { if (withGC) gc(); const text = JSON.stringify(jsonObject); var response = blobbyConstructor(text); if (withGC) gc(); const size = new TextEncoder().encode(text).byteLength; if (withGC) gc(); const blobed = await response.blob(); if (withGC) gc(); expect(blobed instanceof Blob).toBe(true); if (withGC) gc(); expect(blobed.size).toBe(size); if (withGC) gc(); expect(blobed.type).toBe("text/plain;charset=utf-8"); const out = await blobed.text(); expect(out).toBe(text); if (withGC) gc(); await new Promise(resolve => setTimeout(resolve, 1)); if (withGC) gc(); expect(out).toBe(text); const first = new Uint8Array(await blobed.arrayBuffer()); const initial = first[0]; first[0] = 254; const second = new Uint8Array(await blobed.arrayBuffer()); expect(second[0]).toBe(initial); expect(first[0]).toBe(254); }); } } } describe.concurrent("Bun.file", () => { let count = 0; testBlobInterface(data => { const blob = new Blob([data]); const buffer = Bun.peek(blob.arrayBuffer()) as ArrayBuffer; const path = join(tmp_dir, `tmp-${count++}.bytes`); writeFileSync(path, buffer as any); const file = Bun.file(path); expect(blob.size).toBe(file.size); expect(file.lastModified).toBeGreaterThan(0); return file; }); // this test uses libc.so or dylib so we skip on windows it.skipIf(isWindows)("size is Infinity on a fifo", () => { const path = join(tmp_dir, "test-fifo"); mkfifo(path); const { size } = Bun.file(path); expect(size).toBe(Infinity); }); const method = ["arrayBuffer", "text", "json", "bytes"] as const; function forEachMethod(fn: (m: (typeof method)[number]) => any, skip?: AnyFunction) { for (const m of method) { (skip ? it.skip : it)(m, fn(m)); } } // on Windows the creator of the file will be able to read from it so this test is disabled on it; // root can read it anywhere, so it is disabled there as well describe.skipIf(isWindows || isRoot)("bad permissions throws", () => { const path = join(tmp_dir, "my-new-file"); beforeAll(async () => { await Bun.write(path, "hey"); chmodSync(path, 0x000); }); forEachMethod(m => () => { const file = Bun.file(path); expect(async () => await file[m]()).toThrow("permission denied"); }); afterAll(() => { rmSync(path, { force: true }); }); }); describe("non-existent file throws", () => { const path = join(tmp_dir, "does-not-exist"); forEachMethod(m => async () => { const file = Bun.file(path); expect(async () => await file[m]()).toThrow("no such file or directory"); }); }); }); describe("Blob", () => { testBlobInterface(data => new Blob([data])); it("should have expected content type", async () => { var response = new Response("
hello
", { headers: { "content-type": "multipart/form-data;boundary=boundary", }, }); expect((await response.blob()).type).toBe("multipart/form-data;boundary=boundary"); response = new Response("
hello
", { headers: { "content-type": "text/html; charset=utf-8", }, }); expect((await response.blob()).type).toBe("text/html;charset=utf-8"); response = new Response("
hello
", { headers: { "content-type": "octet/stream", }, }); expect((await response.blob()).type).toBe("octet/stream"); response = new Response("
hello
", { headers: { "content-type": "text/plain;charset=utf-8", }, }); expect((await response.blob()).type).toBe("text/plain;charset=utf-8"); }); var blobConstructorValues = [ ["123", "456"], ["123", 456], ["123", "456", "789"], ["123", 456, 789], [1, 2, 3, 4, 5, 6, 7, 8, 9], [Uint8Array.from([1, 2, 3, 4, 5, 6, 7, 9])], [Uint8Array.from([1, 2, 3, 4]), "5678", 9], [new Blob([Uint8Array.from([1, 2, 3, 4])]), "5678", 9], [ new Blob([ new TextEncoder().encode( "πŸ˜€ πŸ˜ƒ πŸ˜„ 😁 πŸ˜† πŸ˜… πŸ˜‚ 🀣 πŸ₯² ☺️ 😊 πŸ˜‡ πŸ™‚ πŸ™ƒ πŸ˜‰ 😌 😍 πŸ₯° 😘 πŸ˜— πŸ˜™ 😚 πŸ˜‹ πŸ˜› 😝 😜 πŸ€ͺ 🀨 🧐 πŸ€“ 😎 πŸ₯Έ 🀩 πŸ₯³", ), ]), ], [ new TextEncoder().encode( "πŸ˜€ πŸ˜ƒ πŸ˜„ 😁 πŸ˜† πŸ˜… πŸ˜‚ 🀣 πŸ₯² ☺️ 😊 πŸ˜‡ πŸ™‚ πŸ™ƒ πŸ˜‰ 😌 😍 πŸ₯° 😘 πŸ˜— πŸ˜™ 😚 πŸ˜‹ πŸ˜› 😝 😜 πŸ€ͺ 🀨 🧐 πŸ€“ 😎 πŸ₯Έ 🀩 πŸ₯³", ), ], ] as any[]; var expected = [ "123456", "123456", "123456789", "123456789", "123456789", "\x01\x02\x03\x04\x05\x06\x07\t", "\x01\x02\x03\x0456789", "\x01\x02\x03\x0456789", "πŸ˜€ πŸ˜ƒ πŸ˜„ 😁 πŸ˜† πŸ˜… πŸ˜‚ 🀣 πŸ₯² ☺️ 😊 πŸ˜‡ πŸ™‚ πŸ™ƒ πŸ˜‰ 😌 😍 πŸ₯° 😘 πŸ˜— πŸ˜™ 😚 πŸ˜‹ πŸ˜› 😝 😜 πŸ€ͺ 🀨 🧐 πŸ€“ 😎 πŸ₯Έ 🀩 πŸ₯³", "πŸ˜€ πŸ˜ƒ πŸ˜„ 😁 πŸ˜† πŸ˜… πŸ˜‚ 🀣 πŸ₯² ☺️ 😊 πŸ˜‡ πŸ™‚ πŸ™ƒ πŸ˜‰ 😌 😍 πŸ₯° 😘 πŸ˜— πŸ˜™ 😚 πŸ˜‹ πŸ˜› 😝 😜 πŸ€ͺ 🀨 🧐 πŸ€“ 😎 πŸ₯Έ 🀩 πŸ₯³", ]; it(`blobConstructorValues`, async () => { for (let i = 0; i < blobConstructorValues.length; i++) { var response = new Blob(blobConstructorValues[i]); const res = await response.text(); if (res !== expected[i]) { throw new Error( `Failed: ${expected[i].split("").map(a => a.charCodeAt(0))}, received: ${res .split("") .map(a => a.charCodeAt(0))}`, ); } expect(res).toBe(expected[i]); } }); for (let withGC of [false, true]) { it(`Blob.slice() ${withGC ? " with gc" : ""}`, async () => { var parts = ["hello", " ", "world"]; if (withGC) gc(); var str = parts.join(""); if (withGC) gc(); var combined = new Blob(parts); if (withGC) gc(); for (let part of parts) { if (withGC) gc(); expect(await combined.slice(str.indexOf(part), str.indexOf(part) + part.length).text()).toBe(part); if (withGC) gc(); } if (withGC) gc(); for (let part of parts) { if (withGC) gc(); expect(await combined.slice(str.indexOf(part), str.indexOf(part) + part.length).text()).toBe(part); if (withGC) gc(); } }); } }); { const sample = new TextEncoder().encode("Hello World!"); const typedArrays = [ Uint8Array, Uint8ClampedArray, Int8Array, Uint16Array, Int16Array, Uint32Array, Int32Array, Float32Array, Float64Array, ]; const Constructors = [Blob, Response, Request]; for (let withGC of [false, true]) { for (let TypedArray of typedArrays) { for (let Constructor of Constructors) { it(`${Constructor.name} arrayBuffer() with ${TypedArray.name}${withGC ? " with gc" : ""}`, async () => { const data = new TypedArray(sample); if (withGC) gc(); const input = Constructor === Blob ? [data] : Constructor === Request ? { body: data, url: "http://example.com" } : data; if (withGC) gc(); const blob = new Constructor(input as any); if (withGC) gc(); const out = await blob.arrayBuffer(); if (withGC) gc(); expect(out instanceof ArrayBuffer).toBe(true); if (withGC) gc(); expect(out.byteLength).toBe(data.byteLength); if (withGC) gc(); }); } } } } describe("Response", () => { describe("Response.json", () => { it("works", async () => { const inputs = ["hellooo", [[123], 456, 789], { hello: "world" }, { ok: "πŸ˜‰ 😌 😍 πŸ₯° 😘 " }]; for (let input of inputs) { const output = JSON.stringify(input); expect(await Response.json(input).text()).toBe(output); } // JSON.stringify() returns undefined expect(await Response.json().text()).toBe(""); // JSON.stringify("") returns '""' expect(await Response.json("").text()).toBe('""'); }); it("sets the content-type header", () => { let response = Response.json("hello"); expect(response.type).toBe("default"); expect(response.headers.get("content-type")).toBe("application/json;charset=utf-8"); expect(response.status).toBe(200); }); it("supports number status code", () => { let response = Response.json("hello", 407); expect(response.type).toBe("default"); expect(response.headers.get("content-type")).toBe("application/json;charset=utf-8"); expect(response.status).toBe(407); }); it("supports headers", () => { var response = Response.json("hello", { headers: { "content-type": "potato", "x-hello": "world", }, status: 408, }); expect(response.headers.get("x-hello")).toBe("world"); expect(response.status).toBe(408); }); it("throws TypeError for non-JSON serializable top-level values (Node.js compatibility)", () => { // Symbol, Function, and undefined should throw "Value is not JSON serializable" expect(() => Response.json(Symbol("test"))).toThrow("Value is not JSON serializable"); expect(() => Response.json(function () {})).toThrow("Value is not JSON serializable"); expect(() => Response.json(undefined)).toThrow("Value is not JSON serializable"); // These should not throw (valid values) expect(() => Response.json(null)).not.toThrow(); expect(() => Response.json({})).not.toThrow(); expect(() => Response.json("string")).not.toThrow(); expect(() => Response.json(123)).not.toThrow(); expect(() => Response.json(true)).not.toThrow(); expect(() => Response.json([1, 2, 3])).not.toThrow(); // Objects containing non-serializable values should not throw at top-level // (they get filtered out by JSON.stringify) expect(() => Response.json({ symbol: Symbol("test") })).not.toThrow(); expect(() => Response.json({ func: function () {} })).not.toThrow(); expect(() => Response.json({ undef: undefined })).not.toThrow(); // BigInt should throw with Node.js compatible error message expect(() => Response.json(123n)).toThrow("Do not know how to serialize a BigInt"); }); }); describe("Response.redirect", () => { it("works", () => { // Location is the serialization of the parsed url, so an empty path // gains a trailing "/". https://fetch.spec.whatwg.org/#dom-response-redirect const inputs = [ ["http://example.com", "http://example.com/"], ["http://example.com/", "http://example.com/"], ["http://example.com/hello", "http://example.com/hello"], ["http://example.com/hello/", "http://example.com/hello/"], ["http://example.com/hello/world", "http://example.com/hello/world"], ["http://example.com/hello/world/", "http://example.com/hello/world/"], ]; for (const [input, expected] of inputs) { expect(Response.redirect(input).headers.get("Location")).toBe(expected); } }); it("supports headers", () => { var response = Response.redirect("https://example.com", { headers: { "content-type": "potato", "x-hello": "world", Location: "https://wrong.com", }, status: 307, }); expect(response.headers.get("x-hello")).toBe("world"); expect(response.headers.get("Location")).toBe("https://example.com/"); expect(response.status).toBe(307); expect(response.type).toBe("default"); expect(response.ok).toBe(false); }); }); describe("Response.error", () => { it("works", () => { expect(Response.error().type).toBe("error"); expect(Response.error().ok).toBe(false); expect(Response.error().status).toBe(0); }); }); it("clone", async () => { gc(); var body = new Response("
hello
", { headers: { "content-type": "text/html; charset=utf-8", }, }); gc(); var clone = body.clone(); gc(); body.headers.set("content-type", "text/plain"); gc(); expect(clone.headers.get("content-type")).toBe("text/html; charset=utf-8"); gc(); expect(body.headers.get("content-type")).toBe("text/plain"); gc(); expect(await clone.text()).toBe("
hello
"); gc(); }); it("invalid json", async () => { gc(); var body = new Response("
hello
", { headers: { "content-type": "text/html; charset=utf-8", }, }); try { await body.json(); expect.unreachable(); } catch (exception) { expect(exception instanceof SyntaxError).toBe(true); } }); describe("should consume body correctly", async () => { it("with text first", async () => { var response = new Response("
hello
"); expect(response.bodyUsed).toBe(false); const promise = response.text(); expect(response.bodyUsed).toBe(true); expect(await promise).toBe("
hello
"); expect(response.bodyUsed).toBe(true); expect(async () => { await response.text(); }).toThrow("Body already used"); expect(async () => { await response.json(); }).toThrow("Body already used"); expect(async () => { await response.formData(); }).toThrow("Body already used"); expect(async () => { await response.blob(); }).toThrow("Body already used"); expect(async () => { await response.arrayBuffer(); }).toThrow("Body already used"); }); it("with json first", async () => { var response = new Response('{ "hello": "world" }'); expect(response.bodyUsed).toBe(false); const promise = response.json(); expect(response.bodyUsed).toBe(true); expect(await promise).toEqual({ "hello": "world" }); expect(response.bodyUsed).toBe(true); expect(async () => { await response.json(); }).toThrow("Body already used"); expect(async () => { await response.text(); }).toThrow("Body already used"); expect(async () => { await response.formData(); }).toThrow("Body already used"); expect(async () => { await response.blob(); }).toThrow("Body already used"); expect(async () => { await response.arrayBuffer(); }).toThrow("Body already used"); }); it("with formData first", async () => { var response = new Response("--boundary--", { headers: { "content-type": "multipart/form-data;boundary=boundary", }, }); expect(response.bodyUsed).toBe(false); const promise = response.formData(); expect(response.bodyUsed).toBe(true); expect(await promise).toBeInstanceOf(FormData); expect(response.bodyUsed).toBe(true); expect(async () => { await response.formData(); }).toThrow("Body already used"); expect(async () => { await response.text(); }).toThrow("Body already used"); expect(async () => { await response.json(); }).toThrow("Body already used"); expect(async () => { await response.blob(); }).toThrow("Body already used"); expect(async () => { await response.arrayBuffer(); }).toThrow("Body already used"); }); it("with blob first", async () => { var response = new Response("
hello
"); expect(response.bodyUsed).toBe(false); const promise = response.blob(); expect(response.bodyUsed).toBe(true); expect(await promise).toBeInstanceOf(Blob); expect(response.bodyUsed).toBe(true); expect(async () => { await response.blob(); }).toThrow("Body already used"); expect(async () => { await response.bytes(); }).toThrow("Body already used"); expect(async () => { await response.text(); }).toThrow("Body already used"); expect(async () => { await response.json(); }).toThrow("Body already used"); expect(async () => { await response.formData(); }).toThrow("Body already used"); expect(async () => { await response.arrayBuffer(); }).toThrow("Body already used"); }); it("with arrayBuffer first", async () => { var response = new Response("
hello
"); expect(response.bodyUsed).toBe(false); const promise = response.arrayBuffer(); expect(response.bodyUsed).toBe(true); expect(await promise).toBeInstanceOf(ArrayBuffer); expect(response.bodyUsed).toBe(true); expect(async () => { await response.arrayBuffer(); }).toThrow("Body already used"); expect(async () => { await response.text(); }).toThrow("Body already used"); expect(async () => { await response.json(); }).toThrow("Body already used"); expect(async () => { await response.formData(); }).toThrow("Body already used"); expect(async () => { await response.blob(); }).toThrow("Body already used"); }); it("with Bun.file() streams", async () => { var stream = Bun.file(join(import.meta.dir, "fixtures/file.txt")).stream(); expect(stream instanceof ReadableStream).toBe(true); var input = new Response((await new Response(stream).blob()).stream()).arrayBuffer(); var output = Bun.file(join(import.meta.dir, "/fixtures/file.txt")).arrayBuffer(); expect(await input).toEqual(await output); }); it("with Bun.file() with request/response", async () => { startServer({ async fetch(request: Request) { var text = await request.text(); expect(async () => { await request.arrayBuffer(); }).toThrow(); return (response = new Response((await new Response(text).blob()).stream())); }, }); var response = await fetch(server.url, { method: "POST", body: await Bun.file(import.meta.dir + "/fixtures/file.txt").arrayBuffer(), }); const input = await response.bytes(); var output = await Bun.file(import.meta.dir + "/fixtures/file.txt").stream(); let chunks: Uint8Array[] = []; const reader = output.getReader(); while (true) { const { done, value } = await reader.read(); if (done) break; chunks.push(value); } expect(input).toEqual(Buffer.concat(chunks)); }); }); it("should work with bigint", () => { var r = new Response("hello status", { status: 200n as any }); expect(r.status).toBe(200); r = new Response("hello status", { status: 599n as any }); expect(r.status).toBe(599); r = new Response("hello status", { status: BigInt(200) as any }); expect(r.status).toBe(200); r = new Response("hello status", { status: BigInt(599) as any }); expect(r.status).toBe(599); }); testBlobInterface(data => new Response(data), true); }); describe("Request", () => { it("clone", async () => { gc(); var body = new Request("https://hello.com", { headers: { "content-type": "text/html; charset=utf-8", }, body: "
hello
", }); gc(); expect(body.signal).toBeDefined(); gc(); expect(body.headers.get("content-type")).toBe("text/html; charset=utf-8"); gc(); var clone = body.clone(); gc(); expect(clone.signal).toBeDefined(); gc(); body.headers.set("content-type", "text/plain"); gc(); expect(clone.headers.get("content-type")).toBe("text/html; charset=utf-8"); gc(); expect(body.headers.get("content-type")).toBe("text/plain"); gc(); expect(await clone.text()).toBe("
hello
"); }); it("signal", async () => { gc(); const controller = new AbortController(); const req = new Request("https://hello.com", { signal: controller.signal }); expect(req.signal.aborted).toBe(false); gc(); controller.abort(); gc(); expect(req.signal.aborted).toBe(true); }); it("copies method (#6144)", () => { const request = new Request("http://localhost:1337/test", { method: "POST", }); const new_req = new Request(request, { body: JSON.stringify({ message: "Hello world" }), }); expect(new_req.method).toBe("POST"); }); it("cloned signal", async () => { gc(); const controller = new AbortController(); const req = new Request("https://hello.com", { signal: controller.signal }); expect(req.signal.aborted).toBe(false); gc(); controller.abort(); gc(); expect(req.signal.aborted).toBe(true); gc(); const cloned = req.clone(); expect(cloned.signal.aborted).toBe(true); }); testBlobInterface(data => new Request("https://hello.com", { body: data }), true); }); describe("Headers", () => { it("writes", async () => { var headers = new Headers({ "content-type": "text/html; charset=utf-8", }); gc(); expect(headers.get("content-type")).toBe("text/html; charset=utf-8"); gc(); headers.delete("content-type"); gc(); expect(headers.get("content-type")).toBe(null); gc(); headers.append("content-type", "text/plain"); gc(); expect(headers.get("content-type")).toBe("text/plain"); gc(); headers.append("content-type", "text/plain"); gc(); expect(headers.get("content-type")).toBe("text/plain, text/plain"); gc(); headers.set("content-type", "text/html; charset=utf-8"); gc(); expect(headers.get("content-type")).toBe("text/html; charset=utf-8"); headers.delete("content-type"); gc(); expect(headers.get("content-type")).toBe(null); gc(); }); }); it("body nullable", async () => { gc(); { const req = new Request("https://hello.com", { body: null }); expect(req.body).toBeNull(); } gc(); { const req = new Request("https://hello.com", { body: undefined }); expect(req.body).toBeNull(); } gc(); { const req = new Request("https://hello.com"); expect(req.body).toBeNull(); } gc(); { const req = new Request("https://hello.com", { body: "" }); expect(req.body).not.toBeNull(); } }); it("Request({}) throws", async () => { // @ts-expect-error expect(() => new Request({})).toThrow(); }); it("Request({toString() { throw 'wat'; } }) throws", async () => { expect( () => new Request({ toString() { throw "wat"; }, } as any), ).toThrow("wat"); }); it("should not be able to parse json from empty body", () => { expect(async () => await new Response().json()).toThrow(SyntaxError); expect(async () => await new Request("http://example.com/").json()).toThrow(SyntaxError); }); it("#874", () => { expect(new Request(new Request("https://example.com"), {}).url).toBe("https://example.com/"); expect(new Request(new Request("https://example.com")).url).toBe("https://example.com/"); expect(new Request({ url: "https://example.com" }).url).toBe("https://example.com/"); }); it("#2794", () => { expect(typeof globalThis.fetch.bind).toBe("function"); expect(typeof Bun.fetch.bind).toBe("function"); }); it("#3545", () => { expect(() => fetch("http://example.com?a=b")).not.toThrow(); }); it("invalid header doesnt crash", () => { expect(() => fetch("http://example.com", { headers: { ["lol!!!!!" + "emoji" + "πŸ˜€"]: "hello", }, }), ).toThrow(); }); it("new Request(https://example.com, otherRequest) uses url from left instead of right", () => { const req1 = new Request("http://localhost/abc", { headers: { foo: "bar", }, }); // Want to rewrite the URL with keeping header values const req2 = new Request("http://localhost/def", req1); // Should be `http://localhost/def` But actual: http://localhost/abc expect(req2.url).toBe("http://localhost/def"); expect(req2.headers.get("foo")).toBe("bar"); }); it("fetch() file:// works", async () => { expect(await (await fetch(import.meta.url)).text()).toEqual(await Bun.file(import.meta.path).text()); expect(await (await fetch(new URL("fetch.test.ts", import.meta.url))).text()).toEqual( await Bun.file(Bun.fileURLToPath(new URL("fetch.test.ts", import.meta.url))).text(), ); gc(true); var fileResponse = await fetch(new URL("file with space in the name.txt", import.meta.url)); gc(true); var fileResponseText = await fileResponse.text(); gc(true); var bunFile = Bun.file(Bun.fileURLToPath(new URL("file with space in the name.txt", import.meta.url))); gc(true); var bunFileText = await bunFile.text(); gc(true); expect(fileResponseText).toEqual(bunFileText); gc(true); }); it("fetch() file:// rejects a host that is not this machine", async () => { const path = Bun.fileURLToPath(new URL("fixture.html", import.meta.url)); const expected = await Bun.file(path).text(); const pathname = new URL(import.meta.url).pathname.replace(/[^/]*$/, "fixture.html"); const outcome = (url: string) => fetch(url).then( r => r.text(), e => `${e.name} ${e.code}: ${e.message}`, ); expect(await outcome(`file://${pathname}`)).toBe(expected); expect(await outcome(`file://localhost${pathname}`)).toBe(expected); expect(await outcome(`file://LOCALHOST${pathname}`)).toBe(expected); const rejected = `TypeError ERR_INVALID_FILE_URL_HOST: File URL host must be "localhost" or empty` + (isWindows ? ": fetch() does not read UNC paths" : ` on ${process.platform}`); expect(await outcome(`file://any.host${pathname}`)).toBe(rejected); expect(await outcome(`file://127.0.0.1${pathname}`)).toBe(rejected); // fileURLToPath's other rule: an encoded separator would become a real one. const encoded = "TypeError ERR_INVALID_FILE_URL_PATH: File URL path must not include encoded " + (isWindows ? "\\ or / characters" : "/ characters"); expect(await outcome(`file://${pathname.replace(/\/([^/]*)$/, "%2F$1")}`)).toBe(encoded); expect(await outcome(`file://${pathname.replace(/\/([^/]*)$/, "/sub/..%2f$1")}`)).toBe(encoded); }); it("fetch() file:// rejects a path with an encoded NUL", async () => { using dir = tempDir("fetch-file-nul", { "note.txt": "NOTE-TXT" }); const url = Bun.pathToFileURL(join(String(dir), "note.txt")).href; await using proc = Bun.spawn({ cmd: [ bunExe(), "-e", `for (const url of process.argv.slice(1)) { const outcome = fetch(url).then( async r => r.status + " " + (await r.text()), e => e.name + " " + e.code + ": " + e.message, ); console.log(await outcome); }`, url, url + "%00.png", url + "%00", ], env: bunEnv, stdout: "pipe", stderr: "inherit", }); const [stdout, exitCode] = await Promise.all([proc.stdout.text(), proc.exited]); // Bun.file(url) and node:fs report the same error for this URL. const rejected = (suffix: string) => "TypeError ERR_INVALID_ARG_VALUE: The argument 'path' must be a string, Uint8Array, or URL without null bytes. " + `Received ${JSON.stringify(decodeURIComponent(new URL(url).pathname) + suffix)}`; expect(stdout.split("\n")).toEqual(["200 NOTE-TXT", rejected("\0.png"), rejected("\0"), ""]); expect(exitCode).toBe(0); }); it("proxy: true is rejected, since it names no proxy", async () => { expect(await fetch("http://example.invalid/", { proxy: true } as any).catch(e => e.code)).toBe( "ERR_INVALID_ARG_TYPE", ); expect(() => new Bun.FetchSession({ proxy: true } as any)).toThrow( 'The "proxy" argument must be a string, a URL, an object with a "url", or false. Received type boolean (true)', ); }); it("URL userinfo is sent as Basic credentials unless an Authorization header is given", async () => { const seen: Record = {}; using server = Bun.serve({ port: 0, fetch(req) { seen[new URL(req.url).pathname] = req.headers.get("authorization"); return new Response(req.url); }, }); const base = `localhost:${server.port}`; const urls = await Promise.all( [ fetch(`http://user:p%40ss@${base}/a`), fetch(`http://user@${base}/b`), fetch(new Request(`http://user:pass@${base}/c`)), fetch(`http://user:pass@${base}/d`, { headers: { authorization: "Bearer explicit" } }), fetch(`http://${base}/e`), ].map(p => p.then(r => r.text())), ); // The request line and Host never carry the userinfo. expect(urls).toEqual(["a", "b", "c", "d", "e"].map(p => `http://${base}/${p}`)); expect(seen).toEqual({ "/a": `Basic ${btoa("user:p@ss")}`, "/b": `Basic ${btoa("user:")}`, "/c": `Basic ${btoa("user:pass")}`, "/d": "Bearer explicit", "/e": null, }); }); it("URL userinfo does not displace the Content-Type a body brings", async () => { using server = Bun.serve({ port: 0, async fetch(req) { const form = await req.formData(); return Response.json({ authorization: req.headers.get("authorization"), field: form.get("field") }); }, }); const body = new FormData(); body.set("field", "value"); const response = await fetch(`http://user:pass@localhost:${server.port}/`, { method: "POST", body }); expect(await response.json()).toEqual({ authorization: `Basic ${btoa("user:pass")}`, field: "value" }); }); it("URL credentials are not forwarded on a cross-origin redirect", async () => { const seen: (string | null)[] = []; using target = Bun.serve({ port: 0, fetch(req) { seen.push(req.headers.get("authorization")); return new Response("target"); }, }); using origin = Bun.serve({ port: 0, fetch(req) { seen.push(req.headers.get("authorization")); return Response.redirect(`http://localhost:${target.port}/`, 302); }, }); expect(await (await fetch(`http://user:pass@localhost:${origin.port}/`)).text()).toBe("target"); expect(seen).toEqual([`Basic ${btoa("user:pass")}`, null]); }); it("URL credentials survive a same-origin redirect whose Location leaves them out", async () => { const seen: [string, string | null][] = []; using server = Bun.serve({ port: 0, fetch(req) { const { pathname } = new URL(req.url); seen.push([pathname, req.headers.get("authorization")]); return pathname === "/a" ? Response.redirect(`http://localhost:${server.port}/b`, 302) : new Response("b"); }, }); expect(await (await fetch(`http://user:pass@localhost:${server.port}/a`)).text()).toBe("b"); const basic = `Basic ${btoa("user:pass")}`; expect(seen).toEqual([ ["/a", basic], ["/b", basic], ]); }); it("connection failures reject with an errno-style code that the message starts with", async () => { using dead = await deadPort(); const refused = await fetch(`http://127.0.0.1:${dead.port}/`).catch(e => e); expect({ name: refused.name, code: refused.code, message: refused.message }).toEqual({ name: "TypeError", code: "ECONNREFUSED", message: "ECONNREFUSED: Unable to connect. Is the computer able to access the url?", }); // A name with several addresses, none of which accepts: the path Windows reports differently. const viaName = await fetch(`http://localhost:${dead.port}/`).catch(e => e); expect(viaName.code).toBe("ECONNREFUSED"); // The URL an error names does not carry the URL's credentials. const withUserinfo = await fetch(`http://user:secret@127.0.0.1:${dead.port}/x`).catch(e => e); expect(withUserinfo.path).toBe(`http://127.0.0.1:${dead.port}/x`); const resetter = net.createServer(socket => socket.once("data", () => socket.destroy())); resetter.listen(0, "127.0.0.1"); await once(resetter, "listening"); try { const reset = await fetch(`http://127.0.0.1:${(resetter.address() as net.AddressInfo).port}/`, { method: "POST", body: "not retried", }).catch(e => e); expect({ name: reset.name, code: reset.code, message: reset.message }).toEqual({ name: "TypeError", code: "ECONNRESET", message: "ECONNRESET: The socket connection was closed unexpectedly. For more information, pass `verbose: true` in the second argument to fetch()", }); } finally { resetter.close(); } using looping = Bun.serve({ port: 0, fetch: req => Response.redirect(req.url, 302) }); const tooMany = await fetch(looping.url, { maxRedirects: 2 }).catch(e => e); expect({ code: tooMany.code, message: tooMany.message }).toEqual({ code: "TooManyRedirects", message: "TooManyRedirects: The response redirected too many times. For more information, pass `verbose: true` in the second argument to fetch()", }); }); it("cloned response headers are independent before accessing", () => { const response = new Response("hello", { headers: { "content-type": "text/html; charset=utf-8", }, }); const cloned = response.clone(); cloned.headers.set("content-type", "text/plain"); expect(response.headers.get("content-type")).toBe("text/html; charset=utf-8"); }); it("cloned response headers are independent after accessing", () => { const response = new Response("hello", { headers: { "content-type": "text/html; charset=utf-8", }, }); // create the headers response.headers; const cloned = response.clone(); cloned.headers.set("content-type", "text/plain"); expect(response.headers.get("content-type")).toBe("text/html; charset=utf-8"); }); it("should work with http 100 continue", async () => { let server: net.Server | undefined; try { server = net.createServer(socket => { socket.on("data", data => { const lines = data.toString().split("\r\n"); for (const line of lines) { if (line.length == 0) { socket.write("HTTP/1.1 100 Continue\r\n\r\n"); socket.write("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\nHello, World!"); break; } } }); }); const { promise: start, resolve } = Promise.withResolvers(); server.listen(8080, resolve); await start; const address = server.address() as net.AddressInfo; const result = await fetch(`http://localhost:${address.port}`).then(r => r.text()); expect(result).toBe("Hello, World!"); } finally { server?.close(); } }); it("should work with http 100 continue on the same buffer", async () => { let server: net.Server | undefined; try { server = net.createServer(socket => { socket.on("data", data => { const lines = data.toString().split("\r\n"); for (const line of lines) { if (line.length == 0) { socket.write( "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\nHello, World!", ); break; } } }); }); const { promise: start, resolve } = Promise.withResolvers(); server.listen(8080, resolve); await start; const address = server.address() as net.AddressInfo; const result = await fetch(`http://localhost:${address.port}`).then(r => r.text()); expect(result).toBe("Hello, World!"); } finally { server?.close(); } }); describe("should strip headers", () => { it("status code 303", async () => { using server = Bun.serve({ port: 0, async fetch(request: Request) { if (request.url.endsWith("/redirect")) { return new Response("hello", { headers: { ...request.headers, "Location": "/redirected", }, status: 303, }); } return new Response("hello", { headers: request.headers, }); }, }); const { headers, url, redirected } = await fetch(`http://${server.hostname}:${server.port}/redirect`, { method: "POST", headers: { "I-Am-Here": "yes", "Content-Language": "This should be stripped", }, }); expect(headers.get("I-Am-Here")).toBe("yes"); expect(headers.get("Content-Language")).toBeNull(); expect(url).toEndWith("/redirected"); expect(redirected).toBe(true); }); it("cross-origin status code 302", async () => { await using server1 = Bun.serve({ port: 0, async fetch(request: Request) { if (request.url.endsWith("/redirect")) { return new Response("hello", { headers: { ...request.headers, "Location": `http://${server2.hostname}:${server2.port}/redirected`, }, status: 302, }); } return new Response("hello", { headers: request.headers, }); }, }); await using server2 = Bun.serve({ port: 0, async fetch(request: Request, server) { if (request.url.endsWith("/redirect")) { return new Response("hello", { headers: { ...request.headers, "Location": `http://${server.hostname}:${server.port}/redirected`, }, status: 302, }); } return new Response("hello", { headers: request.headers, }); }, }); const { headers, url, redirected } = await fetch(`http://${server1.hostname}:${server1.port}/redirect`, { method: "GET", headers: { "Authorization": "yes", "Proxy-Authorization": "yes", "Cookie": "yes", }, }); expect(headers.get("Authorization")).toBeNull(); expect(headers.get("Proxy-Authorization")).toBeNull(); expect(headers.get("Cookie")).toBeNull(); expect(url).toEndWith("/redirected"); expect(redirected).toBe(true); }); }); it("same-origin status code 302 should not strip headers", async () => { using server = Bun.serve({ port: 0, async fetch(request: Request, server) { if (request.url.endsWith("/redirect")) { return new Response("hello", { headers: { ...request.headers, "Location": `http://${server.hostname}:${server.port}/redirected`, }, status: 302, }); } return new Response("hello", { headers: request.headers, }); }, }); const { headers, url, redirected } = await fetch(`http://${server.hostname}:${server.port}/redirect`, { method: "GET", headers: { "Authorization": "yes", "Proxy-Authorization": "yes", "Cookie": "yes", }, }); expect(headers.get("Authorization")).toEqual("yes"); expect(headers.get("Proxy-Authorization")).toEqual("yes"); expect(headers.get("Cookie")).toEqual("yes"); expect(url).toEndWith("/redirected"); expect(redirected).toBe(true); }); describe("should handle relative location in the redirect, issue#5635", () => { let server: Server; beforeAll(async () => { server = Bun.serve({ port: 0, async fetch(request: Request) { return new Response("Not Found", { status: 404, }); }, }); }); afterAll(() => { server.stop(true); }); it.each([ ["/a/b", "/c", "/c"], ["/a/b", "c", "/a/c"], ["/a/b", "/c/d", "/c/d"], ["/a/b", "c/d", "/a/c/d"], ["/a/b", "../c", "/c"], ["/a/b", "../c/d", "/c/d"], ["/a/b", "../../../c", "/c"], // slash ["/a/b/", "/c", "/c"], ["/a/b/", "c", "/a/b/c"], ["/a/b/", "/c/d", "/c/d"], ["/a/b/", "c/d", "/a/b/c/d"], ["/a/b/", "../c", "/a/c"], ["/a/b/", "../c/d", "/a/c/d"], ["/a/b/", "../../../c", "/c"], ])("('%s', '%s')", async (pathname, location, expected) => { server.reload({ async fetch(request: Request) { const url = new URL(request.url); if (url.pathname == pathname) { return new Response("redirecting", { headers: { "Location": location, }, status: 302, }); } else if (url.pathname == expected) { return new Response("Fine."); } return new Response("Not Found", { status: 404, }); }, }); const resp = await fetch(`http://${server.hostname}:${server.port}${pathname}`); expect(resp.redirected).toBe(true); expect(new URL(resp.url).pathname).toStrictEqual(expected); expect(resp.status).toBe(200); expect(await resp.text()).toBe("Fine."); }); }); describe("maxRedirects", () => { let server: Server; beforeAll(() => { server = Bun.serve({ port: 0, async fetch(request: Request) { const url = new URL(request.url); if (url.pathname.startsWith("/hop/")) { const hop = Number(url.pathname.slice("/hop/".length)); if (hop >= 4) { return new Response("done"); } return new Response(null, { status: 302, headers: { Location: `/hop/${hop + 1}` } }); } return new Response("Not Found", { status: 404 }); }, }); }); afterAll(() => { server.stop(true); }); it("rejects once the chain exceeds maxRedirects", async () => { expect(fetch(`${server.url}hop/0`, { maxRedirects: 2 })).rejects.toThrow("redirected too many times"); }); it("follows the chain when maxRedirects is large enough", async () => { const resp = await fetch(`${server.url}hop/0`, { maxRedirects: 4 }); expect(resp.status).toBe(200); expect(await resp.text()).toBe("done"); expect(new URL(resp.url).pathname).toBe("/hop/4"); }); it("rejects invalid values", async () => { expect(async () => await fetch(`${server.url}hop/0`, { maxRedirects: -1 })).toThrow(); expect(async () => await fetch(`${server.url}hop/0`, { maxRedirects: 1.5 })).toThrow(); expect(async () => await fetch(`${server.url}hop/0`, { maxRedirects: NaN })).toThrow(); }); }); it.concurrent("should allow very long redirect URLS", async () => { const Location = "/" + "B".repeat(7 * 1024); using server = Bun.serve({ port: 0, async fetch(request: Request) { gc(); const url = new URL(request.url); if (url.pathname == "/redirect") { return new Response("redirecting", { headers: { Location, }, status: 302, }); } return new Response("Not Found", { status: 404, }); }, }); // run it more times to check Malformed_HTTP_Response errors for (let i = 0; i < 100; i++) { const { url, status } = await fetch(`${server.url.origin}/redirect`); expect(url).toBe(`${server.url.origin}${Location}`); expect(status).toBe(404); } }); it.concurrent("304 not modified with missing content-length does not cause a request timeout", async () => { const server = await Bun.listen({ socket: { open(socket) { socket.write("HTTP/1.1 304 Not Modified\r\n\r\n"); socket.flush(); setTimeout(() => { socket.end(); }, 9999).unref(); }, data() {}, close() {}, }, port: 0, hostname: "localhost", }); const response = await fetch(`http://${server.hostname}:${server.port}/`); expect(response.status).toBe(304); expect(await response.arrayBuffer()).toHaveLength(0); server.stop(true); }); it("304 not modified with missing content-length and connection close does not cause a request timeout", async () => { const server = await Bun.listen({ socket: { open(socket) { socket.write("HTTP/1.1 304 Not Modified\r\nConnection: close\r\n\r\n"); socket.flush(); setTimeout(() => { socket.end(); }, 9999).unref(); }, data() {}, close() {}, }, port: 0, hostname: "localhost", }); const response = await fetch(`http://${server.hostname}:${server.port}/`); expect(response.status).toBe(304); expect(await response.arrayBuffer()).toHaveLength(0); server.stop(true); }); it("304 not modified with content-length 0 and connection close does not cause a request timeout", async () => { const server = await Bun.listen({ socket: { open(socket) { socket.write("HTTP/1.1 304 Not Modified\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"); socket.flush(); setTimeout(() => { socket.end(); }, 9999).unref(); }, data() {}, close() {}, }, port: 0, hostname: "localhost", }); const response = await fetch(`http://${server.hostname}:${server.port}/`); expect(response.status).toBe(304); expect(await response.arrayBuffer()).toHaveLength(0); server.stop(true); }); it("304 not modified with 0 content-length does not cause a request timeout", async () => { const server = await Bun.listen({ socket: { open(socket) { socket.write("HTTP/1.1 304 Not Modified\r\nContent-Length: 0\r\n\r\n"); socket.flush(); setTimeout(() => { socket.end(); }, 9999).unref(); }, data() {}, close() {}, }, port: 0, hostname: "localhost", }); const response = await fetch(`http://${server.hostname}:${server.port}/`); expect(response.status).toBe(304); expect(await response.arrayBuffer()).toHaveLength(0); server.stop(true); }); describe("http/1.1 response body length", () => { // issue #6932 (support response without Content-Length and Transfer-Encoding) + some regression tests let server: TCPSocketListener | undefined; beforeAll(async () => { server = Bun.listen({ socket: { open(socket) { setTimeout(() => { socket.end(); }, 9999).unref(); }, data(socket, data) { const text = data.toString(); if (text.startsWith("GET /text")) { socket.end("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHello, World!"); } else if (text.startsWith("GET /json")) { socket.end('HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{"hello":"World"}'); } else if (text.startsWith("GET /chunked")) { socket.end( "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nTransfer-Encoding: chunked\r\n\r\nd\r\nHello, World!\r\n0\r\n\r\n", ); } else if (text.startsWith("GET /empty")) { socket.end("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); } else if (text.startsWith("GET /keepalive/bad")) { const resp = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: keep-alive\r\n\r\nHello, World!"; socket.end(`${resp}${resp}`); } else if (text.startsWith("GET /keepalive")) { const resp = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: keep-alive\r\nContent-Length: 13\r\n\r\nHello, World!"; socket.end(`${resp}${resp}`); } else { socket.end(`HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\nHello, World!`); } }, close() {}, }, port: 0, hostname: "localhost", }); }); afterAll(() => { server?.stop?.(); }); const getHost = () => `${server!.hostname}:${server!.port}`; describe("without content-length", () => { it("should read text until socket closed", async () => { const response = await fetch(`http://${getHost()}/text`); expect(response.status).toBe(200); expect(response.text()).resolves.toBe("Hello, World!"); }); it("should read json until socket closed", async () => { const response = await fetch(`http://${getHost()}/json`); expect(response.status).toBe(200); expect(response.json()).resolves.toEqual({ "hello": "World" }); }); it("should disable keep-alive", async () => { // according to http/1.1 spec, the keep-alive persistence behavior should be disabled when // "Content-Length" header is not set (and response is not chunked) // therefore the response text for this test should contain // the 1st http response body + the full 2nd http response as text const response = await fetch(`http://${getHost()}/keepalive/bad`); expect(response.status).toBe(200); expect(response.text()).resolves.toHaveLength(95); }); }); it("should support keep-alive", async () => { const response = await fetch(`http://${getHost()}/keepalive`); expect(response.status).toBe(200); expect(response.text()).resolves.toBe("Hello, World!"); }); it("should support transfer-encoding: chunked", async () => { const response = await fetch(`http://${getHost()}/chunked`); expect(response.status).toBe(200); expect(response.text()).resolves.toBe("Hello, World!"); }); it("should support non-zero content-length", async () => { const response = await fetch(`http://${getHost()}/non-empty`); expect(response.status).toBe(200); expect(response.text()).resolves.toBe("Hello, World!"); }); it("should support content-length: 0", async () => { const response = await fetch(`http://${getHost()}/empty`); expect(response.status).toBe(200); expect(response.arrayBuffer()).resolves.toHaveLength(0); }); it.todoIf(isBroken)("should ignore body on HEAD", async () => { const response = await fetch(`http://${getHost()}/text`, { method: "HEAD" }); expect(response.status).toBe(200); expect(response.arrayBuffer()).resolves.toHaveLength(0); }); }); describe("fetch Response life cycle", () => { // error: Malformed_HTTP_Response fetching "http://localhost:58888/". For more information, pass `verbose: true` in the second argument to fetch() // path: "http://localhost:58888/", // errno: 0, // code: "Malformed_HTTP_Response" // 2054 | stderr: "inherit", // 2055 | stdout: "inherit", // 2056 | stdin: "inherit", // 2057 | env: bunEnv, // 2058 | }); // 2059 | expect(await clientProcess.exited).toBe(0); // ^ // error: expect(received).toBe(expected) // Expected: 0 // Received: 1 // at (/opt/homebrew/etc/buildkite-agent/builds/macOS-13-aarch64-1/bun/bun/test/js/web/fetch/fetch.test.ts:2059:40) // βœ— fetch Response life cycle > should not keep Response alive if not consumed [205.17ms] it.skipIf(isFlaky && isMacOS)("should not keep Response alive if not consumed", async () => { let deferred = Promise.withResolvers(); await using serverProcess = Bun.spawn({ cmd: [bunExe(), "--smol", fetchFixture3], stderr: "inherit", stdout: "inherit", stdin: "inherit", env: bunEnv, ipc(message) { deferred.resolve(message); }, }); const serverUrl = await deferred.promise; await using clientProcess = Bun.spawn({ cmd: [bunExe(), "--smol", fetchFixture4, serverUrl], stderr: "inherit", stdout: "inherit", stdin: "inherit", env: bunEnv, }); expect(await clientProcess.exited).toBe(0); }); it("should allow to get promise result after response is GC'd", async () => { using server = Bun.serve({ port: 0, async fetch(request: Request) { return new Response( new ReadableStream({ async pull(controller) { await Bun.sleep(100); controller.enqueue(new TextEncoder().encode("Hello, World!")); await Bun.sleep(100); controller.close(); }, }), { status: 200 }, ); }, }); async function fetchResponse() { const url = new URL("non-empty", server.url); const response = await fetch(url); return response.text(); } try { const response_promise = fetchResponse(); Bun.gc(true); expect(await response_promise).toBe("Hello, World!"); } finally { server.stop(true); } }); }); describe("fetch should allow duplex", () => { it("should allow duplex streaming", async () => { using server = Bun.serve({ port: 0, async fetch(req) { return new Response(req.body); }, }); const intervalStream = new ReadableStream({ start(c) { let count = 0; const timer = setInterval(() => { c.enqueue("Hello\n"); if (count === 5) { clearInterval(timer); c.close(); } count++; }, 20); }, }).pipeThrough(new TextEncoderStream()); const resp = await fetch(server.url, { method: "POST", body: intervalStream, duplex: "half", }); const reader = resp.body!.pipeThrough(new TextDecoderStream()).getReader(); var result = ""; while (true) { const { value, done } = await reader.read(); if (done) break; result += value; } expect(result).toBe("Hello\n".repeat(6)); }); it("should allow duplex extending Readable (sync)", async () => { class HelloWorldStream extends Readable { declare chunks: string[]; declare index: number; constructor(options?) { super(options); this.chunks = ["Hello", " ", "World!"]; this.index = 0; } _read(size) { if (this.index < this.chunks.length) { this.push(this.chunks[this.index]); this.index++; } else { this.push(null); } } } using server = Bun.serve({ port: 0, async fetch(req) { return new Response(req.body); }, }); const response = await fetch(server.url, { body: new HelloWorldStream(), method: "POST", duplex: "half", }); expect(await response.text()).toBe("Hello World!"); }); it("should allow duplex extending Readable (async)", async () => { class HelloWorldStream extends Readable { declare chunks: string[]; declare index: number; constructor(options?) { super(options); this.chunks = ["Hello", " ", "World!"]; this.index = 0; } _read(size) { setTimeout(() => { if (this.index < this.chunks.length) { this.push(this.chunks[this.index]); this.index++; } else { this.push(null); } }, 20); } } using server = Bun.serve({ port: 0, async fetch(req) { return new Response(req.body); }, }); const response = await fetch(server.url, { body: new HelloWorldStream(), method: "POST", duplex: "half", }); expect(await response.text()).toBe("Hello World!"); }); // A node Readable whose _read() pushes synchronously produces an async iterator whose // .next() fulfills synchronously; the request-body sink must report backpressure so the // pump suspends on flush(true) instead of spinning. it("does not wedge on Readable.destroy() when _read pushes synchronously", async () => { const fixture = ` const net = require("node:net"); const { Readable } = require("node:stream"); const srv = net.createServer(s => s.on("data", () => {})); await new Promise(r => srv.listen(0, "127.0.0.1", r)); const chunk = Buffer.alloc(16 * 1024, 0x47); const rd = new Readable({ read() { this.push(chunk); } }); setTimeout(() => rd.destroy(new Error("upstream went away")), 50); let ticks = 0; const iv = setInterval(() => { ticks++ }, 25); try { await fetch("http://127.0.0.1:" + srv.address().port + "/", { method: "POST", body: rd, duplex: "half" }); console.log("BUG: fetch resolved"); } catch (e) { console.log("rejected:" + String(e?.message || e) + " ticks:" + ticks); } clearInterval(iv); srv.close(); process.exit(0); `; await using proc = Bun.spawn({ cmd: [bunExe(), "-e", fixture], env: bunEnv, stdout: "pipe", stderr: "inherit", }); const stdoutP = proc.stdout.text(); // The failure mode is a 100%-CPU spin that never yields, so proc.exited alone cannot // resolve on an unfixed build; the race is the condition, not a timing crutch. const exited = await Promise.race([proc.exited, sleep(isDebug ? 4000 : 2000).then(() => "timeout" as const)]); if (exited === "timeout") proc.kill(9); const stdout = await stdoutP; expect({ exited, stdout: stdout.trim() }).toEqual({ exited: 0, stdout: expect.stringMatching(/^rejected:upstream went away ticks:[1-9]/), }); }); it("should allow duplex using async iterator (async)", async () => { using server = Bun.serve({ port: 0, async fetch(req) { return new Response(req.body); }, }); const response = await fetch(server.url, { body: async function* iter() { yield "Hello"; await Bun.sleep(20); yield " "; await Bun.sleep(20); yield "World!"; }, method: "POST", duplex: "half", }); expect(await response.text()).toBe("Hello World!"); }); it("should fail in redirects .follow when using duplex", async () => { using server = Bun.serve({ port: 0, async fetch(req) { if (req.url.indexOf("/redirect") === -1) { return Response.redirect("/"); } return new Response(req.body); }, }); expect(async () => { const response = await fetch(server.url, { body: async function* iter() { yield "Hello"; await Bun.sleep(20); yield " "; await Bun.sleep(20); yield "World!"; }, method: "POST", duplex: "half", }); await response.text(); }).toThrow(); }); it("should work in redirects .manual when using duplex", async () => { using server = Bun.serve({ port: 0, idleTimeout: 0, async fetch(req) { if (req.url.indexOf("/redirect") === -1) { return Response.redirect("/"); } return new Response(req.body); }, }); expect(async () => { const response = await fetch(server.url, { body: async function* iter() { yield "Hello"; await Bun.sleep(20); yield " "; await Bun.sleep(20); yield "World!"; }, method: "POST", duplex: "half", redirect: "manual", }); await response.text(); }).not.toThrow(); }); // When the download source is faster than the upload target, the response- // body ByteStream must pause the source socket instead of buffering the // rate difference in-process. Before the fix, a chunk arriving before the // upload sink attached flipped the source to BufferAll and RSS grew at the // line rate (several GB in seconds on localhost). it("bounds memory when the upload target is slower than the download source", async () => { const fixture = ` const net = require("node:net"); const chunk = Buffer.alloc(64 * 1024, 0x47); const source = net.createServer(sock => { sock.write("HTTP/1.1 200 OK\\r\\ntransfer-encoding: chunked\\r\\nconnection: close\\r\\n\\r\\n"); const framed = Buffer.concat([Buffer.from("10000\\r\\n"), chunk, Buffer.from("\\r\\n")]); const pump = () => { while (sock.write(framed)); sock.once("drain", pump); }; pump(); }); // Sink reads ~1 MB/s so the source (line rate on loopback) outpaces it. const sink = net.createServer(sock => { let seen = 0; const start = Date.now(); const RATE = 1024 * 1024; sock.on("data", d => { seen += d.length; const ahead = (seen / RATE) * 1000 - (Date.now() - start); if (ahead > 0) { sock.pause(); setTimeout(() => sock.resume(), ahead); } }); }); await Promise.all([source, sink].map(s => new Promise(r => s.listen(0, "127.0.0.1", r)))); Bun.gc(true); const rssBefore = process.memoryUsage.rss(); const up = await fetch(\`http://127.0.0.1:\${source.address().port}/\`); fetch(\`http://127.0.0.1:\${sink.address().port}/\`, { method: "POST", body: up.body, duplex: "half" }).catch(() => {}); let peak = rssBefore; for (let i = 0; i < 20; i++) { await Bun.sleep(100); peak = Math.max(peak, process.memoryUsage.rss()); } console.log(JSON.stringify({ deltaMB: (peak - rssBefore) / 1024 / 1024 })); process.exit(0); `; await using proc = Bun.spawn({ cmd: [bunExe(), "-e", fixture], env: bunEnv, stdout: "pipe", stderr: "pipe", }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); expect(stderr).toBe(""); const { deltaMB } = JSON.parse(stdout.trim()); // Without the fix RSS grows by hundreds of MB per second; with it, the // in-flight bytes are bounded by kernel socket buffers plus one chunk. expect(deltaMB).toBeLessThan(isASAN || isDebug ? 160 : 64); expect(exitCode).toBe(0); }); // A type:"direct" stream body where pull does `await controller.write(chunk)` // in a loop must suspend when the sink is backpressured: write() returns a // pending Promise once the stream buffer is over the high-water mark. it("suspends a type:'direct' body's controller.write() when the upload target is backpressured", async () => { const fixture = ` const net = require("node:net"); const sink = net.createServer(sock => sock.pause()); await new Promise(r => sink.listen(0, "127.0.0.1", r)); Bun.gc(true); const rssBefore = process.memoryUsage.rss(); let writes = 0, suspended = false; const body = new ReadableStream({ type: "direct", async pull(controller) { const chunk = new Uint8Array(64 * 1024).fill(0x47); while (writes < 4096) { const wrote = controller.write(chunk); writes++; if (wrote instanceof Promise) { suspended = true; await wrote; } else await 1; } }, }); fetch(\`http://127.0.0.1:\${sink.address().port}/\`, { method: "POST", body, duplex: "half" }).catch(() => {}); let peak = rssBefore; for (let i = 0; i < 15; i++) { await Bun.sleep(100); peak = Math.max(peak, process.memoryUsage.rss()); } console.log(JSON.stringify({ deltaMB: (peak - rssBefore) / 1024 / 1024, writes, suspended })); process.exit(0); `; await using proc = Bun.spawn({ cmd: [bunExe(), "-e", fixture], env: bunEnv, stdout: "pipe", stderr: "pipe", }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); expect(stderr).toBe(""); const { deltaMB, writes, suspended } = JSON.parse(stdout.trim()); expect(suspended).toBe(true); // Without the fix the loop is unbounded (tens of GB before harness kill); // with it, writes stop once the kernel send buffer fills. expect(deltaMB).toBeLessThan(isASAN || isDebug ? 96 : 32); expect(writes).toBeLessThan(256); expect(exitCode).toBe(0); }); // Passing a response body as a request body attaches it to a native sink; // the source stream must be marked locked + disturbed so a second consumer // errors instead of hanging on data that will never be delivered to it. it("locks the response body when it is used as a request body", async () => { await using source = Bun.serve({ port: 0, fetch: () => new Response(new ReadableStream({ pull: c => c.enqueue(new Uint8Array(64 * 1024)) })), }); const streaming = Promise.withResolvers(); const gate = Promise.withResolvers(); await using sink = Bun.serve({ port: 0, fetch: async req => { // Body bytes arriving here proves the client has attached the // source stream to its request-body sink (headers alone do not). await req.body!.getReader().read(); streaming.resolve(); await gate.promise; return new Response("ok"); }, }); const up = await fetch(source.url); const controller = new AbortController(); const upload = fetch(sink.url, { method: "POST", body: up.body, duplex: "half", signal: controller.signal, } as RequestInit).catch(() => {}); await streaming.promise; expect(up.body!.locked).toBe(true); expect(up.bodyUsed).toBe(true); expect(() => up.body!.getReader()).toThrow(expect.objectContaining({ code: "ERR_INVALID_STATE" })); gate.resolve(); controller.abort(); await upload; }); // A Bun.serve handler forwarding req.body to a slower upload target must // back-pressure the uploading client rather than buffering the difference: // the request-body ByteStream stops reading from the inbound socket while // the outbound sink is over its high-water mark. it("bounds memory when a handler forwards req.body to a stalled target", async () => { const fixture = ` const net = require("node:net"); const CHUNK = Buffer.alloc(64 * 1024, 0x47), COUNT = 2048; // 128 MB // Upload target stalls before reading, then drains and reports the total. const { promise: drained, resolve: onDrained } = Promise.withResolvers(); const sink = net.createServer(sock => { let got = 0; sock.pause(); setTimeout(() => sock.resume(), 500); sock.on("data", d => { got += d.length; if (got >= CHUNK.length * COUNT) onDrained(got); }); }); await new Promise(r => sink.listen(0, "127.0.0.1", r)); const proxy = Bun.serve({ port: 0, idleTimeout: 0, maxRequestBodySize: 512 * 1024 * 1024, async fetch(req) { await fetch(\`http://127.0.0.1:\${sink.address().port}/\`, { method: "POST", body: req.body, duplex: "half" }).catch(() => {}); return new Response("ok"); }, }); // Client uploads exactly COUNT chunks as fast as its socket accepts. const client = net.connect(proxy.port, "127.0.0.1", () => { client.write("POST / HTTP/1.1\\r\\nHost: x\\r\\nTransfer-Encoding: chunked\\r\\n\\r\\n"); const framed = Buffer.concat([Buffer.from("10000\\r\\n"), CHUNK, Buffer.from("\\r\\n")]); let n = 0; const pump = () => { while (n < COUNT) { n++; if (!client.write(framed)) return client.once("drain", pump); } client.write("0\\r\\n\\r\\n"); }; pump(); }); client.on("error", () => {}); console.log(JSON.stringify({ drained: (await drained) >= CHUNK.length * COUNT })); process.exit(0); `; const [fixtureMaxRSS, baselineMaxRSS] = await Promise.all([ runFixtureMaxRSS(fixture, { drained: true }), emptyProcessMaxRSS(), ]); // Without inbound back-pressure the proxy absorbs the whole payload while // the target stalls; with it the uploader's socket fills instead. expect((fixtureMaxRSS - baselineMaxRSS) / 1024 / 1024).toBeLessThan(isASAN || isDebug ? 256 : 96); }); }); it("should allow to follow redirect if connection is closed, abort should work even if the socket was closed before the redirect", async () => { for (const type of ["normal", "delay"]) { await using server = net.createServer(socket => { // Raw test server: tolerate client aborts, surface anything unexpected. socket.on("error", (err: NodeJS.ErrnoException) => { if (err.code !== "ECONNRESET" && err.code !== "EPIPE" && err.code !== "ECONNABORTED") throw err; }); let body = ""; socket.on("data", data => { body += data.toString("utf8"); const headerEndIndex = body.indexOf("\r\n\r\n"); if (headerEndIndex !== -1) { // headers received const headers = body.split("\r\n\r\n")[0]; const path = headers.split("\r\n")[0].split(" ")[1]; if (path === "/redirect") { socket.end( "HTTP/1.1 308 Permanent Redirect\r\nCache-Control: public, max-age=0, must-revalidate\r\nContent-Type: text/plain\r\nLocation: /\r\nConnection: close\r\n\r\n", ); } else { if (type === "delay") { setTimeout(() => { if (!socket.destroyed) socket.end( "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 9\r\nConnection: close\r\n\r\nHello Bun", ); }, 200); } else { socket.end( "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 9\r\nConnection: close\r\n\r\nHello Bun", ); } } } }); }); await once(server.listen(0), "listening"); try { let { address, port } = server.address() as AddressInfo; if (address === "::") { address = "[::]"; } const response = await fetch(`http://${address}:${port}/redirect`, { signal: AbortSignal.timeout(150), }); if (type === "delay") { console.error(response, type); expect.unreachable(); } else { expect(response.status).toBe(200); expect(await response.text()).toBe("Hello Bun"); } } catch (err) { if (type === "delay") { expect((err as Error).name).toBe("TimeoutError"); } else { expect.unreachable(); } } } }); it("rejects a response with an unparseable Content-Length instead of treating it as empty", async () => { // RFC 9112 section 6.3: an invalid Content-Length (or duplicate Content-Length // headers with differing values) is an unrecoverable framing error. Falling // back to "0" would deliver an empty body and return a desynchronized socket // to the keep-alive pool with the unread response bytes still in flight, // where they would be read as the response to the next request. await using server = net.createServer(socket => { socket.once("data", data => { const path = data.toString("utf8").split(" ")[1]; if (path === "/invalid") { socket.end( "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: x\r\nConnection: keep-alive\r\n\r\n" + "HTTP/1.1 200 OK\r\nContent-Length: 25\r\n\r\ninjected follow-up bytes!", ); } else if (path === "/conflicting") { socket.end( "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5\r\nContent-Length: 6\r\n\r\nhello!", ); } else { socket.end( "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5\r\nConnection: close\r\n\r\nhello", ); } }); }); await once(server.listen(0, "localhost"), "listening"); const { port } = server.address() as AddressInfo; for (const path of ["invalid", "conflicting"]) { const result = await fetch(`http://localhost:${port}/${path}`) .then(res => res.text()) .catch(e => e); expect(result).toBeInstanceOf(Error); expect((result as any).code).toBe("InvalidContentLength"); } // A well-formed Content-Length is still delivered normally. const ok = await fetch(`http://localhost:${port}/valid`); expect(await ok.text()).toBe("hello"); }); it("never sends the URL fragment in the request-target", async () => { // The request-target is `new URL(s).pathname + search`. A fragment is never // sent, even one that contains a `?`. const targets: string[] = []; await using server = net.createServer(socket => { socket.once("data", data => { targets.push(data.toString("utf8").split("\r\n")[0]); socket.end("HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok"); }); }); await once(server.listen(0, "localhost"), "listening"); const { port } = server.address() as AddressInfo; const tails = [ "/p#frag?x=1", "/p#/route?id=7", "/#?", "/cb#access_token=abc&scope=x?y", "/p?q=1#frag?x=2", "/p#plain", "/a//b?q=1#/c?d", ]; const expected: string[] = []; for (const tail of tails) { const href = `http://localhost:${port}${tail}`; const url = new URL(href); expected.push(`GET ${url.pathname}${url.search} HTTP/1.1`); await (await fetch(href)).text(); } expect(targets).toEqual(expected); }); it("combines duplicate response headers per the Fetch spec", async () => { // WHATWG Fetch requires repeated header fields to be combined with ", " when // read via Headers.get(), except Set-Cookie which is stored as separate // values exposed by getSetCookie(). Previously Bun overwrote duplicate // non-common header names with the last value, dropping earlier values. await using server = net.createServer(socket => { socket.once("data", () => { socket.end( "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "X-Dup: first\r\n" + "X-Dup: second\r\n" + "X-Dup: third\r\n" + "X-Once: only\r\n" + "X-Gap: a\r\n" + "X-Gap:\r\n" + "X-Gap: c\r\n" + "X-Empty:\r\n" + "Accept: text/html\r\n" + "Accept: application/json\r\n" + "Set-Cookie: a=1\r\n" + "Set-Cookie: b=2\r\n" + "Connection: close\r\n" + "\r\n" + "ok", ); }); }); await once(server.listen(0, "127.0.0.1"), "listening"); const { port } = server.address() as AddressInfo; const res = await fetch(`http://127.0.0.1:${port}/`); expect(await res.text()).toBe("ok"); expect(res.headers.get("x-dup")).toBe("first, second, third"); expect(res.headers.get("x-once")).toBe("only"); // the combine step has no empty-value exception, and a lone empty header is // still visible β€” undici returns "a, , c" and "" here, not "a, c" and null expect(res.headers.get("x-gap")).toBe("a, , c"); expect(res.headers.get("x-empty")).toBe(""); expect(res.headers.get("accept")).toBe("text/html, application/json"); expect(res.headers.getSetCookie()).toEqual(["a=1", "b=2"]); }); it("drops a custom Host header when following a cross-origin redirect", async () => { // A per-request Host override must not survive a change of origin: the // follow-up request's Host header has to be re-computed from the redirect // target's URL, not carried over from the previous origin. await using target = Bun.serve({ port: 0, async fetch(request) { return new Response(request.headers.get("host") ?? ""); }, }); await using origin = Bun.serve({ port: 0, async fetch(request) { if (new URL(request.url).pathname === "/redirect") { return new Response(null, { status: 302, headers: { "Location": `http://${target.hostname}:${target.port}/landed` }, }); } return new Response(request.headers.get("host") ?? ""); }, }); // Cross-origin redirect: the redirect target must see its own authority, // not the caller-supplied Host override naming the previous origin. const redirected = await fetch(`http://${origin.hostname}:${origin.port}/redirect`, { headers: { "Host": "tenant.shared-cdn.example" }, }); expect(redirected.redirected).toBe(true); expect(await redirected.text()).toBe(`${target.hostname}:${target.port}`); // Without a redirect the explicit Host header is still honored. const direct = await fetch(`http://${origin.hostname}:${origin.port}/direct`, { headers: { "Host": "tenant.shared-cdn.example" }, }); expect(await direct.text()).toBe("tenant.shared-cdn.example"); }); it("fetch() with a fixed-size body drops a caller-supplied Transfer-Encoding header and sends only Content-Length", async () => { // RFC 9112 section 6.2/6.3: a sender must never emit both Transfer-Encoding and // Content-Length on the same message. For a fixed-size (non-streaming) body, // fetch() writes the body as raw bytes framed by a computed Content-Length, so a // caller-supplied "Transfer-Encoding: chunked" header (e.g. headers copied wholesale // from an inbound request in a gateway) must be dropped rather than forwarded // alongside Content-Length, where a TE-preferring upstream would mis-frame the body. const requests: string[] = []; await using server = net.createServer(socket => { let raw = ""; socket.on("data", data => { raw += data.toString("latin1"); const headerEnd = raw.indexOf("\r\n\r\n"); if (headerEnd === -1) return; const head = raw.slice(0, headerEnd); const contentLength = Number(/^content-length:\s*(\d+)\s*$/im.exec(head)?.[1] ?? 0); if (raw.length < headerEnd + 4 + contentLength) return; requests.push(raw); raw = ""; socket.end("HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nOK"); }); }); await once(server.listen(0, "localhost"), "listening"); const { port } = server.address() as AddressInfo; // A body whose raw bytes look like a terminal chunk followed by a second request. const body = "0\r\n\r\nGET /other HTTP/1.1\r\nHost: upstream\r\nX-Pad: junk"; const bodyLength = Buffer.byteLength(body); // Caller-supplied Transfer-Encoding header alongside a buffered (string) body. const withTE = await fetch(`http://localhost:${port}/`, { method: "POST", headers: { "Transfer-Encoding": "chunked", "Content-Type": "text/plain" }, body, }); expect(await withTE.text()).toBe("OK"); // The same request without the header still works the same way. const plain = await fetch(`http://localhost:${port}/`, { method: "POST", headers: { "Content-Type": "text/plain" }, body, }); expect(await plain.text()).toBe("OK"); expect(requests).toHaveLength(2); for (const rawRequest of requests) { const [head, ...bodyParts] = rawRequest.split("\r\n\r\n"); const headerLines = head .split("\r\n") .slice(1) .map(line => line.toLowerCase()); // Exactly one framing header reaches the wire: the computed Content-Length. expect(headerLines.filter(line => line.startsWith("transfer-encoding:"))).toEqual([]); expect(headerLines.filter(line => line.startsWith("content-length:"))).toEqual([`content-length: ${bodyLength}`]); // The body is the raw bytes described by Content-Length, with no chunk framing added. expect(bodyParts.join("\r\n\r\n")).toBe(body); } }); it("fetch() does not forward a caller-supplied Content-Length on a request without a body", async () => { // The Content-Length emitted on the wire must always describe the body fetch() is // actually about to send. A Content-Length copied from an inbound request (e.g. by a // gateway forwarding headers wholesale) on a request with no body would make the // upstream wait for body bytes that never arrive and read the start of the next // request on a kept-alive connection as that body. const requests: string[] = []; await using server = net.createServer(socket => { let raw = ""; socket.on("data", data => { raw += data.toString("latin1"); const headerEnd = raw.indexOf("\r\n\r\n"); if (headerEnd === -1) return; const head = raw.slice(0, headerEnd); const method = head.split(" ")[0]; if (method !== "GET") { // wait for the declared body before replying const contentLength = Number(/^content-length:\s*(\d+)\s*$/im.exec(head)?.[1] ?? 0); if (raw.length < headerEnd + 4 + contentLength) return; } requests.push(raw); raw = ""; socket.end("HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nOK"); }); }); await once(server.listen(0, "localhost"), "listening"); const { port } = server.address() as AddressInfo; // GET request with no body: a caller-supplied Content-Length must not reach the wire. const bodyless = await fetch(`http://localhost:${port}/`, { headers: { "Content-Length": "52", "X-Custom": "still-forwarded" }, }); expect(await bodyless.text()).toBe("OK"); // POST request with a real body: the emitted Content-Length is computed from the // body, not taken from the caller-supplied header. const withBody = await fetch(`http://localhost:${port}/`, { method: "POST", headers: { "Content-Length": "999" }, body: "hi", }); expect(await withBody.text()).toBe("OK"); expect(requests).toHaveLength(2); const headerLinesOf = (request: string) => request .split("\r\n\r\n")[0] .split("\r\n") .slice(1) .map(line => line.toLowerCase()); const bodylessHeaders = headerLinesOf(requests[0]); // No Content-Length at all on the bodyless request: the bogus value is dropped. expect(bodylessHeaders.filter(line => line.startsWith("content-length:"))).toEqual([]); // Other caller-supplied headers are still forwarded. expect(bodylessHeaders).toContain("x-custom: still-forwarded"); const withBodyHeaders = headerLinesOf(requests[1]); expect(withBodyHeaders.filter(line => line.startsWith("content-length:"))).toEqual(["content-length: 2"]); }); describe("fetch() with a streaming request body and caller framing headers", () => { // fetch() cannot measure a ReadableStream, an async generator or a node // stream.Readable body. It sends such a body in one of two ways: raw bytes // behind a Content-Length the caller declared, or chunk-encoded bytes behind a // Transfer-Encoding that ends in "chunked". A caller header that asks for // anything else describes framing fetch() does not produce, so the fetch // rejects before a byte is written. A declared Content-Length must match the // body: a surplus byte would land on the connection after the declared end, // where a keep-alive peer reads it as the start of the next request, and a // missing byte leaves the peer waiting for a body that never completes. type RawRequest = { framing: string[]; body: string }; // A raw origin that records, per connection, the framing header lines exactly // as written and the body bytes exactly as received. async function rawOrigin() { const queue: RawRequest[] = []; const waiting: ((request: RawRequest) => void)[] = []; let connections = 0; const record = (raw: string) => { const headerEnd = raw.indexOf("\r\n\r\n"); const request: RawRequest = { framing: raw .slice(0, headerEnd === -1 ? raw.length : headerEnd) .split("\r\n") .filter(line => /^(content-length|transfer-encoding):/i.test(line)), body: headerEnd === -1 ? "" : raw.slice(headerEnd + 4), }; const resolve = waiting.shift(); if (resolve) resolve(request); else queue.push(request); }; const server = net.createServer(socket => { connections++; let raw = ""; let replied = false; socket.on("error", () => {}); // The client resets a connection whose body does not match its // Content-Length, so the close is the only signal that the request is // over. Record whatever arrived. socket.on("close", () => { if (!replied) record(raw); }); socket.on("data", data => { raw += data.toString("latin1"); const headerEnd = raw.indexOf("\r\n\r\n"); if (headerEnd === -1 || replied) return; const head = raw.slice(0, headerEnd); const received = raw.slice(headerEnd + 4); if (/^transfer-encoding:.*chunked\s*$/im.test(head)) { if (!received.endsWith("0\r\n\r\n")) return; } else { const declared = Number(/^content-length:\s*(\d+)\s*$/im.exec(head)?.[1] ?? 0); if (received.length < declared) return; } replied = true; record(raw); socket.end("HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nOK"); }); }); await once(server.listen(0, "localhost"), "listening"); return { url: `http://localhost:${(server.address() as AddressInfo).port}/`, nextRequest: () => queue.length > 0 ? Promise.resolve(queue.shift()!) : new Promise(resolve => waiting.push(resolve)), get connections() { return connections; }, [Symbol.asyncDispose]: () => server[Symbol.asyncDispose](), }; } const body = "nr1-nr2"; // 7 bytes const chunked = `${body.length.toString(16)}\r\n${body}\r\n0\r\n\r\n`; const bodyKinds: [string, () => unknown][] = [ [ "ReadableStream", () => new ReadableStream({ start(controller) { controller.enqueue(new TextEncoder().encode(body)); controller.close(); }, }), ], [ "async generator", async function* () { yield body; }, ], ["stream.Readable", () => Readable.from([body])], ]; const post = (url: string, headers: Bun.HeadersInit, makeBody: () => unknown) => fetch(url, { method: "POST", headers, body: makeBody(), duplex: "half" } as RequestInit); // The response text, or what the fetch rejected with. const outcome = (response: Promise) => response.then( r => r.text(), e => ({ name: e?.name, code: e?.code }), ); const invalidHeader = { name: "TypeError", code: "ERR_HTTP_INVALID_HEADER_VALUE" }; const mismatch = { name: "Error", code: "ERR_HTTP_CONTENT_LENGTH_MISMATCH" }; describe.each(bodyKinds)("%s body", (_, makeBody) => { it("sends the raw bytes behind a Content-Length that matches them", async () => { await using origin = await rawOrigin(); expect(await outcome(post(origin.url, { "Content-Length": String(body.length) }, makeBody))).toBe("OK"); expect(await origin.nextRequest()).toEqual({ framing: ["Content-Length: 7"], body }); }); it("fails the request when the body does not match its Content-Length", async () => { await using origin = await rawOrigin(); // Longer than declared: no byte of the surplus chunk is written, so // nothing can be read as the start of the next request. expect(await outcome(post(origin.url, { "Content-Length": "2" }, makeBody))).toEqual(mismatch); expect((await origin.nextRequest()).body).toBe(""); // Shorter than declared: the request fails instead of leaving the peer // waiting for 43 bytes that never come. The peer sees the connection reset // mid-message. How much it read before the reset is up to its TCP stack. expect(await outcome(post(origin.url, { "Content-Length": "50" }, makeBody))).toEqual(mismatch); expect((await origin.nextRequest()).body.length).toBeLessThanOrEqual(body.length); // The next request gets a connection of its own. expect(await outcome(post(origin.url, {}, makeBody))).toBe("OK"); expect(await origin.nextRequest()).toEqual({ framing: ["Transfer-Encoding: chunked"], body: chunked }); }); it("forwards a Transfer-Encoding whose final coding is chunked, as written", async () => { await using origin = await rawOrigin(); for (const value of ["chunked", "Chunked", "gzip, chunked"]) { expect(await outcome(post(origin.url, { "Transfer-Encoding": value }, makeBody))).toBe("OK"); expect(await origin.nextRequest()).toEqual({ framing: [`Transfer-Encoding: ${value}`], body: chunked }); } // Two caller rows reach the client joined, like any list header. const joined = new Headers(); joined.append("Transfer-Encoding", "gzip"); joined.append("Transfer-Encoding", "chunked"); expect(await outcome(post(origin.url, joined, makeBody))).toBe("OK"); expect(await origin.nextRequest()).toEqual({ framing: ["Transfer-Encoding: gzip, chunked"], body: chunked }); // A Content-Length next to it is neither sent nor counted. for (const contentLength of [String(body.length), "2"]) { const headers = { "Transfer-Encoding": "chunked", "Content-Length": contentLength }; expect(await outcome(post(origin.url, headers, makeBody))).toBe("OK"); expect(await origin.nextRequest()).toEqual({ framing: ["Transfer-Encoding: chunked"], body: chunked }); } }); it("rejects a Content-Length that is not a count of bytes, before anything is sent", async () => { await using origin = await rawOrigin(); const twoRows = new Headers(); twoRows.append("Content-Length", "5"); twoRows.append("Content-Length", "7"); const rows: Bun.HeadersInit[] = [ ...["abc", "+5", "0x5", "5.0", "-1", "", "99999999999999999999"].map(value => ({ "Content-Length": value })), // FetchHeaders joins two caller rows into "5, 7". twoRows, { "content-length": "7", "Content-Length": "7" }, // Not a count even when a usable Transfer-Encoding makes it moot. { "Content-Length": "abc", "Transfer-Encoding": "chunked" }, ]; for (const headers of rows) { expect(await outcome(post(origin.url, headers, makeBody))).toEqual(invalidHeader); // The next fetch to the origin works, and is the only request it sees. expect(await outcome(post(origin.url, {}, makeBody))).toBe("OK"); expect(await origin.nextRequest()).toEqual({ framing: ["Transfer-Encoding: chunked"], body: chunked }); } expect(origin.connections).toBe(rows.length); }); it("rejects a Transfer-Encoding that is not a list of known codings ending in chunked, before anything is sent", async () => { await using origin = await rawOrigin(); const rows: Bun.HeadersInit[] = [ ...["identity", "gzip", "chunked, gzip", "chunked, chunked", "gzip; q=1, chunked", "br2, chunked", ""].map( value => ({ "Transfer-Encoding": value }), ), // A usable Content-Length does not rescue it. { "Transfer-Encoding": "gzip", "Content-Length": String(body.length) }, ]; for (const headers of rows) { expect(await outcome(post(origin.url, headers, makeBody))).toEqual(invalidHeader); expect(await outcome(post(origin.url, {}, makeBody))).toBe("OK"); expect(await origin.nextRequest()).toEqual({ framing: ["Transfer-Encoding: chunked"], body: chunked }); } expect(origin.connections).toBe(rows.length); }); }); it("treats fetch(new Request(url, init)) and fetch(request, { headers }) like fetch(url, init)", async () => { await using origin = await rawOrigin(); const [, makeBody] = bodyKinds[0]; const init = (headers: Bun.HeadersInit) => ({ method: "POST", headers, body: makeBody(), duplex: "half" }) as RequestInit; expect(await outcome(fetch(new Request(origin.url, init({ "Content-Length": "abc" }))))).toEqual(invalidHeader); expect( await outcome(fetch(new Request(origin.url, init({})), { headers: { "Transfer-Encoding": "gzip" } })), ).toEqual(invalidHeader); expect(await outcome(fetch(new Request(origin.url, init({ "Content-Length": "2" }))))).toEqual(mismatch); expect((await origin.nextRequest()).body).toBe(""); expect(origin.connections).toBe(1); }); it("keeps the computed Content-Length for a body it can measure", async () => { await using origin = await rawOrigin(); // A blob-backed stream has a known size. The caller's framing headers are // dropped and the computed Content-Length wins, as for a string or a Blob. for (const headers of [{ "Content-Length": "2" }, { "Content-Length": "abc" }, { "Transfer-Encoding": "gzip" }]) { expect(await outcome(post(origin.url, headers as any, () => new Response(body).body))).toBe("OK"); expect(await origin.nextRequest()).toEqual({ framing: [`Content-Length: ${body.length}`], body }); } }); describe("across a redirect", () => { // A 303 (or a 301/302 on POST) drops the stream body and follows with a GET. // The caller's framing headers describe that dropped body, so the follow-up // must carry neither of them: a bodyless GET that announces Content-Length: 7 // makes the target read the next request on the connection as its body. type Seen = { connection: number; request: string; framing: string[]; body: string }; // A keep-alive origin: it parses every request on a connection by its own // framing, records it, and answers by path. "/early-303" answers before it // reads the body. "/303" and "/307" answer once the body is complete. async function redirectOrigin() { const queue: Seen[] = []; const waiting: ((seen: Seen) => void)[] = []; const seenPaths = new Map>(); const whenSeen = (request: string) => { if (!seenPaths.has(request)) seenPaths.set(request, Promise.withResolvers()); return seenPaths.get(request)!.promise; }; let connections = 0; let requests = 0; const record = (seen: Seen) => { requests++; whenSeen(seen.request); seenPaths.get(seen.request)!.resolve(); const resolve = waiting.shift(); if (resolve) resolve(seen); else queue.push(seen); }; const reply = (status: string, extra = "", text = "") => `HTTP/1.1 ${status}\r\n${extra}Content-Length: ${text.length}\r\n\r\n${text}`; const sockets = new Set(); const server = net.createServer(socket => { const connection = ++connections; let raw = ""; let abandoned = false; sockets.add(socket); socket.on("close", () => sockets.delete(socket)); socket.on("error", () => {}); socket.on("data", data => { if (abandoned) return; raw += data.toString("latin1"); for (;;) { const headerEnd = raw.indexOf("\r\n\r\n"); if (headerEnd === -1) return; const lines = raw.slice(0, headerEnd).split("\r\n"); const request = lines[0].replace(/ HTTP\/1\.1$/, ""); const framing = lines.slice(1).filter(line => /^(content-length|transfer-encoding):/i.test(line)); const rest = raw.slice(headerEnd + 4); if (request === "POST /early-303") { // The rest of this connection is the body the client gives up on. abandoned = true; record({ connection, request, framing, body: "" }); socket.write(reply("303 See Other", "Location: /next\r\n")); return; } let bodyLength = 0; if (framing.some(line => /^transfer-encoding:.*chunked\s*$/i.test(line))) { const end = rest.indexOf("0\r\n\r\n"); if (end === -1) return; bodyLength = end + 5; } else { bodyLength = Number(/^content-length:\s*(\d+)\s*$/i.exec(framing[0] ?? "")?.[1] ?? 0); if (rest.length < bodyLength) return; } record({ connection, request, framing, body: rest.slice(0, bodyLength) }); raw = rest.slice(bodyLength); if (request === "POST /303") socket.write(reply("303 See Other", "Location: /next\r\n")); else if (request === "POST /307") socket.write(reply("307 Temporary Redirect", "Location: /next\r\n")); else socket.write(reply("200 OK", "", "OK")); } }); }); await once(server.listen(0, "localhost"), "listening"); return { url: `http://localhost:${(server.address() as AddressInfo).port}/`, take: () => queue.length > 0 ? Promise.resolve(queue.shift()!) : new Promise(resolve => waiting.push(resolve)), whenSeen, get requests() { return requests; }, // The client keeps these connections alive in its pool; close() would wait for them. [Symbol.asyncDispose]: () => { for (const socket of sockets) socket.destroy(); return server[Symbol.asyncDispose](); }, }; } const [, makeBody] = bodyKinds[0]; const framings: [string, Bun.HeadersInit, Pick][] = [ ["Content-Length: 7", { "Content-Length": "7" }, { framing: ["Content-Length: 7"], body }], [ "Transfer-Encoding: gzip, chunked", { "Transfer-Encoding": "gzip, chunked" }, { framing: ["Transfer-Encoding: gzip, chunked"], body: chunked }, ], ["no framing header", {}, { framing: ["Transfer-Encoding: chunked"], body: chunked }], ]; it.each(framings)("a 303 follow-up carries no framing header (%s)", async (_, headers, hop1) => { await using origin = await redirectOrigin(); const response = await post(origin.url + "303", headers, makeBody); expect([response.status, response.redirected, await response.text()]).toEqual([200, true, "OK"]); expect(await origin.take()).toMatchObject({ request: "POST /303", ...hop1 }); const followUp = await origin.take(); expect(followUp).toEqual({ connection: followUp.connection, request: "GET /next", framing: [], body: "" }); // Nothing is left over on the follow-up's connection: the next fetch reuses // it and the origin parses that request as its own. expect(await outcome(fetch(origin.url + "after", { method: "POST", body: "hello" }))).toBe("OK"); expect(await origin.take()).toEqual({ connection: followUp.connection, request: "POST /after", framing: ["Content-Length: 5"], body: "hello", }); }); it("a 303 that arrives before the body resolves with the final response and drops hop 1's connection", async () => { await using origin = await redirectOrigin(); // The stream says nothing until the follow-up has reached the origin, then // ends 7 bytes short of its declared length. That body was already dropped, // so the count does not fail the fetch. const followedUp = origin.whenSeen("GET /next"); const late = new ReadableStream({ async pull(controller) { await followedUp; controller.close(); }, }); const response = await post(origin.url + "early-303", { "Content-Length": "7" }, () => late); expect([response.status, response.redirected, await response.text()]).toEqual([200, true, "OK"]); const hop1 = await origin.take(); expect(hop1).toMatchObject({ request: "POST /early-303", framing: ["Content-Length: 7"] }); const followUp = await origin.take(); expect(followUp).toMatchObject({ request: "GET /next", framing: [], body: "" }); // Hop 1 still owed 7 body bytes, so its connection was closed, not pooled. expect(followUp.connection).not.toBe(hop1.connection); }); it.each(framings)("a 307 rejects as not replayable (%s)", async (_, headers, hop1) => { await using origin = await redirectOrigin(); const rejection = await post(origin.url + "307", headers, makeBody).then( response => response.status, e => ({ name: e?.name, message: e?.message }), ); expect(rejection).toEqual({ name: "TypeError", message: "Request body is a ReadableStream and cannot be replayed for this redirect", }); expect(await origin.take()).toMatchObject({ request: "POST /307", ...hop1 }); expect(origin.requests).toBe(1); }); }); }); it("releases interim 1xx response bytes as they are parsed while waiting for the final response", async () => { // A misbehaving origin can stream an arbitrarily long sequence of interim (1xx) // responses before the final status line. Bytes belonging to interim responses that // have already been parsed must be released from the header accumulation buffer as // they are consumed, instead of being retained (and re-parsed) for the lifetime of // the request. The flood below totals ~48 MB of interim responses, so process RSS // must not grow by anywhere near that amount while the request is still waiting for // its final status line, and the final response must still be delivered normally. const informational = "HTTP/1.1 103 Early Hints\r\nx-filler: " + "a".repeat(1024) + "\r\n\r\n"; const responseLength = informational.length; const writeSize = 256 * 1024 - 13; // never a multiple of responseLength, so writes end mid-response const pattern = Buffer.from(informational.repeat(Math.ceil(writeSize / responseLength) + 2), "latin1"); const floodBytes = 48 * 1024 * 1024; let floodedBytes = 0; const { promise: floodDone, resolve: floodDoneResolve } = Promise.withResolvers(); const sockets: net.Socket[] = []; const server = net.createServer(socket => { sockets.push(socket); socket.once("data", () => { const writeMore = () => { while (floodedBytes < floodBytes) { const offset = floodedBytes % responseLength; const slice = pattern.subarray(offset, offset + writeSize); floodedBytes += slice.length; if (!socket.write(slice)) { socket.once("drain", writeMore); return; } } floodDoneResolve(); }; writeMore(); }); }); await once(server.listen(0, "localhost"), "listening"); const { port } = server.address() as AddressInfo; try { Bun.gc(true); const rssBefore = rss(); const responsePromise = fetch(`http://localhost:${port}/`); await floodDone; Bun.gc(true); const rssDuringFlood = rss(); // Complete the partially written interim response, then send the real response. const socket = sockets[0]; const tail = floodedBytes % responseLength; if (tail !== 0) socket.write(pattern.subarray(tail, responseLength)); socket.end("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 4\r\nConnection: close\r\n\r\ndone"); // The final response after the interim responses is still delivered normally. const response = await responsePromise; expect(await response.text()).toBe("done"); // Only a small parse tail may be retained while the interim responses stream in; // the ~48 MB of already-consumed 1xx bytes must not accumulate in the process. const deltaMB = (rssDuringFlood - rssBefore) / 1024 / 1024; // A local `bun bd` debug build is ASAN-instrumented but not named // `bun-asan`, so isASAN is false there; its quarantine retains the freed // flood bytes the same way - give it the same allowance. expect(deltaMB).toBeLessThan(isASAN || isDebug ? 48 : 16); } finally { for (const socket of sockets) socket.destroy(); server.close(); } }, 60_000); it("does not reuse a keep-alive connection when bytes follow a response that ended at its header block", async () => { // Bodyless counterpart of the Content-Length overshoot test below. Each of these // responses is complete once its header block is in (the followed 3xx through the // redirect path, which pools the socket itself), so trailing bytes must cost the // connection while the same responses without them must still be pooled. const injected = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 8\r\n\r\ninjected"; const responses: Record = { "/204": { head: "HTTP/1.1 204 No Content\r\nConnection: keep-alive\r\n\r\n", junk: injected }, "/304": { head: 'HTTP/1.1 304 Not Modified\r\nETag: "x"\r\nConnection: keep-alive\r\n\r\n', junk: injected }, "/empty": { head: "HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: keep-alive\r\n\r\n", junk: injected }, // Answered with HEAD: Content-Length describes the GET body, nothing may follow. // The junk variant sends that body anyway. "/head": { head: "HTTP/1.1 200 OK\r\nContent-Length: 5\r\nConnection: keep-alive\r\n\r\n", junk: "hello" }, "/303": { head: "HTTP/1.1 303 See Other\r\nLocation: /legit\r\nContent-Length: 0\r\nConnection: keep-alive\r\n\r\n", junk: injected, }, }; const legit = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 6\r\nConnection: keep-alive\r\n\r\nlegit!"; let connections = 0; const sockets: net.Socket[] = []; const server = net.createServer(socket => { connections++; sockets.push(socket); socket.on("error", () => {}); let buffered = ""; socket.on("data", data => { buffered += data.toString("latin1"); while (true) { const headerEnd = buffered.indexOf("\r\n\r\n"); if (headerEnd === -1) return; const head = buffered.slice(0, headerEnd); // Consume the request body (the redirect case is a POST) before answering, so // body bytes are never mistaken for the next request. let requestEnd = headerEnd + 4; if (/^transfer-encoding:.*\bchunked\b/im.test(head)) { const terminator = buffered.indexOf("0\r\n\r\n", requestEnd); if (terminator === -1) return; requestEnd = terminator + 5; } else { requestEnd += Number(/^content-length:\s*(\d+)/im.exec(head)?.[1] ?? 0); if (buffered.length < requestEnd) return; } buffered = buffered.slice(requestEnd); const target = head.split("\r\n")[0].split(" ")[1]; const [path, query] = target.split("?"); const response = responses[path]; if (response) { socket.write(response.head + (query === "junk" ? response.junk : "")); } else { socket.write(legit); } } }); }); // 127.0.0.1 explicitly: counting connections requires the server to listen on the // address fetch() connects to. await once(server.listen(0, "127.0.0.1"), "listening"); const { port } = server.address() as AddressInfo; const origin = `http://127.0.0.1:${port}`; const cases: { path: string; init?: () => RequestInit }[] = [ { path: "/204" }, { path: "/304" }, { path: "/empty" }, { path: "/head", init: () => ({ method: "HEAD" }) }, // The redirect path only pools the socket once the upload is known to be // complete, which today is the case for streamed bodies, so a streamed POST is // what exercises that path's pooling decision. { path: "/303", init: () => ({ method: "POST", body: new ReadableStream({ start(controller) { controller.enqueue(new TextEncoder().encode("payload")); controller.close(); }, }), }), }, ]; try { // Warm the pool with one clean connection so every case below starts out // reusing a pooled socket. expect(await (await fetch(`${origin}/legit`)).text()).toBe("legit!"); expect(connections).toBe(1); const results: { request: string; status: number; body: string; newConnections: number }[] = []; for (const { path, init } of cases) { for (const junk of [true, false]) { const before = connections; const response = await fetch(`${origin}${path}${junk ? "?junk" : ""}`, init?.()); const body = await response.text(); // newConnections counts what this response plus the follow-up request opened. const followUp = await fetch(`${origin}/legit`); expect(await followUp.text()).toBe("legit!"); results.push({ request: `${path}${junk ? " + trailing bytes" : ""}`, status: response.status, body, newConnections: connections - before, }); } } expect(results).toEqual([ { request: "/204 + trailing bytes", status: 204, body: "", newConnections: 1 }, { request: "/204", status: 204, body: "", newConnections: 0 }, { request: "/304 + trailing bytes", status: 304, body: "", newConnections: 1 }, { request: "/304", status: 304, body: "", newConnections: 0 }, { request: "/empty + trailing bytes", status: 200, body: "", newConnections: 1 }, { request: "/empty", status: 200, body: "", newConnections: 0 }, { request: "/head + trailing bytes", status: 200, body: "", newConnections: 1 }, { request: "/head", status: 200, body: "", newConnections: 0 }, // The redirect is still followed; only the hop to /legit needs the new connection. { request: "/303 + trailing bytes", status: 200, body: "legit!", newConnections: 1 }, { request: "/303", status: 200, body: "legit!", newConnections: 0 }, ]); } finally { for (const socket of sockets) socket.destroy(); server.close(); } }); it("does not reuse a keep-alive connection whose response carried more bytes than its Content-Length", async () => { // Surplus bytes past the declared Content-Length mean the connection's framing can // no longer be trusted: anything still buffered on (or later delivered to) that // socket would be parsed as the response to whichever request next reuses it from // the keep-alive pool. The mis-framed response itself is still delivered (truncated // to its declared length), but the connection must be closed instead of pooled. let connections = 0; const sockets: net.Socket[] = []; const server = net.createServer(socket => { connections++; sockets.push(socket); let buffered = ""; socket.on("data", data => { buffered += data.toString("latin1"); while (true) { const headerEnd = buffered.indexOf("\r\n\r\n"); if (headerEnd === -1) break; const head = buffered.slice(0, headerEnd); buffered = buffered.slice(headerEnd + 4); const path = head.split("\r\n")[0].split(" ")[1]; if (path === "/overshoot") { // Declares 5 body bytes but sends those 5 plus a complete pipelined // "injected" response that the declared framing never accounted for. socket.write( "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5\r\nConnection: keep-alive\r\n\r\nhello" + "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 8\r\n\r\ninjected", ); } else { socket.write( "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 6\r\nConnection: keep-alive\r\n\r\nlegit!", ); } } }); }); await once(server.listen(0, "localhost"), "listening"); const { port } = server.address() as AddressInfo; try { // The mis-framed response is still delivered, truncated to its declared length. const first = await fetch(`http://localhost:${port}/overshoot`); expect(await first.text()).toBe("hello"); // The follow-up request must go out on a fresh connection, so it can never be // answered by the leftover "injected" bytes on the desynchronized socket. const second = await fetch(`http://localhost:${port}/after`); expect(await second.text()).toBe("legit!"); expect(connections).toBe(2); // A correctly framed keep-alive response is still pooled and reused. const third = await fetch(`http://localhost:${port}/again`); expect(await third.text()).toBe("legit!"); expect(connections).toBe(2); } finally { for (const socket of sockets) socket.destroy(); server.close(); } }); // https://github.com/oven-sh/bun/issues/16682 it("an explicit numeric `timeout` extends the socket idle deadline past the default", async () => { // The child runs with a 1s idle default (BUN_CONFIG_HTTP_IDLE_TIMEOUT=1) and // talks to an in-process server whose handler holds every request idle for // 10s (longer than the worst-case firing window of the 1s idle timer, which // is swept on uSockets' 4s tick) before responding. // // - `timeout: 60_000` must override the 1s idle default and resolve. // - `timeout: 0` must keep meaning "no timeout" and resolve. // - no `timeout` at all must still hit the 1s idle default (control that // proves the env override and the stall are both real). const script = /* js */ ` const HOLD_MS = 10_000; using server = Bun.serve({ port: 0, // Disable Bun.serve's own request idle timeout; only the client-side // idle timer under test may abort anything here. idleTimeout: 0, async fetch(req) { const arrived = Date.now(); // Hold the connection idle (no bytes in either direction) until the // hold window has really elapsed on the server's clock. while (Date.now() - arrived < HOLD_MS) { await Bun.sleep(HOLD_MS - (Date.now() - arrived)); } return new Response("hello"); }, }); const get = init => fetch(server.url, init).then(r => r.text(), e => "ERR:" + (e?.code ?? e?.name ?? e)); const [withTimeout, withZero, withInfinity, withDefault] = await Promise.all([ get({ timeout: 60_000 }), get({ timeout: 0 }), get({ timeout: Infinity }), get(undefined), ]); console.log(JSON.stringify({ withTimeout, withZero, withInfinity, withDefault })); `; await using proc = Bun.spawn({ cmd: [bunExe(), "-e", script], env: { ...bunEnv, BUN_CONFIG_HTTP_IDLE_TIMEOUT: "1" }, stdout: "pipe", stderr: "pipe", }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); const out = JSON.parse(stdout.trim().split("\n").pop()!) as Record; expect({ withTimeout: out.withTimeout, withZero: out.withZero, withInfinity: out.withInfinity }).toEqual({ withTimeout: "hello", withZero: "hello", withInfinity: "hello", }); // Control: without an explicit `timeout`, the 1s idle default still aborts // the stalled request. expect(out.withDefault).toStartWith("ERR:"); expect(exitCode).toBe(0); }, 60_000); it("the idle timer is an absolute deadline for the response header block (not re-armed by a byte drip)", async () => { // A server that trickles one response-header byte at a time, each interval // shorter than the request's idle timeout, must not be able to keep the // request alive indefinitely. The idle timer is armed when the request is // written and is not re-armed on partial header reads, so it bounds how long // the header block may take to arrive in total (undici `headersTimeout` // semantics). Once the header block completes the body path re-arms per // chunk, so a slow-but-steady body is still accepted. const BODY = "abc"; const HEAD = `HTTP/1.1 200 OK\r\nContent-Length: ${BODY.length}\r\n\r\n`; const DRIP_MS = 2_000; const DRIP_N = 10; // header drip sends this many single bytes, then the rest at once const IDLE_MS = 5_000; const sockets = new Set(); const intervals = new Set>(); const server = net.createServer(sock => { sockets.add(sock); sock.on("close", () => sockets.delete(sock)); sock.on("error", () => {}); sock.once("data", chunk => { // /h drips DRIP_N header bytes then bursts the rest + body. // /b bursts the header block then drips the body byte-by-byte. const headerDrip = chunk.includes("/h "); if (!headerDrip) sock.write(HEAD); const dripped = headerDrip ? HEAD.slice(0, DRIP_N) : BODY; const tail = headerDrip ? HEAD.slice(DRIP_N) + BODY : ""; let i = 0; const iv = setInterval(() => { if (sock.destroyed) { clearInterval(iv); intervals.delete(iv); return; } if (i < dripped.length) { sock.write(dripped[i++]); } else { clearInterval(iv); intervals.delete(iv); sock.end(tail); } }, DRIP_MS); intervals.add(iv); }); }); await new Promise(r => server.listen(0, "127.0.0.1", () => r())); const port = (server.address() as AddressInfo).port; try { const settle = (path: string) => fetch(`http://127.0.0.1:${port}${path}`, { timeout: IDLE_MS }).then( async r => ({ ok: true as const, status: r.status, body: await r.text() }), e => ({ ok: false as const, name: e?.name as string, message: String(e?.message ?? e) }), ); // /h: DRIP_N bytes * DRIP_MS = ~20s of drip before the response would // complete; the 5s idle deadline (armed padded on uSockets' 4s-tick // sweep, so it fires at ~8-12s) must fire first. A build that re-arms on // every partial header read resolves 200 after the full drip instead. // /b: headers arrive in one write, then the 3-byte body trickles at // DRIP_MS/byte (~8s). Each body chunk re-arms the idle timer, so this // resolves despite taking longer than IDLE_MS overall. const [hdr, bod] = await Promise.all([settle("/h"), settle("/b")]); expect({ hdr, bod }).toEqual({ hdr: { ok: false, name: "TimeoutError", message: "The operation timed out." }, bod: { ok: true, status: 200, body: BODY }, }); } finally { for (const iv of intervals) clearInterval(iv); for (const s of sockets) s.destroy(); await new Promise(r => server.close(() => r())); } }, 60_000); // https://github.com/oven-sh/bun/issues/39952 it("a numeric `timeout` does not abort in-flight requests on the 4s sweep tick", async () => { // Regression: a `timeout` of 4000ms or less was armed as a single tick of // uSockets' 4s sweep timer, whose phase is unrelated to the request, so the // next sweep aborted whichever request was in flight, no matter how long it // had been running. Keep at least one request in flight for longer than one // full sweep period: none may abort, because each request idles only // ~SLEEP_MS against a 1000ms budget. const SLEEP_MS = 600; const WINDOW_MS = 5_500; // one full 4s sweep period at any phase, plus slop await using server = Bun.serve({ port: 0, idleTimeout: 0, async fetch() { await Bun.sleep(SLEEP_MS); return new Response("ok"); }, }); const errors: string[] = []; const start = Date.now(); const worker = async (offsetMs: number) => { // Stagger the start so the workers' request waves interleave: workers // launched together stay phase-locked, and their inter-request gaps // could all line up with the sweep tick. await Bun.sleep(offsetMs); while (Date.now() - start < WINDOW_MS && errors.length === 0) { const t0 = Date.now(); try { const res = await fetch(server.url, { timeout: 1000 }); await res.text(); } catch (e) { errors.push(`${(e as Error).name} after ${Date.now() - t0}ms on the request started at t=${t0 - start}ms`); } } }; // Three staggered workers so a sweep tick always lands on an in-flight // request in the unfixed build. await Promise.all([worker(0), worker(SLEEP_MS / 3), worker((2 * SLEEP_MS) / 3)]); expect(errors).toEqual([]); }, 20_000); it.skipIf(isWindows)("sends the exact Content-Length for a file body of 100 GB", async () => { using dir = tempDir("fetch-large-file-body", { "large.bin": "" }); const path = join(String(dir), "large.bin"); const size = 100_000_000_000; const fd = openSync(path, "r+"); try { ftruncateSync(fd, size); } finally { closeSync(fd); } expect(Bun.file(path).size).toBe(size); const { promise: headPromise, resolve: resolveHead } = Promise.withResolvers(); let received = ""; let headComplete = false; using listener = Bun.listen({ hostname: "127.0.0.1", port: 0, socket: { data(socket, chunk) { if (headComplete) return; received += chunk.toString("latin1"); const end = received.indexOf("\r\n\r\n"); if (end !== -1) { headComplete = true; resolveHead(received.slice(0, end)); socket.end(); } }, }, }); const controller = new AbortController(); const result = fetch(`http://${listener.hostname}:${listener.port}/upload`, { method: "PUT", body: Bun.file(path), signal: controller.signal, }).then( () => null, e => e, ); const head = await headPromise; controller.abort(); await result; const contentLength = head .split("\r\n") .find(line => line.toLowerCase().startsWith("content-length:")) ?.slice("content-length:".length) .trim(); expect(contentLength).toBe(String(size)); }); it("verbose fetch logging prints [redacted] in place of Authorization credentials", async () => { using server = Bun.serve({ port: 0, fetch(req) { return new Response(req.headers.get("authorization") ?? ""); }, }); await using proc = Bun.spawn({ cmd: [ bunExe(), "-e", `const res = await fetch(process.env.SERVER_URL, { headers: { Authorization: "Bearer sekret-token" } }); console.log(await res.text());`, ], env: { ...bunEnv, BUN_CONFIG_VERBOSE_FETCH: "1", SERVER_URL: server.url.href }, stdout: "pipe", stderr: "pipe", }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); expect(stdout).toBe("Bearer sekret-token\n"); const authorizationLines = stderr.split(/\r?\n/).flatMap(line => { const match = /^(?:\[fetch\])?\s*>?\s*authorization:(.*)$/i.exec(line); return match ? [match[1].trim()] : []; }); expect(authorizationLines).toEqual(["Bearer [redacted]"]); expect(stderr).not.toContain("sekret-token"); expect(exitCode).toBe(0); }); it("verbose fetch logging escapes control characters coming from the peer", async () => { // U+009B is the one-byte form of ESC [ (CSI), so "\u009b31m" written to a terminal as-is turns // the rest of the line red. HTTP/1.1 lets a server put any byte >= 0x80 in a reason phrase or // header value, which includes the UTF-8 encoding of U+009B. let request = ""; using listener = Bun.listen({ hostname: "127.0.0.1", port: 0, socket: { data(socket, chunk) { request += chunk.toString("latin1"); if (!request.includes("\r\n\r\n")) return; socket.end( Buffer.from( "HTTP/1.1 500 Server \u009bError\r\n" + "x-evil: a\u009b31mb\r\n" + "content-length: 0\r\n" + "connection: close\r\n" + "\r\n", ), ); }, }, }); // Header values are byte strings, so the two Latin-1 characters "\u00c2\u009b" are sent as the // bytes C2 9B, i.e. the same UTF-8 encoded U+009B the server sends back. await using proc = Bun.spawn({ cmd: [ bunExe(), "-e", `const res = await fetch(process.env.SERVER_URL, { verbose: "curl", headers: { "x-req": "v\\u00c2\\u009b31mq", authorization: "Bea\\u00c2\\u009brer sekret-token" }, }); console.log(res.status);`, ], env: { ...bunEnv, SERVER_URL: `http://127.0.0.1:${listener.port}/` }, stdout: "pipe", stderr: "pipe", }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); expect(stdout).toBe("500\n"); // The request line and the response status line are printed on their own lines; header lines // are printed once per header in the "name: value" trace and once more in the curl command. expect(stderr).toMatch(/^.*x-req: v\\u009b31mq$/im); expect(stderr).toMatch(/^.*authorization: Bea\\u009brer \[redacted\]$/im); expect(stderr).toMatch(/^< 500 Server \\u009bError$/m); expect(stderr).toMatch(/^< x-evil: a\\u009b31mb$/m); expect(stderr).toContain(`-H "x-req: v\\u009b31mq"`); expect(stderr).not.toMatch(/[\x80-\x9f]/); expect(exitCode).toBe(0); });