From cf37e94dd875e9f6ab1f189146fb34e7296af93c Mon Sep 17 00:00:00 2001 From: Eugene Date: Wed, 30 Oct 2024 10:32:08 +0100 Subject: [PATCH] fix(HTTP Request Tool Node): Fix HTML response optimization issue (#11439) --- .../test/ToolHttpRequest.node.test.ts | 82 ++++++++++++++++--- .../nodes/tools/ToolHttpRequest/utils.ts | 2 +- 2 files changed, 71 insertions(+), 13 deletions(-) diff --git a/packages/@n8n/nodes-langchain/nodes/tools/ToolHttpRequest/test/ToolHttpRequest.node.test.ts b/packages/@n8n/nodes-langchain/nodes/tools/ToolHttpRequest/test/ToolHttpRequest.node.test.ts index f18a2437e3..1a99896fff 100644 --- a/packages/@n8n/nodes-langchain/nodes/tools/ToolHttpRequest/test/ToolHttpRequest.node.test.ts +++ b/packages/@n8n/nodes-langchain/nodes/tools/ToolHttpRequest/test/ToolHttpRequest.node.test.ts @@ -10,19 +10,19 @@ describe('ToolHttpRequest', () => { const helpers = mock(); const executeFunctions = mock({ helpers }); - describe('Binary response', () => { - beforeEach(() => { - jest.resetAllMocks(); - executeFunctions.getNode.mockReturnValue( - mock({ - type: 'n8n-nodes-base.httpRequest', - name: 'HTTP Request', - typeVersion: 1.1, - }), - ); - executeFunctions.addInputData.mockReturnValue({ index: 0 }); - }); + beforeEach(() => { + jest.resetAllMocks(); + executeFunctions.getNode.mockReturnValue( + mock({ + type: 'n8n-nodes-base.httpRequest', + name: 'HTTP Request', + typeVersion: 1.1, + }), + ); + executeFunctions.addInputData.mockReturnValue({ index: 0 }); + }); + describe('Binary response', () => { it('should return the error when receiving a binary response', async () => { helpers.httpRequest.mockResolvedValue({ body: Buffer.from(''), @@ -237,4 +237,62 @@ describe('ToolHttpRequest', () => { ); }); }); + + describe('Optimize response', () => { + it('should extract body from the response HTML', async () => { + helpers.httpRequest.mockResolvedValue({ + body: ` + + + + +

Test

+ +
+

+ Test content +

+
+ +`, + headers: { + 'content-type': 'text/html', + }, + }); + + executeFunctions.getNodeParameter.mockImplementation( + (paramName: string, _: any, fallback: any) => { + switch (paramName) { + case 'method': + return 'GET'; + case 'url': + return '{url}'; + case 'options': + return {}; + case 'placeholderDefinitions.values': + return []; + case 'optimizeResponse': + return true; + case 'responseType': + return 'html'; + case 'cssSelector': + return 'body'; + default: + return fallback; + } + }, + ); + + const { response } = await httpTool.supplyData.call(executeFunctions, 0); + + const res = await (response as N8nTool).invoke({ + url: 'https://httpbin.org/html', + }); + + expect(helpers.httpRequest).toHaveBeenCalled(); + expect(res).toEqual( + JSON.stringify(['

Test

Test content

'], null, 2), + ); + }); + }); }); diff --git a/packages/@n8n/nodes-langchain/nodes/tools/ToolHttpRequest/utils.ts b/packages/@n8n/nodes-langchain/nodes/tools/ToolHttpRequest/utils.ts index 74f40f0a02..f1d6dfd150 100644 --- a/packages/@n8n/nodes-langchain/nodes/tools/ToolHttpRequest/utils.ts +++ b/packages/@n8n/nodes-langchain/nodes/tools/ToolHttpRequest/utils.ts @@ -1,5 +1,5 @@ import { Readability } from '@mozilla/readability'; -import cheerio from 'cheerio'; +import * as cheerio from 'cheerio'; import { convert } from 'html-to-text'; import { JSDOM } from 'jsdom'; import get from 'lodash/get';