-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·176 lines (159 loc) · 6.74 KB
/
main.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
'use strict';
const chalk = require('chalk');
const request = require('request');
const pretty = require('js-object-pretty-print').pretty;
const getTests = require('./getTests.js');
const waterfall = require('promise.waterfall');
const validate = require('./validate');
const { getValue } = require('./utils');
const fs = require('fs');
const path = require('path');
const getHtml = require('./getHtml');
const summary = [];
const jsonArray = [];
module.exports = (tests, options) => new Promise((resolve, reject) => {
getTests(tests, options)
.then(({ verbose, outfile, tests }) => {
waterfall(
tests.map(({ name, test }) => {
return () => new Promise((resolve, reject) => {
test()
.then(({ description, input = {}, output: expectedOutput, uri, method, options }) => {
Object.entries({ output: expectedOutput, uri, method }).forEach(([key, value]) => {
if (!value) throw new Error(`Missing '${key}' value in test params.`);
});
const json = outfile ? {
name,
description,
input,
expectedOutput,
uri,
method,
options
} : {};
const { headers, body } = input;
const requestParams = {
uri,
method,
headers
};
if (headers) {
const headerValue = getValue(headers, 'content-type');
if (typeof headerValue === 'string' && headerValue.toLowerCase() === 'application/json') requestParams.json = body;
}
if (!requestParams.json && body) {
requestParams.body = body;
}
const start = new Date();
request(requestParams, (err, res, body) => {
Object.assign(json, {
executionTime: new Date() - start
});
if (err) return reject({ name, err });
const result = {
status: res.statusCode,
headers: res.headers
};
try {
result.body = JSON.parse(body);
} catch(ignore) {
result.body = body;
}
const { status: statusErrors, headers: headersErrors, body: bodyErrors } = validate(expectedOutput, result, options);
const errorMessage = [];
const statusErrorsL = statusErrors.length;
const headersErrorsL = headersErrors.length;
const bodyErrorsL = bodyErrors.length;
if (statusErrorsL) errorMessage.push(`${chalk.red('STATUS ERRORS:')}\n${statusErrors.join('\n')}`);
if (headersErrorsL) errorMessage.push(`${chalk.red('HEADERS ERRORS:')}\n${headersErrors.join('\n')}`);
if (bodyErrorsL) errorMessage.push(`${chalk.red('BODY ERRORS:')}\n${bodyErrors.join('\n')}`);
console.log(('TEST:'), name);
if (verbose) {
if (description) {
console.log(chalk.white('DESCRIPTION:'));
console.log(description);
}
console.log(chalk.white('URI:'), uri, '\n');
console.log(chalk.white('METHOD:'), method, '\n');
console.log(chalk.white('INPUT HEADERS:'), input.headers, '\n');
console.log(chalk.white('INPUT BODY:'), requestParams.json? pretty(input.body) :input.body, '\n');
}
if (verbose || bodyErrorsL || headersErrorsL || statusErrorsL) {
console.log(chalk.white('EXPECTED:'));
console.log(pretty(expectedOutput));
console.log(chalk.white('RESULT:'));
console.log(pretty(result));
}
if (errorMessage.length) {
if (outfile) {
Object.assign(json, {
errorMessage: errorMessage.map(message =>
message.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g
, '')).join('\n')
});
}
console.log(errorMessage.join('\n'));
}
let testStatus;
if (statusErrorsL || headersErrorsL || bodyErrorsL) {
testStatus = chalk.red('\u2718', name);
summary.push(chalk.red(`\u2718 Failure: ${name}`));
if (outfile) {
Object.assign(json, {
passed: false,
result
});
jsonArray.push(json);
}
} else if (!statusErrorsL && !headersErrorsL && !bodyErrorsL) {
testStatus = chalk.green('\u2714', name);
summary.push(chalk.green(`\u2714 Succeeded: ${name}`));
if (outfile) {
Object.assign(json, {
passed: true,
result
});
jsonArray.push(json);
}
}
console.log(testStatus, '\n');
resolve();
});
})
.catch(err => {
if (name) console.log(chalk.red(`*** ${name} ***`));
console.log(err);
resolve();
});
});
})
)
.then(() => {
if (outfile) {
console.log('ciao');
const jsonPath = path.join(process.cwd(), outfile);
fs.writeFileSync(jsonPath, JSON.stringify(jsonArray, null, 2), 'utf8');
const htmlOut = path.basename(jsonPath, '.json') + '.html';
fs.writeFileSync(path.join(process.cwd(), htmlOut), getHtml(jsonPath), 'utf8');
}
if (tests.length <= 1) return resolve();
console.log(chalk.white('SUMMARY:'));
summary.forEach((data) => console.log(data));
resolve();
})
.catch((err) => {
if (err.err) {
console.log(chalk.red(`*** CONFIGURATION ERRORS IN TEST ${err.name} ***`));
return reject(err.err);
}
reject(err);
});
})
.catch((err) => {
if (err.err) {
console.log(chalk.red(`*** ERRORS IN TEST ${err.name} ***`));
return reject(err.err);
}
reject(err);
});
}).catch((err) => { console.log(err); });