Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Webhook Node): Fix handling of form-data files #8256

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/Webhook/Webhook.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -225,7 +225,7 @@ export class Webhook extends Node {
},
};

if (files?.length) {
if (files && Object.keys(files).length) {
returnItem.binary = {};
}

Expand Down
38 changes: 37 additions & 1 deletion packages/nodes-base/nodes/Webhook/test/Webhook.test.ts
Original file line number Diff line number Diff line change
@@ -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<IWebhookFunctions>({
nodeHelpers: mock(),
});
context.getNodeParameter.calledWith('options').mockReturnValue({});
const req = mock<Request>();
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();
});
});
});