From 6adb5cfe7955447d0d576a1d7b785e20e3988a41 Mon Sep 17 00:00:00 2001 From: melohagan <101575380+melohagan@users.noreply.github.com> Date: Wed, 20 Dec 2023 11:02:59 +0000 Subject: [PATCH] Do not throw error on 204 no content (#12643) --- packages/server/src/integrations/rest.ts | 5 ++++- .../src/integrations/tests/rest.spec.ts | 22 +++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/server/src/integrations/rest.ts b/packages/server/src/integrations/rest.ts index e9d1d8268e..9cb8f8e2c1 100644 --- a/packages/server/src/integrations/rest.ts +++ b/packages/server/src/integrations/rest.ts @@ -131,7 +131,10 @@ class RestIntegration implements IntegrationBase { let data, raw, headers const contentType = response.headers.get("content-type") || "" try { - if (contentType.includes("application/json")) { + if (response.status === 204) { + data = [] + raw = [] + } else if (contentType.includes("application/json")) { data = await response.json() raw = JSON.stringify(data) } else if ( diff --git a/packages/server/src/integrations/tests/rest.spec.ts b/packages/server/src/integrations/tests/rest.spec.ts index 98c57580a3..bc0c0cac2f 100644 --- a/packages/server/src/integrations/tests/rest.spec.ts +++ b/packages/server/src/integrations/tests/rest.spec.ts @@ -186,9 +186,15 @@ describe("REST Integration", () => { }) describe("response", () => { - function buildInput(json: any, text: any, header: any) { + const contentTypes = ["application/json", "text/plain", "application/xml"] + function buildInput( + json: any, + text: any, + header: any, + status: number = 200 + ) { return { - status: 200, + status, json: json ? async () => json : undefined, text: text ? async () => text : undefined, headers: { @@ -225,6 +231,18 @@ describe("REST Integration", () => { expect(output.extra.raw).toEqual(text) expect(output.extra.headers["content-type"]).toEqual("application/xml") }) + + test.each(contentTypes)( + "should not throw an error on 204 no content", + async contentType => { + const input = buildInput(undefined, null, contentType, 204) + const output = await config.integration.parseResponse(input) + expect(output.data).toEqual([]) + expect(output.extra.raw).toEqual([]) + expect(output.info.code).toEqual(204) + expect(output.extra.headers["content-type"]).toEqual(contentType) + } + ) }) describe("authentication", () => {