ftmocks-utils 1.1.2 → 1.1.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +38 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ftmocks-utils",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "Util functions for FtMocks",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -14,6 +14,14 @@ const getMockDir = config => {
14
14
  return config.MOCK_DIR;
15
15
  }
16
16
 
17
+ const getFallbackDir = config => {
18
+ if(config.FALLBACK_DIR && !path.isAbsolute(config.FALLBACK_DIR)) {
19
+ return path.resolve( process.cwd(), config.FALLBACK_DIR);
20
+ }
21
+ return config.FALLBACK_DIR;
22
+ }
23
+
24
+
17
25
  const areJsonEqual = (jsonObj1, jsonObj2) => {
18
26
  // Check if both are objects and not null
19
27
  if (typeof jsonObj1 === 'object' && jsonObj1 !== null &&
@@ -198,12 +206,12 @@ async function resetAllMockStats({testMockData, testConfig, testName}) {
198
206
  }
199
207
  }
200
208
 
201
- async function initiatePlaywrightRoutes (page, ftmocksConifg, testName, path = '**/*') {
209
+ async function initiatePlaywrightRoutes (page, ftmocksConifg, testName, mockPath = '**/*') {
202
210
  const testMockData = testName ? loadMockDataFromConfig(ftmocksConifg, testName) : [];
203
211
  resetAllMockStats({testMockData, testConfig: ftmocksConifg, testName});
204
212
  const defaultMockData = getDefaultMockDataFromConfig(ftmocksConifg);
205
213
  console.debug('calling initiatePlaywrightRoutes fetch');
206
- await page.route(path, async (route, request) => {
214
+ await page.route(mockPath, async (route, request) => {
207
215
  const url = request.url();
208
216
  const options = {
209
217
  options: {
@@ -226,7 +234,34 @@ async function initiatePlaywrightRoutes (page, ftmocksConifg, testName, path = '
226
234
  await route.fulfill(json);
227
235
  } else {
228
236
  console.debug('missing mock data', url, options);
229
- await route.fallback();
237
+ const fallbackDir = getFallbackDir(ftmocksConifg);
238
+ if(!fallbackDir) {
239
+ await route.fallback();
240
+ return;
241
+ }
242
+ const urlObj = new URL(route.request().url());
243
+ const filePath = path.join(fallbackDir, urlObj.pathname === '/' || urlObj.pathname === '' ? (ftmocksConifg.FALLBACK_DIR_INDEX_FILE || 'index.html') : urlObj.pathname);
244
+ console.debug('serving file ', filePath);
245
+ if (fs.existsSync(filePath) && fs.lstatSync(filePath).isFile()) {
246
+ const fileContent = fs.readFileSync(filePath);
247
+ const ext = path.extname(filePath);
248
+ const contentType = {
249
+ '.html': 'text/html',
250
+ '.css': 'text/css',
251
+ '.js': 'application/javascript',
252
+ '.json': 'application/json',
253
+ '.png': 'image/png',
254
+ '.jpg': 'image/jpeg',
255
+ }[ext] || 'application/octet-stream';
256
+
257
+ console.debug('serving file', filePath);
258
+ await route.fulfill({
259
+ body: fileContent,
260
+ headers: { 'Content-Type': contentType },
261
+ });
262
+ } else {
263
+ await route.fallback();
264
+ }
230
265
  }
231
266
  });
232
267
  }