From fc2903096e6e64e5b2a14593418d5651e07ca9ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Mon, 8 Jan 2024 14:33:14 +0100 Subject: [PATCH] fix(Webhook Node): Fix handling of form-data files (#8256) --- .../nodes-base/nodes/Webhook/Webhook.node.ts | 4 +- .../nodes/Webhook/test/Webhook.test.ts | 38 ++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/packages/nodes-base/nodes/Webhook/Webhook.node.ts b/packages/nodes-base/nodes/Webhook/Webhook.node.ts index e2f42d25ae..e0a194cdb3 100644 --- a/packages/nodes-base/nodes/Webhook/Webhook.node.ts +++ b/packages/nodes-base/nodes/Webhook/Webhook.node.ts @@ -99,7 +99,7 @@ export class Webhook extends Node { const options = context.getNodeParameter('options', {}) as { binaryData: boolean; ignoreBots: boolean; - rawBody: Buffer; + rawBody: boolean; responseData?: string; }; const req = context.getRequestObject(); @@ -225,7 +225,7 @@ export class Webhook extends Node { }, }; - if (files?.length) { + if (files && Object.keys(files).length) { returnItem.binary = {}; } diff --git a/packages/nodes-base/nodes/Webhook/test/Webhook.test.ts b/packages/nodes-base/nodes/Webhook/test/Webhook.test.ts index bb903fd934..246f7dae1e 100644 --- a/packages/nodes-base/nodes/Webhook/test/Webhook.test.ts +++ b/packages/nodes-base/nodes/Webhook/test/Webhook.test.ts @@ -1,4 +1,40 @@ +import type { Request } from 'express'; +import type { IWebhookFunctions } from 'n8n-workflow'; +import { mock } from 'jest-mock-extended'; +import { Webhook } from '../Webhook.node'; import { testWorkflows, getWorkflowFilenames } from '@test/nodes/Helpers'; + const workflows = getWorkflowFilenames(__dirname); -describe('Test Webhook Node', () => testWorkflows(workflows)); +describe('Test Webhook Node', () => { + testWorkflows(workflows); + + describe('handleFormData', () => { + const node = new Webhook(); + const context = mock({ + nodeHelpers: mock(), + }); + context.getNodeParameter.calledWith('options').mockReturnValue({}); + const req = mock(); + req.contentType = 'multipart/form-data'; + context.getRequestObject.mockReturnValue(req); + + it('should handle when no files are present', async () => { + req.body = { + files: {}, + }; + const returnData = await node.webhook(context); + expect(returnData.workflowData?.[0][0].binary).toBeUndefined(); + expect(context.nodeHelpers.copyBinaryFile).not.toHaveBeenCalled(); + }); + + it('should handle when files are present', async () => { + req.body = { + files: { file1: {} }, + }; + const returnData = await node.webhook(context); + expect(returnData.workflowData?.[0][0].binary).not.toBeUndefined(); + expect(context.nodeHelpers.copyBinaryFile).toHaveBeenCalled(); + }); + }); +});