forked from vdespa/write-write-response
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriteToFile.js
More file actions
45 lines (39 loc) · 1.33 KB
/
writeToFile.js
File metadata and controls
45 lines (39 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const newman = require('newman'); // require newman in your project
const fs = require('fs');
// call newman.run to pass `options` object and wait for callback
newman.run({
collection: require('./collection.json'),
reporters: 'cli'
}).on('beforeRequest', (error, data) => {
if (error) {
console.log(error);
return;
}
if (data.request.body) {
const requestName = data.item.name.replace(/[^a-z0-9]/gi, '-');
const randomString = Math.random().toString(36).substring(7);
const fileName = `request-${requestName}-${randomString}.txt`;
const content = data.request.body.raw;
fs.writeFile(fileName, content, function (error) {
if (error) {
console.error(error);
}
});
}
})
.on('request', (error, data) => {
if (error) {
console.log(error);
return;
}
const requestName = data.item.name.replace(/[^a-z0-9]/gi, '-');
const randomString = Math.random().toString(36).substring(7);
const fileName = `response-${requestName}-${randomString}.txt`;
const content = data.response.stream.toString();
fs.writeFile(fileName, content, function (error) {
if (error) {
console.error(error);
}
});
/// TODO: all global error handling
});