forked from ctimmerm/axios-mock-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (51 loc) · 1.57 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
var verbs = ['get', 'post', 'head', 'delete', 'patch', 'put'];
function findHandler(matchers, method, url) {
return matchers[method].find(function(matcher) {
if (typeof matcher[0] === 'string') {
return url === matcher[0];
} else if (matcher[0] instanceof RegExp) {
return matcher[0].test(url);
}
});
}
function adapter() {
return function(resolve, reject, config) {
var handler = findHandler(this.matchers, config.method, config.url);
if (handler) {
var response = (handler[1] instanceof Function)
? handler[1](config)
: handler.slice(1);
((response[0] === 1223) || (response[0] >= 200 && response[0] < 300 ) ? resolve : reject)
({
status: response[0],
data: response[1],
headers: response[2],
config: config
});
} else {
reject({ status: 404, config: config });
}
}.bind(this);
}
function MockAdapter(axiosInstance) {
this.matchers = verbs.reduce(function(previousValue, currentValue) {
previousValue[currentValue] = [];
return previousValue;
}, {});
if (axiosInstance) {
axiosInstance.defaults.adapter = adapter.call(this);
}
}
MockAdapter.prototype.adapter = adapter;
verbs.forEach(function(method) {
var methodName = 'on' + method.charAt(0).toUpperCase() + method.slice(1);
MockAdapter.prototype[methodName] = function(matcher) {
var _this = this;
return {
reply: function reply(code, response, headers) {
_this.matchers[method].push([matcher, code, response, headers]);
}
};
};
});
module.exports = MockAdapter;