-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathindex.js
222 lines (194 loc) · 5.62 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"use strict";
var handleRequest = require("./handle_request");
var utils = require("./utils");
var VERBS = [
"get",
"post",
"head",
"delete",
"patch",
"put",
"options",
"list",
"link",
"unlink",
];
function adapter() {
return function (config) {
var mockAdapter = this;
return new Promise(function (resolve, reject) {
handleRequest(mockAdapter, resolve, reject, config);
});
}.bind(this);
}
function getVerbObject() {
return VERBS.reduce(function (accumulator, verb) {
accumulator[verb] = [];
return accumulator;
}, {});
}
function reset() {
resetHandlers.call(this);
resetHistory.call(this);
}
function resetHandlers() {
this.handlers = getVerbObject();
}
function resetHistory() {
this.history = getVerbObject();
}
function MockAdapter(axiosInstance, options) {
reset.call(this);
if (axiosInstance) {
this.axiosInstance = axiosInstance;
// Clone the axios instance to remove interceptors
// this is used for the passThrough mode with axios > 1.2
this.axiosInstanceWithoutInterceptors = axiosInstance.create
? axiosInstance.create()
: undefined;
this.originalAdapter = axiosInstance.defaults.adapter;
this.delayResponse =
options && options.delayResponse > 0 ? options.delayResponse : null;
this.onNoMatch = (options && options.onNoMatch) || null;
axiosInstance.defaults.adapter = this.adapter.call(this);
} else {
throw new Error("Please provide an instance of axios to mock");
}
}
MockAdapter.prototype.adapter = adapter;
MockAdapter.prototype.restore = function restore() {
if (this.axiosInstance) {
this.axiosInstance.defaults.adapter = this.originalAdapter;
this.axiosInstance = undefined;
}
};
MockAdapter.prototype.reset = reset;
MockAdapter.prototype.resetHandlers = resetHandlers;
MockAdapter.prototype.resetHistory = resetHistory;
VERBS.concat("any").forEach(function (method) {
var methodName = "on" + method.charAt(0).toUpperCase() + method.slice(1);
MockAdapter.prototype[methodName] = function (matcher, body, requestHeaders) {
var _this = this;
var matcher = matcher === undefined ? /.*/ : matcher;
function reply(code, response, headers) {
var handler = [matcher, body, requestHeaders, code, response, headers];
addHandler(method, _this.handlers, handler);
return _this;
}
function replyOnce(code, response, headers) {
var handler = [
matcher,
body,
requestHeaders,
code,
response,
headers,
true,
];
addHandler(method, _this.handlers, handler);
return _this;
}
return {
reply: reply,
replyOnce: replyOnce,
passThrough: function passThrough() {
var handler = [matcher, body];
addHandler(method, _this.handlers, handler);
return _this;
},
abortRequest: function () {
return reply(function (config) {
var error = utils.createAxiosError(
"Request aborted",
config,
undefined,
"ECONNABORTED"
);
return Promise.reject(error);
});
},
abortRequestOnce: function () {
return replyOnce(function (config) {
var error = utils.createAxiosError(
"Request aborted",
config,
undefined,
"ECONNABORTED"
);
return Promise.reject(error);
});
},
networkError: function () {
return reply(function (config) {
var error = utils.createAxiosError("Network Error", config);
return Promise.reject(error);
});
},
networkErrorOnce: function () {
return replyOnce(function (config) {
var error = utils.createAxiosError("Network Error", config);
return Promise.reject(error);
});
},
timeout: function () {
return reply(function (config) {
var error = utils.createAxiosError(
config.timeoutErrorMessage ||
"timeout of " + config.timeout + "ms exceeded",
config,
undefined,
"ECONNABORTED"
);
return Promise.reject(error);
});
},
timeoutOnce: function () {
return replyOnce(function (config) {
var error = utils.createAxiosError(
config.timeoutErrorMessage ||
"timeout of " + config.timeout + "ms exceeded",
config,
undefined,
"ECONNABORTED"
);
return Promise.reject(error);
});
},
};
};
});
function findInHandlers(method, handlers, handler) {
var index = -1;
for (var i = 0; i < handlers[method].length; i += 1) {
var item = handlers[method][i];
var isReplyOnce = item.length === 7;
var comparePaths =
item[0] instanceof RegExp && handler[0] instanceof RegExp
? String(item[0]) === String(handler[0])
: item[0] === handler[0];
var isSame =
comparePaths &&
utils.isEqual(item[1], handler[1]) &&
utils.isEqual(item[2], handler[2]);
if (isSame && !isReplyOnce) {
index = i;
}
}
return index;
}
function addHandler(method, handlers, handler) {
if (method === "any") {
VERBS.forEach(function (verb) {
handlers[verb].push(handler);
});
} else {
var indexOfExistingHandler = findInHandlers(method, handlers, handler);
if (indexOfExistingHandler > -1 && handler.length < 7) {
handlers[method].splice(indexOfExistingHandler, 1, handler);
} else {
handlers[method].push(handler);
}
}
}
module.exports = MockAdapter;
module.exports.default = MockAdapter;