ftmocks-utils 1.1.3 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +34 -5
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -21,6 +21,29 @@ const getFallbackDir = config => {
|
|
|
21
21
|
return config.FALLBACK_DIR;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
const capitalizeHeader = header => {
|
|
25
|
+
return header
|
|
26
|
+
.split('-')
|
|
27
|
+
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
|
|
28
|
+
.join('-');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const capitalizeHeaders = headers => {
|
|
32
|
+
return Object.fromEntries(
|
|
33
|
+
Object.entries(headers).map(([key, value]) => [capitalizeHeader(key), value])
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const getHeaders = headers => {
|
|
38
|
+
let res = null;
|
|
39
|
+
try {
|
|
40
|
+
res = new Map([...Object.entries(headers), ...Object.entries(capitalizeHeaders(headers))]);
|
|
41
|
+
} catch(e) {
|
|
42
|
+
console.debug('error at getHeaders', e);
|
|
43
|
+
res = new Map([['Content-Type', 'application/json'], ['content-type', 'application/json']]);
|
|
44
|
+
}
|
|
45
|
+
return Object.fromEntries(res);
|
|
46
|
+
}
|
|
24
47
|
|
|
25
48
|
const areJsonEqual = (jsonObj1, jsonObj2) => {
|
|
26
49
|
// Check if both are objects and not null
|
|
@@ -206,7 +229,7 @@ async function resetAllMockStats({testMockData, testConfig, testName}) {
|
|
|
206
229
|
}
|
|
207
230
|
}
|
|
208
231
|
|
|
209
|
-
async function initiatePlaywrightRoutes (page, ftmocksConifg, testName, mockPath = '**/*') {
|
|
232
|
+
async function initiatePlaywrightRoutes (page, ftmocksConifg, testName, mockPath = '**/*', excludeMockPath = null) {
|
|
210
233
|
const testMockData = testName ? loadMockDataFromConfig(ftmocksConifg, testName) : [];
|
|
211
234
|
resetAllMockStats({testMockData, testConfig: ftmocksConifg, testName});
|
|
212
235
|
const defaultMockData = getDefaultMockDataFromConfig(ftmocksConifg);
|
|
@@ -220,14 +243,19 @@ async function initiatePlaywrightRoutes (page, ftmocksConifg, testName, mockPath
|
|
|
220
243
|
body: request.postData(),
|
|
221
244
|
}
|
|
222
245
|
}
|
|
246
|
+
if (excludeMockPath && new RegExp(excludeMockPath).test(url)) {
|
|
247
|
+
await route.fallback();
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
223
250
|
console.debug('got fetch request', request.method(), request.url(), request.postData());
|
|
224
251
|
let mockData = getMatchingMockData({testMockData, defaultMockData, url, options, testConfig: ftmocksConifg, testName});
|
|
225
252
|
if (mockData) {
|
|
226
253
|
console.debug('mocked', url, options);
|
|
227
254
|
const { content, headers, status } = mockData.response;
|
|
255
|
+
|
|
228
256
|
const json = {
|
|
229
257
|
status,
|
|
230
|
-
headers:
|
|
258
|
+
headers: getHeaders(headers),
|
|
231
259
|
body: content,
|
|
232
260
|
};
|
|
233
261
|
|
|
@@ -280,7 +308,7 @@ async function initiateJestFetch (jest, ftmocksConifg, testName) {
|
|
|
280
308
|
console.debug('missing mock data', url, options);
|
|
281
309
|
return Promise.resolve({
|
|
282
310
|
status: 404,
|
|
283
|
-
headers: new Map([['
|
|
311
|
+
headers: new Map([['Content-Type', 'application/json']]),
|
|
284
312
|
json: () => Promise.resolve({ error: 'Mock data not found' }),
|
|
285
313
|
});
|
|
286
314
|
}
|
|
@@ -289,7 +317,7 @@ async function initiateJestFetch (jest, ftmocksConifg, testName) {
|
|
|
289
317
|
|
|
290
318
|
return Promise.resolve({
|
|
291
319
|
status,
|
|
292
|
-
headers:
|
|
320
|
+
headers: getHeaders(headers),
|
|
293
321
|
json: () => Promise.resolve(JSON.parse(content)),
|
|
294
322
|
});
|
|
295
323
|
});
|
|
@@ -310,6 +338,7 @@ async function initiateJestFetch (jest, ftmocksConifg, testName) {
|
|
|
310
338
|
status: 0,
|
|
311
339
|
response: null,
|
|
312
340
|
responseText: '',
|
|
341
|
+
headers: getHeaders(headers),
|
|
313
342
|
onreadystatechange: null,
|
|
314
343
|
onload: null,
|
|
315
344
|
onerror: null,
|
|
@@ -332,7 +361,7 @@ async function initiateJestFetch (jest, ftmocksConifg, testName) {
|
|
|
332
361
|
xhrMock.status = status;
|
|
333
362
|
xhrMock.responseText = content;
|
|
334
363
|
xhrMock.response = content;
|
|
335
|
-
xhrMock.headers = headers;
|
|
364
|
+
xhrMock.headers = getHeaders(headers);
|
|
336
365
|
|
|
337
366
|
if (xhrMock.onreadystatechange) {
|
|
338
367
|
xhrMock.onreadystatechange();
|