-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration.js
140 lines (117 loc) · 4.87 KB
/
integration.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
const assert = require('assert');
const axios = require('axios');
const extend = require('deep-extend');
const http = require('http');
const ms = require('ms');
const extensions = {
beforeFns: {},
eachFns: {},
props: [],
afterFns: {},
};
module.exports = function (opts) {
return async function () {
if (!opts || !opts.app) throw new Error('Missing `app` from opts');
if (!opts.req) throw new Error('Missing `req` from opts');
if (!opts.res) throw new Error('Missing `res` from opts');
// If this test needs to adjust it's timeout
if (opts.timeout) this.timeout(ms(opts.timeout)); // eslint-disable-line no-invalid-this
const req = extend({ responseType: 'json' }, opts.defaults || {}, opts.req);
// Handle generic before() functions
if (typeof opts.before === 'function') await opts.before({
req, res: null,
opts: Object.assign({}, opts, { before: null, after: null }),
});
// Handle "before" items if the relevant property is found
for (const prop in extensions.beforeFns) {
if (opts.hasOwnProperty(prop) && typeof extensions.beforeFns[prop] === 'function') {
await extensions.beforeFns[prop].call(null, opts[prop], req, opts); // eslint-disable-line no-await-in-loop
}
}
const server = await startServer(opts.app);
const res = await axios.request(Object.assign(req, {
baseURL: `http://127.0.0.1:${server.address().port}/`,
maxRedirects: 0,
validateStatus: null,
}));
server.close();
// Handle "each" items on every request
for (const prop in extensions.eachFns) {
/* istanbul ignore else */
if (typeof extensions.eachFns[prop] === 'function') {
await extensions.eachFns[prop].call(null, req, res, opts); // eslint-disable-line no-await-in-loop
}
}
// Handle "after" items if the relevant property is found
for (const prop in extensions.afterFns) {
if (opts.hasOwnProperty(prop) && typeof extensions.afterFns[prop] === 'function') {
await extensions.afterFns[prop].call(null, opts[prop], req, opts); // eslint-disable-line no-await-in-loop
}
}
if (opts.res.status) assert.equal(res.status, opts.res.status, 'Incorrect response status');
if (opts.res.headers) assertResHeaders(res.headers, opts.res.headers);
if (opts.res.data) {
if (req.responseType === 'json') assert.deepEqual(res.data, opts.res.data, 'Incorrect response JSON');
else assert.equal(res.data, opts.res.data, 'Incorrect response body');
}
// Handle generic after() functions
if (typeof opts.after === 'function') await opts.after({
req, res,
opts: Object.assign({}, opts, { before: null, after: null }),
});
};
};
function startServer(app) {
const server = http.createServer(app);
return new Promise((resolve, reject) => {
/* istanbul ignore next */
const timer = setTimeout(() => onceError(new Error('Failed to start server')), 1000);
/* istanbul ignore next */
function onceError(err) {
clearTimeout(timer);
server.removeListener('listening', onceListening);
reject(err);
}
function onceListening() {
clearTimeout(timer);
server.removeListener('error', onceError);
resolve(server);
}
server.once('error', onceError);
server.once('listening', onceListening);
server.listen(0, '127.0.0.1');
});
}
function assertResHeaders(actual, expected) {
/**
* Pulled from https://github.com/visionmedia/supertest/blob/master/lib/test.js
* Superagent does some of the best assertions I've ever seen
*/
Object.keys(expected).forEach(function eachResHeader(key) {
const actualValue = actual[key.toLowerCase()];
if (typeof actualValue === 'undefined') throw new Error(`Expected "${key.toLowerCase()}" header`);
const expectedValue = expected[key];
// This check handles header values that may be a String or single element Array
if ((actualValue instanceof Array && actualValue.toString() === expectedValue) || expectedValue === actualValue) {
return;
}
if (expectedValue instanceof RegExp) {
if (!expectedValue.test(actualValue)) {
throw new Error(`Expected "${key}" header matching ${expectedValue.toString()}, got "${actualValue}"`);
}
} else {
throw new Error(`Expected "${key}" header equal to "${expectedValue}", got "${actualValue}"`);
}
});
}
module.exports.with = function (prop, { before, after, each }) {
if (!prop || [ 'app', 'req', 'res' ].indexOf(prop) >= 0) throw new Error('Invalid property name');
if (extensions.props.indexOf(prop) >= 0) throw new Error(`Property "${prop}" has already been registered`);
extensions.props.push(prop);
if (typeof each === 'function') {
extensions.eachFns[prop] = each;
} else {
if (typeof before === 'function') extensions.beforeFns[prop] = before;
if (typeof after === 'function') extensions.afterFns[prop] = after;
}
};