From 6dd4629ee15d34984b093e615c6ea90bf2b97bcb Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 27 Nov 2026 06:53:15 -0400 Subject: [PATCH] fix: honour falsy json overrides when beforeRequest returns the original body --- src/rules/requests/request-step-impls.ts | 2 +- .../proxying/http-proxying.spec.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/rules/requests/request-step-impls.ts b/src/rules/requests/request-step-impls.ts index 199f79aab..ec6c21803 100644 --- a/src/rules/requests/request-step-impls.ts +++ b/src/rules/requests/request-step-impls.ts @@ -452,7 +452,7 @@ function returnsOriginalBody( resultingHeaders: Headers ) { if (result?.rawBody) return result.rawBody === originalBody.buffer; - if (result?.json) return false; + if (result?.json !== undefined) return false; const returnedBody: unknown = result?.body; const isSameBody = returnedBody === originalBody || returnedBody === originalBody.buffer; diff --git a/test/integration/proxying/http-proxying.spec.ts b/test/integration/proxying/http-proxying.spec.ts index 39dafa279..0b61176a0 100644 --- a/test/integration/proxying/http-proxying.spec.ts +++ b/test/integration/proxying/http-proxying.spec.ts @@ -811,6 +811,25 @@ nodeOnly(() => { expect(response).to.deep.equal({ hello: "world" }); }); + it("should rewrite a spread request's body with a falsy JSON value", async () => { + await remoteServer.forAnyRequest().thenCallback(async (req) => ({ + status: 200, + body: await req.body.getText() + })); + + for (const value of [false, 0, '', null]) { + await server.reset(); + await server.forPost(remoteServer.urlFor("/")).thenPassThrough({ + beforeRequest: (req) => ({ ...req, body: req.body.buffer, json: value }) + }); + + const response = await request.post(remoteServer.urlFor("/"), { + body: 'original body' + }); + expect(response).to.equal(JSON.stringify(value)); + } + }); + it("should be able to rewrite a request's body as JSON and encode it automatically", async () => { await remoteServer.forAnyRequest().thenCallback(async (req) => ({ status: 200,