mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-21 02:56:40 -08:00
fix(HTTP Request Tool Node): Fix HTML response optimization issue (#11439)
This commit is contained in:
parent
d7ba206b30
commit
cf37e94dd8
|
@ -10,7 +10,6 @@ describe('ToolHttpRequest', () => {
|
|||
const helpers = mock<IExecuteFunctions['helpers']>();
|
||||
const executeFunctions = mock<IExecuteFunctions>({ helpers });
|
||||
|
||||
describe('Binary response', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
executeFunctions.getNode.mockReturnValue(
|
||||
|
@ -23,6 +22,7 @@ describe('ToolHttpRequest', () => {
|
|||
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: `<!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),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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';
|
||||
|
|
Loading…
Reference in a new issue