n8n/packages/cli/test/integration/webhooks.api.test.ts
कारतोफ्फेलस्क्रिप्ट™ 31d8f478ee
refactor(core): Parse Webhook request bodies on-demand (#6394)
Also,
1. Consistent CORS support ~on all three webhook types~ waiting webhooks never supported CORS. I'll fix that in another PR
2. [Fixes binary-data handling when request body is text, json, or xml](https://linear.app/n8n/issue/NODE-505/webhook-binary-data-handling-fails-for-textplain-files).
3. Reduced number of middleware that each request has to go through.
4. Removed the need to maintain webhook endpoints in the auth-exception list.
5. Skip all middlewares (apart from `compression`) on Webhook routes. 
6. move `multipart/form-data` support out of individual nodes
7. upgrade `formidable`
8. fix the filenames on binary-data in webhooks nodes
9. add unit tests and integration tests for webhook request handling, and increase test coverage
2023-08-01 17:32:30 +02:00

179 lines
4.6 KiB
TypeScript

import { readFileSync } from 'fs';
import type { SuperAgentTest } from 'supertest';
import { agent as testAgent } from 'supertest';
import type { INodeType, INodeTypeDescription, IWebhookFunctions } from 'n8n-workflow';
import { LoggerProxy } from 'n8n-workflow';
import { AbstractServer } from '@/AbstractServer';
import { ExternalHooks } from '@/ExternalHooks';
import { InternalHooks } from '@/InternalHooks';
import { getLogger } from '@/Logger';
import { NodeTypes } from '@/NodeTypes';
import { Push } from '@/push';
import { mockInstance, initActiveWorkflowRunner } from './shared/utils';
import * as testDb from './shared/testDb';
describe('Webhook API', () => {
mockInstance(ExternalHooks);
mockInstance(InternalHooks);
mockInstance(Push);
LoggerProxy.init(getLogger());
let agent: SuperAgentTest;
describe('Content-Type support', () => {
beforeAll(async () => {
await testDb.init();
const node = new WebhookTestingNode();
const user = await testDb.createUser();
await testDb.createWorkflow(
{
active: true,
nodes: [
{
name: 'Webhook',
type: node.description.name,
typeVersion: 1,
parameters: {
httpMethod: 'POST',
path: 'abcd',
},
id: '74786112-fb73-4d80-bd9a-43982939b801',
webhookId: '5ccef736-be16-4d10-b7fb-feed7a61ff22',
position: [740, 420],
},
],
},
user,
);
const nodeTypes = mockInstance(NodeTypes);
nodeTypes.getByName.mockReturnValue(node);
nodeTypes.getByNameAndVersion.mockReturnValue(node);
await initActiveWorkflowRunner();
const server = new (class extends AbstractServer {})();
await server.start();
agent = testAgent(server.app);
});
test('should handle JSON', async () => {
const response = await agent.post('/webhook/abcd').send({ test: true });
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({ type: 'application/json', body: { test: true } });
});
test('should handle XML', async () => {
const response = await agent
.post('/webhook/abcd')
.set('content-type', 'application/xml')
.send(
'<?xml version="1.0" encoding="UTF-8"?><Outer attr="test"><Inner>value</Inner></Outer>',
);
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
type: 'application/xml',
body: {
outer: {
$: {
attr: 'test',
},
inner: 'value',
},
},
});
});
test('should handle form-urlencoded', async () => {
const response = await agent
.post('/webhook/abcd')
.set('content-type', 'application/x-www-form-urlencoded')
.send('x=5&y=str&z=false');
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
type: 'application/x-www-form-urlencoded',
body: { x: '5', y: 'str', z: 'false' },
});
});
test('should handle plain text', async () => {
const response = await agent
.post('/webhook/abcd')
.set('content-type', 'text/plain')
.send('{"key": "value"}');
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
type: 'text/plain',
body: '{"key": "value"}',
});
});
test('should handle multipart/form-data', async () => {
const response = await agent
.post('/webhook/abcd')
.field('field', 'value')
.attach('file', Buffer.from('random-text'))
.set('content-type', 'multipart/form-data');
expect(response.statusCode).toEqual(200);
expect(response.body.type).toEqual('multipart/form-data');
const {
data,
files: {
file: [file],
},
} = response.body.body;
expect(data).toEqual({ field: ['value'] });
expect(file.mimetype).toEqual('application/octet-stream');
expect(readFileSync(file.filepath, 'utf-8')).toEqual('random-text');
});
});
class WebhookTestingNode implements INodeType {
description: INodeTypeDescription = {
displayName: 'Webhook Testing Node',
name: 'webhook-testing-node',
group: ['trigger'],
version: 1,
description: '',
defaults: {},
inputs: [],
outputs: ['main'],
webhooks: [
{
name: 'default',
isFullPath: true,
httpMethod: '={{$parameter["httpMethod"]}}',
path: '={{$parameter["path"]}}',
},
],
properties: [
{
name: 'httpMethod',
type: 'string',
displayName: 'Method',
default: 'GET',
},
{
displayName: 'Path',
name: 'path',
type: 'string',
default: 'xyz',
},
],
};
async webhook(this: IWebhookFunctions) {
const req = this.getRequestObject();
return {
webhookResponse: {
type: req.contentType,
body: req.body,
},
};
}
}
});