feat: Implement streaming response node on ChatTrigger and Webhook (no-changelog) (#16761)

This commit is contained in:
Benjamin Schroth
2025-07-01 11:03:23 +02:00
committed by GitHub
parent 8e62c80d48
commit 47bf4b77d3
5 changed files with 510 additions and 167 deletions

View File

@@ -1,5 +1,5 @@
import { NodeTestHarness } from '@nodes-testing/node-test-harness';
import type { Request } from 'express';
import type { Request, Response } from 'express';
import { mock } from 'jest-mock-extended';
import type { IWebhookFunctions } from 'n8n-workflow';
@@ -40,4 +40,93 @@ describe('Test Webhook Node', () => {
expect(context.nodeHelpers.copyBinaryFile).toHaveBeenCalled();
});
});
describe('streaming response mode', () => {
const node = new Webhook();
const context = mock<IWebhookFunctions>({
nodeHelpers: mock(),
});
const req = mock<Request>();
const res = mock<Response>();
beforeEach(() => {
jest.clearAllMocks();
context.getRequestObject.mockReturnValue(req);
context.getResponseObject.mockReturnValue(res);
context.getChildNodes.mockReturnValue([]);
context.getNode.mockReturnValue({
type: 'n8n-nodes-base.webhook',
typeVersion: 2,
name: 'Webhook',
} as any);
context.getNodeParameter.mockImplementation((paramName: string) => {
if (paramName === 'options') return {};
if (paramName === 'responseMode') return 'streaming';
return undefined;
});
req.headers = {};
req.params = {};
req.query = {};
req.body = { message: 'test' };
Object.defineProperty(req, 'ips', { value: [], configurable: true });
Object.defineProperty(req, 'ip', { value: '127.0.0.1', configurable: true });
res.writeHead.mockImplementation(() => res);
res.flushHeaders.mockImplementation(() => undefined);
});
it('should enable streaming when responseMode is "streaming"', async () => {
const result = await node.webhook(context);
// Verify streaming headers are set
expect(res.writeHead).toHaveBeenCalledWith(200, {
'Content-Type': 'application/json; charset=utf-8',
'Transfer-Encoding': 'chunked',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
});
expect(res.flushHeaders).toHaveBeenCalled();
// Verify response structure for streaming
expect(result).toEqual({
noWebhookResponse: true,
workflowData: expect.any(Array),
});
});
it('should not enable streaming when responseMode is not "streaming"', async () => {
context.getNodeParameter.mockImplementation((paramName: string) => {
if (paramName === 'options') return {};
if (paramName === 'responseMode') return 'onReceived';
return undefined;
});
const result = await node.webhook(context);
// Verify streaming headers are NOT set
expect(res.writeHead).not.toHaveBeenCalled();
expect(res.flushHeaders).not.toHaveBeenCalled();
// Verify normal response structure
expect(result).toEqual({
webhookResponse: undefined,
workflowData: expect.any(Array),
});
});
it('should handle multipart form data with streaming enabled', async () => {
req.contentType = 'multipart/form-data';
req.body = {
data: { message: 'Hello' },
files: {},
};
const result = await node.webhook(context);
// For multipart form data, streaming is handled in handleFormData method
// The current implementation returns normal workflowData for form data
expect(result).toEqual({
workflowData: expect.any(Array),
});
});
});
});