diff options
author | 2023-03-01 23:28:21 -0600 | |
---|---|---|
committer | 2023-03-01 21:28:21 -0800 | |
commit | b9137dbdc81591f8b30cf95a4d27514bfb1ae71c (patch) | |
tree | c369d655111fbc3d1d346dbfc22f3d2f699a33e9 | |
parent | 706a3e8169ae27b1b5c3694d46b593f220c41b80 (diff) | |
download | bun-b9137dbdc81591f8b30cf95a4d27514bfb1ae71c.tar.gz bun-b9137dbdc81591f8b30cf95a4d27514bfb1ae71c.tar.zst bun-b9137dbdc81591f8b30cf95a4d27514bfb1ae71c.zip |
fix(node:http): match Node `http.request()` GET/HEAD w/ body (#2262)
-rw-r--r-- | src/bun.js/http.exports.js | 8 | ||||
-rw-r--r-- | test/bun.js/node-http.test.ts | 25 |
2 files changed, 31 insertions, 2 deletions
diff --git a/src/bun.js/http.exports.js b/src/bun.js/http.exports.js index a7f67f0e9..d21f768d4 100644 --- a/src/bun.js/http.exports.js +++ b/src/bun.js/http.exports.js @@ -959,10 +959,14 @@ export class ClientRequest extends OutgoingMessage { if (this.#signal?.aborted) { this[kAbortController].abort(); } + + var method = this.#method, + body = this.#body; + this.#fetchRequest = fetch(`${this.#protocol}//${this.#host}:${this.#port}${this.#path}`, { - method: this.#method, + method, headers: this.getHeaders(), - body: this.#body || undefined, + body: body && method !== "GET" && method !== "HEAD" && method !== "OPTIONS" ? body : undefined, redirect: "manual", verbose: Boolean(__DEBUG__), signal: this[kAbortController].signal, diff --git a/test/bun.js/node-http.test.ts b/test/bun.js/node-http.test.ts index 40102cd9a..7818fba62 100644 --- a/test/bun.js/node-http.test.ts +++ b/test/bun.js/node-http.test.ts @@ -386,6 +386,31 @@ describe("node:http", () => { req.write("Hello World"); req.end(); }); + + it("should ignore body when method is GET/HEAD/OPTIONS", done => { + const createDone = createDoneDotAll(done); + const methods = ["GET", "HEAD", "OPTIONS"]; + const dones = {}; + for (const method of methods) { + dones[method] = createDone(); + } + for (const method of methods) { + const req = request(`http://localhost:${serverPort}`, { method }, res => { + let data = ""; + res.setEncoding("utf8"); + res.on("data", chunk => { + data += chunk; + }); + res.on("end", () => { + expect(data).toBe(method === "GET" ? "Maybe GET maybe not\nHello World" : ""); + dones[method](); + }); + res.on("error", err => dones[method](err)); + }); + req.write("BODY"); + req.end(); + } + }); }); describe("signal", () => { |