-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.js
405 lines (364 loc) · 11.8 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
'use strict';
const interceptor = require('./lib/interceptor');
const PKG_PREFIX = '[wdio-intercept-service]: ';
class InterceptServiceError extends Error {
constructor(message, ...args) {
super(PKG_PREFIX + message, ...args);
}
}
const issueDeprecation = (map, key, what) => {
if (!map[key]) {
console.warn(
`${PKG_PREFIX}${what} is deprecated and will no longer work in v5`,
);
map[key] = true;
}
};
class WebdriverAjax {
constructor() {
this._wdajaxExpectations = null;
this._deprecations = {};
}
beforeTest() {
this._wdajaxExpectations = [];
}
beforeScenario() {
this._wdajaxExpectations = [];
}
before() {
// WebdriverIO before v6.10 does not pass in the browser instance
/** @type {import('webdriverio').BrowserBase} */
const browser =
typeof arguments[2] === 'undefined' ? globalThis.browser : arguments[2];
/**
* instance need to have addCommand method
*/
if (!browser || typeof browser.addCommand !== 'function') {
throw new Error(
"you can't use WebdriverAjax with this version of WebdriverIO",
);
}
browser.addCommand('setupInterceptor', setup.bind(this));
browser.addCommand('disableInterceptor', disableInterceptor.bind(this));
browser.addCommand('excludeUrls', excludeUrls.bind(this));
browser.addCommand('getExpectations', getExpectations.bind(this));
browser.addCommand('resetExpectations', resetExpectations.bind(this));
browser.addCommand('expectRequest', expectRequest.bind(this));
browser.addCommand('assertRequests', assertRequests.bind(this));
browser.addCommand(
'assertExpectedRequestsOnly',
assertExpectedRequestsOnly.bind(this),
);
browser.addCommand('hasPendingRequests', hasPendingRequests);
browser.addCommand('getRequest', getRequest);
browser.addCommand('getRequests', getRequests);
function disableInterceptor() {
return browser.executeAsync(interceptor.disableInterceptor);
}
function excludeUrls(urls) {
urls = urls.map(function (regex) {
return typeof regex === 'object'
? { source: regex.source, flags: regex.flags }
: { source: regex, flags: undefined };
});
return browser.executeAsync(interceptor.excludeUrls, urls);
}
function setup() {
return browser.executeAsync(interceptor.setup);
}
function expectRequest(method, url, statusCode) {
this._wdajaxExpectations.push({
method: method.toUpperCase(),
url: url,
statusCode: statusCode,
});
return browser;
}
function assertRequests(options = {}) {
const expectations = this._wdajaxExpectations;
if (!expectations.length) {
return Promise.reject(
new Error('No expectations found. Call .expectRequest() first'),
);
}
// Don't let users request pending requests:
if (options.includePending) {
throw new InterceptServiceError(
'passing `includePending` option to `assertRequests` is not supported!',
);
}
return getRequests(options).then((requests) => {
if (expectations.length !== requests.length) {
return Promise.reject(
new Error(
'Expected ' +
expectations.length +
' requests but was ' +
requests.length,
),
);
}
for (let i = 0; i < expectations.length; i++) {
const ex = expectations[i];
const request = requests[i];
if (request.method !== ex.method) {
return Promise.reject(
new Error(
'Expected request to URL ' +
request.url +
' to have method ' +
ex.method +
' but was ' +
request.method,
),
);
}
if (
ex.url instanceof RegExp &&
request.url &&
!request.url.match(ex.url)
) {
return Promise.reject(
new Error(
'Expected request ' +
i +
' to match ' +
ex.url.toString() +
' but was ' +
request.url,
),
);
}
if (typeof ex.url == 'string' && request.url !== ex.url) {
return Promise.reject(
new Error(
'Expected request ' +
i +
' to have URL ' +
ex.url +
' but was ' +
request.url,
),
);
}
if (request.response.statusCode !== ex.statusCode) {
return Promise.reject(
new Error(
'Expected request to URL ' +
request.url +
' to have status ' +
ex.statusCode +
' but was ' +
request.response.statusCode,
),
);
}
}
return browser;
});
}
function assertExpectedRequestsOnly(orderOrOptions) {
const expectations = this._wdajaxExpectations;
let inOrder = true;
let options = {};
if (typeof orderOrOptions === 'boolean') {
issueDeprecation(
this._deprecations,
'inOrder',
'Calling `assertExpectedRequestsOnly` with a boolean parameter',
);
inOrder = orderOrOptions;
} else if (orderOrOptions && typeof orderOrOptions === 'object') {
options = orderOrOptions;
inOrder = 'inOrder' in orderOrOptions ? orderOrOptions.inOrder : true;
delete options.inOrder;
}
// Don't let users request pending requests:
if (options.includePending) {
throw new InterceptServiceError(
'passing `includePending` option to `assertExpectedRequestsOnly` is not supported!',
);
}
return getRequests(options).then((requests) => {
const clonedRequests = requests.slice();
const matchedRequestIndexes = [];
for (let i = 0; i < expectations.length; i++) {
const ex = expectations[i];
const matchingRequestIndex = clonedRequests.findIndex((request) => {
if (
!request ||
request.method !== ex.method ||
(ex.url instanceof RegExp &&
request.url &&
!request.url.match(ex.url)) ||
(typeof ex.url == 'string' && request.url !== ex.url) ||
request.response.statusCode !== ex.statusCode
) {
return false;
}
return true;
});
if (matchingRequestIndex !== -1) {
matchedRequestIndexes.push(matchingRequestIndex);
delete clonedRequests[matchingRequestIndex];
} else {
return Promise.reject(
new Error(
'Expected request was not found. ' +
'method: ' +
ex.method +
' url: ' +
ex.url +
' statusCode: ' +
ex.statusCode,
),
);
}
}
if (matchedRequestIndexes.length !== expectations.length) {
return Promise.reject(
new Error(
'Expected ' +
expectations.length +
' requests but found ' +
matchedRequestIndexes.length +
' matching requests',
),
);
} else if (
inOrder &&
JSON.stringify(matchedRequestIndexes) !==
JSON.stringify(matchedRequestIndexes.slice().sort())
) {
return Promise.reject(
new Error('Requests not received in the expected order'),
);
}
return browser;
});
}
// In a long test, it's possible you might want to reset the list
// of expected requests after validating some.
function resetExpectations() {
this._wdajaxExpectations = [];
return browser;
}
function getExpectations() {
return this._wdajaxExpectations;
}
function getRequests(options = {}) {
return getRequest(undefined, options);
}
async function getRequest(index, options = {}) {
const hasIndex = typeof index === 'number';
if (typeof index !== 'undefined' && !hasIndex) {
throw new TypeError(
`${PKG_PREFIX}the "index" property must be a non-negative integer`,
);
}
if (hasIndex && index < 0) {
throw new RangeError(
`${PKG_PREFIX}the "index" property must be a non-negative integer`,
);
}
const request = await browser.execute(
interceptor.getRequest,
hasIndex ? index : undefined,
options,
);
if (!request) {
if (hasIndex) {
throw new Error('Could not find request with index ' + index);
}
return [];
}
if (Array.isArray(request)) {
return request.map(transformRequest);
}
// The edge driver does not seem to typecast arrays correctly
if (typeof request[0] == 'object') {
return mapIndexed(request, transformRequest);
}
return transformRequest(request);
}
function hasPendingRequests() {
return browser.execute(interceptor.hasPending);
}
function transformRequest(req) {
if (!req) {
return;
}
const transformed = {
url: req.url,
method: req.method && req.method.toUpperCase(),
headers: normalizeRequestHeaders(req.requestHeaders),
body: parseBody(req.requestBody),
pending: true,
};
// Check for a '__fulfilled' property on the retrieved request, which is
// set by the interceptor only when the response completes. Before this
// flag is set, the request is still being processed (e.g. a large response
// body is downloading) and therefore is pending.
if (req.__fulfilled) {
transformed.pending = false;
transformed.response = {
headers: parseResponseHeaders(req.headers),
body: parseBody(req.body),
statusCode: req.statusCode,
};
if (!req.headers) {
console.warn(
`${transformed.method} request to ${req.url} (HTTP ${req.statusCode}) had no response headers!`,
);
}
}
return transformed;
}
function normalizeRequestHeaders(headers) {
const normalized = {};
Object.keys(headers).forEach((key) => {
normalized[key.toLowerCase()] = headers[key];
});
return normalized;
}
// parses raw header to key-value objects
// (best effort compliance with RFC)
function parseResponseHeaders(rawHeader) {
const headers = {};
if (!rawHeader) {
return headers;
}
const lines = rawHeader.trim().split(/(?:\r?\n)+/);
for (const line of lines) {
const parts = line.split(/(?<=^[^:]*):/);
const key = parts[0].trim().toLowerCase();
const value = parts[1].trim();
if (typeof headers[key] == 'undefined') {
headers[key] = value;
} else if (typeof headers[key] == 'string') {
headers[key] = headers[key] + ', ' + value;
}
}
return headers;
}
function parseBody(str) {
let body;
try {
body = JSON.parse(str);
} catch (e) {
body = str;
}
return body;
}
// maps an 'array-like' object. returns proper array
function mapIndexed(obj, fn) {
const arr = [];
const max = Math.max.apply(Math, Object.keys(obj).map(Number));
for (let i = 0; i <= max; i++) {
arr.push(fn(obj[i], i));
}
return arr;
}
}
}
exports.default = WebdriverAjax;