-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathnewmanTestUtil.js
215 lines (206 loc) · 7.58 KB
/
newmanTestUtil.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
var fs = require('fs'),
expect = require('chai').expect,
exec = require('shelljs').exec,
{ Request } = require('postman-collection/lib/collection/request'),
path = require('path'),
newmanResponses = require('./newmanResponses.json'),
async = require('async');
const PATH_TO_COLLECTIONS = path.resolve(__dirname, './fixtures');
/**
*
* @param {String} collection_folder - path to the collections folder
* @returns {Array} - Array of objects, with each object containing path and name of the collection
*/
function getCollections (collection_folder) {
return fs.readdirSync(collection_folder)
.map((collection) => {
return {
path: path.join(collection_folder, collection),
name: collection.includes('.') ? collection.split('.')[0] : collection
};
});
}
/**
* compiles and runs codesnippet then compare it with newman output
*
* @param {Object} testConfig - Config essential to run code snippet.
* @param {String} testConfig.headerSnippet - Header snippet required to run the snippet. Default - ''
* @param {String} testConfig.footerSnippet - Footer snippet required to run snippet. Default - ''
* @param {String} testConfig.runScript - Script required to run code snippet
* @param {String} testConfig.compileScript - Script required to compile code snippet
* @param {String} testConfig.fileName - Filename with extension
* @param {Object} snippets - Array of generated code codeSnippets.
* @param {String} collectionName
*/
function runSnippet (testConfig, snippets, collectionName) {
var currentCollectionResponses = newmanResponses[collectionName];
snippets.forEach((item, index) => {
var headerSnippet = testConfig.headerSnippet ? testConfig.headerSnippet : '',
footerSnippet = testConfig.footerSnippet ? testConfig.footerSnippet : '',
codeSnippet = headerSnippet + item.snippet + footerSnippet;
it(item.name, function (done) {
if (testConfig.fileName) {
fs.writeFileSync(testConfig.fileName, codeSnippet);
}
// bash command string for compiling codeSnippet
var compile = testConfig.compileScript ? testConfig.compileScript : null,
// bash command stirng for run compiled file file
run = testConfig.runScript ? testConfig.runScript : codeSnippet;
// step by step process for compile, run code snippet
async.waterfall([
function compileCodeSnippet (next) {
if (compile) {
return exec(compile, function (code, stdout, stderr) {
if (code) {
return next(JSON.stringify({
exitCode: code,
message: 'Compile error'
}));
}
if (stderr) {
return next(JSON.stringify({
stderr: stderr,
message: 'Compile error'
}));
}
console.log(stdout);
return next(null);
});
}
return next(null);
},
function runCodeSnippet (next) {
if (run) {
return exec(run, function (code, stdout, stderr) {
if (code) {
return next(code);
}
if (stderr) {
return next(stderr);
}
try {
stdout = JSON.parse(stdout);
}
catch (e) {
console.error(e);
}
return next(null, stdout);
});
}
}
], function (err, response) {
var result = [response, currentCollectionResponses[index]];
if (err) {
expect.fail(null, null, err);
}
else if (typeof result[1] !== 'object' || typeof result[0] !== 'object') {
expect(result[0].toString().trim()).to.include(result[1].toString().trim());
}
const propertiesTodelete = ['cookies', 'headersSize', 'startedDateTime', 'clientIPAddress'],
headersTodelete = [
'accept-encoding',
'user-agent',
'cf-ray',
'x-real-ip',
'x-request-id',
'kong-request-id',
'x-request-start',
'connect-time',
'x-forwarded-for',
'content-type',
'content-length',
'accept',
'total-route-time',
'cookie',
'kong-cloud-request-id',
'cache-control',
'postman-token',
'accept-language',
'x-forwarded-port',
'if-none-match',
'referer',
'x-amzn-trace-id',
'transfer-encoding',
'cf-connecting-ip',
'cf-request-id'
];
if (result[0]) {
propertiesTodelete.forEach(function (property) {
delete result[0][property];
});
if (result[0].headers) {
headersTodelete.forEach(function (property) {
delete result[0].headers[property];
});
}
}
if (result[1]) {
propertiesTodelete.forEach(function (property) {
delete result[1][property];
});
if (result[1].headers) {
headersTodelete.forEach(function (property) {
delete result[1].headers[property];
});
}
}
expect(result[0].toString().trim()).deep.equal(result[1].toString().trim());
return done(null);
});
});
});
}
module.exports = {
/**
* compiles and runs codesnippet then compare it with newman output
*
* @param {Function} convert - convert function of the codegen
* @param {Object} options - options to be passed to the convert function
* @param {Object} testConfig - Config essential to run code snippet.
* @param {String} testConfig.headerSnippet - Header snippet required to run the snippet. Default - ''
* @param {String} testConfig.footerSnippet - Footer snippet required to run snippet. Default - ''
* @param {String} testConfig.runScript - Script required to run code snippet
* @param {String} testConfig.compileScript - Script required to compile code snippet
* @param {String} testConfig.fileName - Filename with extension
*/
runNewmanTest: function (convert, options, testConfig) {
const collections = getCollections(PATH_TO_COLLECTIONS),
// array of collections that need to be skipped for this codegen.
collectionsToSkip = testConfig.skipCollections ? testConfig.skipCollections : [];
async.eachSeries(collections, (collectionObj, callback) => {
if (!collectionsToSkip.includes(collectionObj.name)) {
// Convert code snippet
var collection = require(collectionObj.path);
async.map(collection.item, function (item, cb) {
var request = new Request(item.request);
convert(request, options, function (err, snippet) {
if (err) {
return cb(err);
}
return cb(null, {
name: item.name,
snippet: snippet
});
});
}, function (err, snippets) {
if (err) {
return callback(err);
}
// Run code snippet.
describe('\nRunning newman test for: ' + collectionObj.name, function () {
runSnippet(testConfig, snippets, collectionObj.name);
});
return callback(null);
});
}
else {
console.log('\nSkipping newman test for: ' + collectionObj.name);
return callback(null);
}
}, function (error) {
if (error) {
expect.fail(null, null, error);
}
});
}
};