-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (140 loc) · 4.96 KB
/
index.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
"use strict";
const path = require('path');
const chai = require('chai');
const expect = chai.expect;
const schemes = {
'http': require('http'),
'https' : require('https')
};
var debug = process.env.SRT_DEBUG || false;
var macros = {};
const macroRegex = /\${[^}]*}(\.[A-Za-z_][0-9A-Za-z_]*)?/g;
const envMacroRegex = /\${env}(\.([A-Za-z_][0-9A-Za-z_]*))/g;
/* Add a user-defined macro. The macro parameter should not have ${}, or it will be saved as ${${...}}. */
exports.addMacro = (macro, substitution) =>
{
if (!macro.match(macroRegex))
macro = '${' + macro + '}';
macros[macro] = substitution;
};
/* Save results of query for future tests to use in a ${A}.b macro */
function saveMacros(prefix, responseStr, macroDef)
{
const macroPrefix = '${' + prefix + '}.';
const responseObject = JSON.parse(responseStr);
Object.keys(responseObject).forEach(function(key){
const index = macroPrefix + key;
macros[index] = responseObject[key];
});
if (macroDef !== null) {
for (const m in macroDef) {
if (macroDef[m].definitionPhase != 'postResponse')
continue;
const f = eval(macroDef[m].definition);
macros[macroPrefix + macroDef[m].name] = f(responseObject);
}
}
}
/* Perform ${A}.b, ${A}, and ${env}.b macro substitution */
function substituteMacros(str)
{
const envSubstitutions = str.match(envMacroRegex);
if (envSubstitutions != null) {
for (const value of envSubstitutions) {
const index = value.split('.')[1];
if (!process.env.hasOwnProperty(index))
console.log('Warning: ${env} macro ' + value + ' is not defined');
else
str = str.replace(value, process.env[index]);
}
}
const substitutions = str.match(macroRegex);
if (substitutions == null)
return str;
for (const value of substitutions) {
// Use of groups in rexexp means we need to make sure we are only substituting full macro values, not the optional qualifier');
if (value.charAt(0) == '$' && !value.match(envMacroRegex)) {
if (!macros.hasOwnProperty(value))
console.log('Warning: macro ' + value + ' is not defined');
else
str = str.replace(value, macros[value]);
}
}
return str;
}
/* Do testing for given request description object via Mocha.js */
exports.mocha = (reqDescr) =>
{
const scheme = schemes[reqDescr.scheme];
const sleepPromise = (resolve) => { setTimeout(resolve, 500); };
describe(reqDescr.description, function(){
this.timeout(reqDescr.timeout || 2000);
let req;
let statusCode;
let responseStr = '';
beforeEach(function(){
if (reqDescr.hasOwnProperty('macroDef')) {
for (const m in reqDescr.macroDef) {
if (reqDescr.macroDef[m].definitionPhase != 'preRequest')
continue;
const f = eval(reqDescr.macroDef[m].definition);
macros['${' + reqDescr.macroDef[m].name + '}'] = f(reqDescr);
}
}
reqDescr = exports.applyMacros(reqDescr);
if (debug) {
console.log('scheme: ' + reqDescr.scheme);
console.log('host: ' + reqDescr.options.host);
console.log('port: ' + reqDescr.options.port);
console.log('path: ' + reqDescr.options.path);
if (reqDescr.options.hasOwnProperty('headers')) {
console.log('Headers');
Object.keys(reqDescr.options.headers).forEach(function(key) {
console.log('\t' + key + ': ' + reqDescr.options.headers[key]);
});
}
}
/* Out-of-the-box, node.js does not support DELETE with body, but we want to. */
const deleteWithBody = reqDescr.options.method.toUpperCase() === 'DELETE' && reqDescr.hasOwnProperty('payload');
if (deleteWithBody)
reqDescr.options.headers['Content-Length'] = Buffer.byteLength(JSON.stringify(reqDescr.payload));
req = scheme.request(reqDescr.options, function(response) {
responseStr = '';
response.on('data', function(chunk){
responseStr += chunk;
});
response.on('end', function(chunk) {
if (reqDescr.saveResponse && response.statusCode == 200)
saveMacros(reqDescr.testname, responseStr, reqDescr.hasOwnProperty('macroDef') ? reqDescr.macroDef : null);
statusCode = response.statusCode;
});
});
});
it(reqDescr.expectation, async function(){
if (reqDescr.hasOwnProperty('payload')) {
if (debug)
console.log('payload: ' + JSON.stringify(reqDescr.payload));
req.write(JSON.stringify(reqDescr.payload));
}
req.end();
while (statusCode === undefined)
await new Promise(sleepPromise);
if (debug)
console.log(`status: ${statusCode} ${statusCode != reqDescr.status ? ' (expected ' + reqDescr.status + ')' : ''}\nresponse: '${responseStr}'`);
expect(statusCode).to.equal(parseInt(reqDescr.status));
if ('responseRegexp' in reqDescr)
expect(responseStr).to.match(new RegExp(reqDescr.returnRegEx));
});
});
};
/* Apply all macro substitutions in the test object */
exports.applyMacros = (reqDescr) =>
{
for (const m of Object.keys(reqDescr)) {
if (typeof reqDescr[m] === 'string')
reqDescr[m] = substituteMacros(reqDescr[m]);
else if (typeof reqDescr[m] === 'object')
reqDescr[m] = exports.applyMacros(reqDescr[m]);
}
return reqDescr;
};