-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommonMethods.js
75 lines (71 loc) · 2.91 KB
/
commonMethods.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
const assert = require('assert');
const { options } = require('./extractArgs');
function postTC(OPTS, vars, methods, test, noti, err, resp) {
const mainResp = err || resp;
noti('RESPONSE', mainResp);
extracting(OPTS, test.extractors, vars, methods, mainResp);
asserting(OPTS, test.assertions, vars, methods, mainResp);
if (typeof test.asserts === 'object' && test.asserts !== null) {
let asar = Object.keys(test.asserts);
let ln = asar.length;
for (let z = 0; z < ln; z++) {
asserting(OPTS, test.asserts[asar[z]], vars, methods, OPTS.replace(asar[z], vars, methods));
}
}
options.vars = vars;
};
exports.getReqObj = function(that, OPTS, test, fileData, done, noti) {
const vars = fileData[1].vars;
const methods = fileData[2];
if (typeof test.vars === 'object' && test.vars !== null) {
Object.keys(test.vars).forEach((ky) => {
const nk = OPTS.replace(ky, vars, methods);
vars[nk] = OPTS.replace(test.vars[ky], vars, methods);
});
}
const input = that.ARshouldClone ? JSON.parse(JSON.stringify(test.request)) : test.request;
const beforeFunction = OPTS.replace(test.before, vars, methods);
if (typeof beforeFunction === 'function') beforeFunction.call(that, vars, methods, null, null, OPTS, test, done);
return {
vars,
methods,
reqObj: OPTS.replace(input, vars, methods) || {},
callback: function callback(err, resp) {
postTC(OPTS, vars, methods, test, noti, err, resp);
const afterFunction = OPTS.replace(test.after, vars, methods);
if (typeof afterFunction === 'function') afterFunction.call(that, vars, methods, err, resp, OPTS, test, done);
done();
}
};
};
function asserting(OPTS, block, vars, methods, source) {
if (typeof block === 'object' && block !== null) {
let asar = Object.keys(block);
let ln = asar.length;
for (let z = 0, key, jpath, exp; z < ln; z++) {
key = asar[z];
jpath = OPTS.replace(key, vars, methods);
exp = OPTS.replace(block[key], vars, methods);
if (typeof exp === 'object' && !Array.isArray(exp) && exp !== null
&& (typeof exp.assertFunction === 'function' || (typeof exp.assertMethod === 'string' && exp.assertMethod.length))) {
if (typeof exp.assertFunction === 'function') {
exp.assertFunction(OPTS.jsonquery(source, jpath), exp.assertExpectedValue, source, exp, vars, methods);
} else {
assert[exp.assertMethod](OPTS.jsonquery(source, jpath), exp.assertExpectedValue);
}
} else {
assert.deepEqual(OPTS.jsonquery(source, jpath), exp);
}
}
}
}
function extracting(OPTS, block, vars, methods, source) {
if (typeof block === 'object' && block !== null) {
let asar = Object.keys(block);
let ln = asar.length;
for (let z = 0, key, jpath, exp; z < ln; z++) {
key = asar[z];
vars[OPTS.replace(key, vars, methods)] = OPTS.jsonquery(source, OPTS.replace(block[key], vars, methods));
}
}
}