Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/rules/passthrough-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions test/passthrough-handling.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});

});

});