-
Notifications
You must be signed in to change notification settings - Fork 19
/
swagger-test.js
executable file
·81 lines (72 loc) · 2.17 KB
/
swagger-test.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
#!/usr/bin/env node
var preq = require('preq');
var assert = require('assert');
var readline = require('readline');
var swaggerTest = require('./lib/swagger-test.js');
var jsonDiff = require('json-diff');
var source = [];
var rl = readline.createInterface({
terminal: false,
input: process.stdin,
});
rl.on('line', function (line) {
source.push(line);
});
rl.on('close', function () {
var passed = [];
var failed = [];
var requests = [];
var swaggerSpec = JSON.parse(source.join('\n'));
var xamples = swaggerTest.parse(swaggerSpec);
xamples.forEach(function (xample) {
requests.push(
preq[xample.request.method](xample.request).then(function (response) {
if (response.status && xample.responses[response.status]) {
var xampleResponse = xample.responses[response.status];
xampleResponse.status = response.status;
if (response.headers && xampleResponse.headers) {
for (var h in response.headers) {
if (response.headers.hasOwnProperty(h)) {
if (!(h in xampleResponse.headers)) {
delete response.headers[h];
}
}
}
}
try {
assert.deepEqual(response, xampleResponse);
passed.push({
description: xample.description,
});
} catch (e) {
failed.push({
description: xample.description,
expected: e.expected,
actual: e.actual,
});
}
}
})
);
});
Promise.all(requests).then(function () {
console.log(passed.length.toString(), 'tests passed:');
passed.forEach(function (x) {
console.log('\n -', x.description);
});
console.log();
console.error(failed.length.toString(), 'tests failed:');
failed.forEach(function (x) {
console.error('\n -', x.description);
console.error();
var diff = jsonDiff.diff(x.actual, x.expected);
console.error(JSON.stringify(diff, null, 2));
});
if (failed.length > 0) {
process.exit(1);
}
}).catch(function (r) {
console.error(r);
process.exit(1);
});
});