Skip to content

Commit

Permalink
fix(HTTP Request Tool Node): Fix HTML response optimization issue (n8…
Browse files Browse the repository at this point in the history
  • Loading branch information
burivuhster authored Oct 30, 2024
1 parent d7ba206 commit cf37e94
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ describe('ToolHttpRequest', () => {
const helpers = mock<IExecuteFunctions['helpers']>();
const executeFunctions = mock<IExecuteFunctions>({ helpers });

describe('Binary response', () => {
beforeEach(() => {
jest.resetAllMocks();
executeFunctions.getNode.mockReturnValue(
mock<INode>({
type: 'n8n-nodes-base.httpRequest',
name: 'HTTP Request',
typeVersion: 1.1,
}),
);
executeFunctions.addInputData.mockReturnValue({ index: 0 });
});
beforeEach(() => {
jest.resetAllMocks();
executeFunctions.getNode.mockReturnValue(
mock<INode>({
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(''),
Expand Down Expand Up @@ -237,4 +237,62 @@ describe('ToolHttpRequest', () => {
);
});
});

describe('Optimize response', () => {
it('should extract body from the response HTML', async () => {
helpers.httpRequest.mockResolvedValue({
body: `<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Test</h1>
<div>
<p>
Test content
</p>
</div>
</body>
</html>`,
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(['<h1>Test</h1> <div> <p> Test content </p> </div>'], null, 2),
);
});
});
});
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit cf37e94

Please sign in to comment.