Skip to content

Commit 9a21eec

Browse files
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 <[email protected]>
1 parent 1703346 commit 9a21eec

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

packages/bruno-js/src/runtime/script-runtime.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ class ScriptRuntime {
283283
axios,
284284
'node-fetch': fetch,
285285
'crypto-js': CryptoJS,
286+
'xml2js': xml2js,
287+
cheerio,
286288
...whitelistedModules,
287289
fs: allowScriptFilesystemAccess ? fs : undefined,
288290
'node-vault': NodeVault

packages/bruno-tests/collection/scripting/inbuilt modules/cheerio/cheerio.bru

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,35 @@ script:pre-request {
1919

2020
const $ = cheerio.load('<h2 class="title">Hello world</h2>');
2121

22-
$('h2.title').text('Hello there!');
22+
$('h2.title').text('Hello pre-request!');
2323
$('h2').addClass('welcome');
2424

25-
bru.setVar("cheerio-test-html", $.html());
25+
bru.setVar("cheerio-test-pre-request", $.html());
26+
}
27+
28+
script:post-response {
29+
const cheerio = require('cheerio');
30+
31+
const $ = cheerio.load('<h2 class="title">Hello world</h2>');
32+
33+
$('h2.title').text('Hello post-response!');
34+
$('h2').addClass('welcome');
35+
36+
bru.setVar("cheerio-test-post-response", $.html());
2637
}
2738

2839
tests {
2940
const cheerio = require('cheerio');
3041

31-
test("cheerio html - from scripts", function() {
32-
const expected = '<html><head></head><body><h2 class="title welcome">Hello there!</h2></body></html>';
33-
const html = bru.getVar('cheerio-test-html');
42+
test("cheerio html - from pre request script", function() {
43+
const expected = '<html><head></head><body><h2 class="title welcome">Hello pre-request!</h2></body></html>';
44+
const html = bru.getVar('cheerio-test-pre-request');
45+
expect(html).to.eql(expected);
46+
});
47+
48+
test("cheerio html - from post response script", function() {
49+
const expected = '<html><head></head><body><h2 class="title welcome">Hello post-response!</h2></body></html>';
50+
const html = bru.getVar('cheerio-test-post-response');
3451
expect(html).to.eql(expected);
3552
});
3653

packages/bruno-tests/collection/scripting/inbuilt modules/xml2js/xml2js.bru

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,36 @@ get {
1212

1313
script:pre-request {
1414
var parseString = require('xml2js').parseString;
15-
var xml = "<root>Hello xml2js!</root>"
15+
var xml = "<root>Hello xml2js - pre request!</root>"
1616
parseString(xml, function (err, result) {
17-
bru.setVar("xml2js-test-result", result);
17+
bru.setVar("xml2js-test-result-pre-request", result);
18+
});
19+
}
20+
21+
script:post-response {
22+
var parseString = require('xml2js').parseString;
23+
var xml = "<root>Hello xml2js - post response!</root>"
24+
parseString(xml, function (err, result) {
25+
bru.setVar("xml2js-test-result-post-response", result);
1826
});
1927
}
2028

2129
tests {
2230
var parseString = require('xml2js').parseString;
2331

24-
test("xml2js parseString in scripts", function() {
32+
test("xml2js parseString in scripts - pre request", function() {
33+
const expected = {
34+
root: 'Hello xml2js - pre request!'
35+
};
36+
const result = bru.getVar('xml2js-test-result-pre-request');
37+
expect(result).to.eql(expected);
38+
});
39+
40+
test("xml2js parseString in scripts - post response", function() {
2541
const expected = {
26-
root: 'Hello xml2js!'
42+
root: 'Hello xml2js - post response!'
2743
};
28-
const result = bru.getVar('xml2js-test-result');
44+
const result = bru.getVar('xml2js-test-result-post-response');
2945
expect(result).to.eql(expected);
3046
});
3147

0 commit comments

Comments
 (0)