Bugfix: Add cheerio and xml2js modules to post-response scripts (#4516)

* Bugfix: Add cheerio and xml2js modules to post-response scripts
* chore: improved cheerio and xml2js test
---------

Co-authored-by: Anoop M D <anoop.md1421@gmail.com>
This commit is contained in:
Andreas Wirth 2025-04-16 22:01:53 +02:00 committed by GitHub
parent 1703346bb6
commit 9a21eec1b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 10 deletions

View File

@ -283,6 +283,8 @@ class ScriptRuntime {
axios,
'node-fetch': fetch,
'crypto-js': CryptoJS,
'xml2js': xml2js,
cheerio,
...whitelistedModules,
fs: allowScriptFilesystemAccess ? fs : undefined,
'node-vault': NodeVault

View File

@ -19,18 +19,35 @@ script:pre-request {
const $ = cheerio.load('<h2 class="title">Hello world</h2>');
$('h2.title').text('Hello there!');
$('h2.title').text('Hello pre-request!');
$('h2').addClass('welcome');
bru.setVar("cheerio-test-html", $.html());
bru.setVar("cheerio-test-pre-request", $.html());
}
script:post-response {
const cheerio = require('cheerio');
const $ = cheerio.load('<h2 class="title">Hello world</h2>');
$('h2.title').text('Hello post-response!');
$('h2').addClass('welcome');
bru.setVar("cheerio-test-post-response", $.html());
}
tests {
const cheerio = require('cheerio');
test("cheerio html - from scripts", function() {
const expected = '<html><head></head><body><h2 class="title welcome">Hello there!</h2></body></html>';
const html = bru.getVar('cheerio-test-html');
test("cheerio html - from pre request script", function() {
const expected = '<html><head></head><body><h2 class="title welcome">Hello pre-request!</h2></body></html>';
const html = bru.getVar('cheerio-test-pre-request');
expect(html).to.eql(expected);
});
test("cheerio html - from post response script", function() {
const expected = '<html><head></head><body><h2 class="title welcome">Hello post-response!</h2></body></html>';
const html = bru.getVar('cheerio-test-post-response');
expect(html).to.eql(expected);
});

View File

@ -12,20 +12,36 @@ get {
script:pre-request {
var parseString = require('xml2js').parseString;
var xml = "<root>Hello xml2js!</root>"
var xml = "<root>Hello xml2js - pre request!</root>"
parseString(xml, function (err, result) {
bru.setVar("xml2js-test-result", result);
bru.setVar("xml2js-test-result-pre-request", result);
});
}
script:post-response {
var parseString = require('xml2js').parseString;
var xml = "<root>Hello xml2js - post response!</root>"
parseString(xml, function (err, result) {
bru.setVar("xml2js-test-result-post-response", result);
});
}
tests {
var parseString = require('xml2js').parseString;
test("xml2js parseString in scripts", function() {
test("xml2js parseString in scripts - pre request", function() {
const expected = {
root: 'Hello xml2js!'
root: 'Hello xml2js - pre request!'
};
const result = bru.getVar('xml2js-test-result');
const result = bru.getVar('xml2js-test-result-pre-request');
expect(result).to.eql(expected);
});
test("xml2js parseString in scripts - post response", function() {
const expected = {
root: 'Hello xml2js - post response!'
};
const result = bru.getVar('xml2js-test-result-post-response');
expect(result).to.eql(expected);
});