From 4ea86cac23956b0a6a3edad76b5e623dbd96028f Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Wed, 18 Nov 2026 19:05:43 -0400 Subject: [PATCH] fix: honour falsy json values in beforeRequest/beforeResponse overrides buildOverriddenBody ignored json overrides of false, 0, '' and null because it checked truthiness instead of undefined. --- src/rules/passthrough-handling.ts | 2 +- test/passthrough-handling.spec.ts | 35 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/passthrough-handling.spec.ts diff --git a/src/rules/passthrough-handling.ts b/src/rules/passthrough-handling.ts index 8e8660a28..01ba0f2e9 100644 --- a/src/rules/passthrough-handling.ts +++ b/src/rules/passthrough-handling.ts @@ -239,7 +239,7 @@ export async function buildOverriddenBody( // for consistent handling later, and encode it to match the headers. let replacementBody: string | Uint8Array | Buffer | CompletedBody | undefined; - if (callbackResult?.json) { + if (callbackResult?.json !== undefined) { headers['content-type'] = 'application/json'; replacementBody = JSON.stringify(callbackResult?.json); } else { diff --git a/test/passthrough-handling.spec.ts b/test/passthrough-handling.spec.ts new file mode 100644 index 000000000..925efd908 --- /dev/null +++ b/test/passthrough-handling.spec.ts @@ -0,0 +1,35 @@ +import { buildOverriddenBody } from "../src/rules/passthrough-handling"; +import { expect } from "./test-utils"; + +describe("Passthrough handling", () => { + + describe("buildOverriddenBody", () => { + + [ + ['false', false], + ['0', 0], + ['an empty string', ''], + ['null', null], + ['true', true], + ['an object', { a: 1 }] + ].forEach(([name, json]) => { + it(`uses ${name} as a JSON body override`, async () => { + const headers: { [key: string]: any } = {}; + const body = await buildOverriddenBody({ json } as any, headers); + + expect(body?.toString()).to.equal(JSON.stringify(json)); + expect(headers['content-type']).to.equal('application/json'); + }); + }); + + it("returns no override if no body or json is provided", async () => { + const headers: { [key: string]: any } = {}; + const body = await buildOverriddenBody({}, headers); + + expect(body).to.equal(undefined); + expect(headers['content-type']).to.equal(undefined); + }); + + }); + +});