ftmocks-utils 1.1.3 → 1.1.4
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 +23 -5
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -21,6 +21,18 @@ 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
|
+
};
|
|
24
36
|
|
|
25
37
|
const areJsonEqual = (jsonObj1, jsonObj2) => {
|
|
26
38
|
// Check if both are objects and not null
|
|
@@ -206,7 +218,7 @@ async function resetAllMockStats({testMockData, testConfig, testName}) {
|
|
|
206
218
|
}
|
|
207
219
|
}
|
|
208
220
|
|
|
209
|
-
async function initiatePlaywrightRoutes (page, ftmocksConifg, testName, mockPath = '**/*') {
|
|
221
|
+
async function initiatePlaywrightRoutes (page, ftmocksConifg, testName, mockPath = '**/*', excludeMockPath = null) {
|
|
210
222
|
const testMockData = testName ? loadMockDataFromConfig(ftmocksConifg, testName) : [];
|
|
211
223
|
resetAllMockStats({testMockData, testConfig: ftmocksConifg, testName});
|
|
212
224
|
const defaultMockData = getDefaultMockDataFromConfig(ftmocksConifg);
|
|
@@ -220,14 +232,19 @@ async function initiatePlaywrightRoutes (page, ftmocksConifg, testName, mockPath
|
|
|
220
232
|
body: request.postData(),
|
|
221
233
|
}
|
|
222
234
|
}
|
|
235
|
+
if (excludeMockPath && new RegExp(excludeMockPath).test(url)) {
|
|
236
|
+
await route.fallback();
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
223
239
|
console.debug('got fetch request', request.method(), request.url(), request.postData());
|
|
224
240
|
let mockData = getMatchingMockData({testMockData, defaultMockData, url, options, testConfig: ftmocksConifg, testName});
|
|
225
241
|
if (mockData) {
|
|
226
242
|
console.debug('mocked', url, options);
|
|
227
243
|
const { content, headers, status } = mockData.response;
|
|
244
|
+
|
|
228
245
|
const json = {
|
|
229
246
|
status,
|
|
230
|
-
headers: new Map(Object.entries(headers)),
|
|
247
|
+
headers: new Map(Object.entries(capitalizeHeaders(headers))),
|
|
231
248
|
body: content,
|
|
232
249
|
};
|
|
233
250
|
|
|
@@ -280,7 +297,7 @@ async function initiateJestFetch (jest, ftmocksConifg, testName) {
|
|
|
280
297
|
console.debug('missing mock data', url, options);
|
|
281
298
|
return Promise.resolve({
|
|
282
299
|
status: 404,
|
|
283
|
-
headers: new Map([['
|
|
300
|
+
headers: new Map([['Content-Type', 'application/json']]),
|
|
284
301
|
json: () => Promise.resolve({ error: 'Mock data not found' }),
|
|
285
302
|
});
|
|
286
303
|
}
|
|
@@ -289,7 +306,7 @@ async function initiateJestFetch (jest, ftmocksConifg, testName) {
|
|
|
289
306
|
|
|
290
307
|
return Promise.resolve({
|
|
291
308
|
status,
|
|
292
|
-
headers: new Map(Object.entries(headers)),
|
|
309
|
+
headers: new Map(Object.entries(capitalizeHeaders(headers))),
|
|
293
310
|
json: () => Promise.resolve(JSON.parse(content)),
|
|
294
311
|
});
|
|
295
312
|
});
|
|
@@ -310,6 +327,7 @@ async function initiateJestFetch (jest, ftmocksConifg, testName) {
|
|
|
310
327
|
status: 0,
|
|
311
328
|
response: null,
|
|
312
329
|
responseText: '',
|
|
330
|
+
headers: new Map(Object.entries(capitalizeHeaders(headers))),
|
|
313
331
|
onreadystatechange: null,
|
|
314
332
|
onload: null,
|
|
315
333
|
onerror: null,
|
|
@@ -332,7 +350,7 @@ async function initiateJestFetch (jest, ftmocksConifg, testName) {
|
|
|
332
350
|
xhrMock.status = status;
|
|
333
351
|
xhrMock.responseText = content;
|
|
334
352
|
xhrMock.response = content;
|
|
335
|
-
xhrMock.headers = headers;
|
|
353
|
+
xhrMock.headers = new Map(Object.entries(capitalizeHeaders(headers)));
|
|
336
354
|
|
|
337
355
|
if (xhrMock.onreadystatechange) {
|
|
338
356
|
xhrMock.onreadystatechange();
|