ftmocks-utils 1.2.1 → 1.2.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +153 -68
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ftmocks-utils",
3
- "version": "1.2.1",
3
+ "version": "1.2.4",
4
4
  "description": "Util functions for FtMocks",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -338,19 +338,6 @@ function getMatchingMockData({
338
338
  let foundMock = matchedMocks.find((mock) => !mock.fileContent.served)
339
339
  ? matchedMocks.find((mock) => !mock.fileContent.served)
340
340
  : matchedMocks[matchedMocks.length - 1];
341
- // updating stats to mock file
342
- if (foundMock) {
343
- const mockFilePath = path.join(
344
- getMockDir(testConfig),
345
- `test_${nameToFolder(testName)}`,
346
- `mock_${foundMock.id}.json`
347
- );
348
- foundMock.fileContent.served = true;
349
- fs.writeFileSync(
350
- mockFilePath,
351
- JSON.stringify(foundMock.fileContent, null, 2)
352
- );
353
- }
354
341
 
355
342
  if (!foundMock) {
356
343
  foundMock = defaultMockData.find((tm) =>
@@ -392,6 +379,19 @@ function getMatchingMockData({
392
379
  }
393
380
  }
394
381
  }
382
+ // updating stats to mock file
383
+ if (foundMock) {
384
+ const mockFilePath = path.join(
385
+ getMockDir(testConfig),
386
+ `test_${nameToFolder(testName)}`,
387
+ `mock_${foundMock.id}.json`
388
+ );
389
+ foundMock.fileContent.served = true;
390
+ fs.writeFileSync(
391
+ mockFilePath,
392
+ JSON.stringify(foundMock.fileContent, null, 2)
393
+ );
394
+ }
395
395
  return foundMock ? foundMock.fileContent : null;
396
396
  }
397
397
 
@@ -424,9 +424,13 @@ async function initiatePlaywrightRoutes(
424
424
  resetAllMockStats({ testMockData, testConfig: ftmocksConifg, testName });
425
425
  const test = await getTestByName(ftmocksConifg, testName);
426
426
  const defaultMockData = getDefaultMockDataFromConfig(ftmocksConifg);
427
- console.debug("calling initiatePlaywrightRoutes fetch");
427
+ console.debug("\x1b[32mcalling initiatePlaywrightRoutes fetch\x1b[0m");
428
+ let firstUrl = null;
428
429
  await page.route(mockPath, async (route, request) => {
429
430
  const url = request.url();
431
+ if (!firstUrl) {
432
+ firstUrl = url;
433
+ }
430
434
  const options = {
431
435
  url,
432
436
  method: request.method(),
@@ -436,12 +440,6 @@ async function initiatePlaywrightRoutes(
436
440
  await route.fallback();
437
441
  return;
438
442
  }
439
- console.debug(
440
- "got fetch request",
441
- request.method(),
442
- request.url(),
443
- request.postData()
444
- );
445
443
  let mockData = getMatchingMockData({
446
444
  testMockData,
447
445
  defaultMockData,
@@ -451,53 +449,140 @@ async function initiatePlaywrightRoutes(
451
449
  testName,
452
450
  mode: test.mode || "loose",
453
451
  });
454
- if (mockData) {
455
- console.debug("mocked", url, options);
456
- const { content, headers, status } = mockData.response;
452
+ try {
453
+ if (mockData) {
454
+ const { content, headers, status, file } = mockData.response;
457
455
 
458
- const json = {
459
- status,
460
- headers: getHeaders(headers),
461
- body: content,
462
- };
456
+ const json = {
457
+ status,
458
+ headers: getHeaders(headers),
459
+ body: content,
460
+ };
463
461
 
464
- await route.fulfill(json);
465
- } else {
466
- console.debug("missing mock data", url, options);
467
- const fallbackDir = getFallbackDir(ftmocksConifg);
468
- if (!fallbackDir) {
469
- await route.fallback();
470
- return;
471
- }
472
- const urlObj = new URL(route.request().url());
473
- const filePath = path.join(
474
- fallbackDir,
475
- urlObj.pathname === "/" || urlObj.pathname === ""
476
- ? ftmocksConifg.FALLBACK_DIR_INDEX_FILE || "index.html"
477
- : urlObj.pathname
478
- );
479
- console.debug("serving file ", filePath);
480
- if (fs.existsSync(filePath) && fs.lstatSync(filePath).isFile()) {
481
- const fileContent = fs.readFileSync(filePath);
482
- const ext = path.extname(filePath);
483
- const contentType =
484
- {
485
- ".html": "text/html",
486
- ".css": "text/css",
487
- ".js": "application/javascript",
488
- ".json": "application/json",
489
- ".png": "image/png",
490
- ".jpg": "image/jpeg",
491
- }[ext] || "application/octet-stream";
492
-
493
- console.debug("serving file", filePath);
494
- await route.fulfill({
495
- body: fileContent,
496
- headers: { "Content-Type": contentType },
497
- });
462
+ if (file) {
463
+ const filePath = path.join(
464
+ getMockDir(ftmocksConifg),
465
+ `test_${nameToFolder(testName)}`,
466
+ "_files",
467
+ file
468
+ );
469
+ if (fs.existsSync(filePath) && fs.lstatSync(filePath).isFile()) {
470
+ const fileContent = fs.readFileSync(filePath);
471
+ json.body = fileContent;
472
+
473
+ console.debug(
474
+ "\x1b[32mresponse is a file, serving file\x1b[0m",
475
+ filePath,
476
+ url
477
+ );
478
+ await route.fulfill(json);
479
+ }
480
+ } else {
481
+ await route.fulfill(json);
482
+ }
498
483
  } else {
499
- await route.fallback();
484
+ const fallbackDir = getFallbackDir(ftmocksConifg);
485
+ if (!fallbackDir) {
486
+ await route.fallback();
487
+ return;
488
+ }
489
+ const urlObj = new URL(route.request().url());
490
+ let filePath = path.join(
491
+ fallbackDir,
492
+ urlObj.pathname === "/" || urlObj.pathname === ""
493
+ ? ftmocksConifg.FALLBACK_DIR_INDEX_FILE || "index.html"
494
+ : urlObj.pathname
495
+ );
496
+
497
+ if (
498
+ !fs.existsSync(filePath) &&
499
+ !path.extname(filePath) &&
500
+ url === firstUrl
501
+ ) {
502
+ filePath = path.join(
503
+ fallbackDir,
504
+ ftmocksConifg.FALLBACK_DIR_INDEX_FILE_FOR_STATUS_404 || "index.html"
505
+ );
506
+ console.debug(
507
+ "\x1b[32mserving file for status 404\x1b[0m",
508
+ filePath,
509
+ url
510
+ );
511
+ }
512
+ if (fs.existsSync(filePath) && fs.lstatSync(filePath).isFile()) {
513
+ const fileContent = fs.readFileSync(filePath);
514
+ const ext = path.extname(filePath);
515
+ const contentType =
516
+ {
517
+ ".html": "text/html",
518
+ ".htm": "text/html",
519
+ ".xhtml": "application/xhtml+xml",
520
+ ".css": "text/css",
521
+ ".js": "application/javascript",
522
+ ".json": "application/json",
523
+ ".png": "image/png",
524
+ ".jpg": "image/jpeg",
525
+ ".svg": "image/svg+xml",
526
+ ".ico": "image/x-icon",
527
+ ".webp": "image/webp",
528
+ ".mp4": "video/mp4",
529
+ ".mp3": "audio/mpeg",
530
+ ".wav": "audio/wav",
531
+ ".ogg": "audio/ogg",
532
+ ".pdf": "application/pdf",
533
+ ".doc": "application/msword",
534
+ ".docx":
535
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
536
+ ".xls": "application/vnd.ms-excel",
537
+ ".xlsx":
538
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
539
+ ".ppt": "application/vnd.ms-powerpoint",
540
+ ".pptx":
541
+ "application/vnd.openxmlformats-officedocument.presentationml.presentation",
542
+ ".zip": "application/zip",
543
+ ".rar": "application/x-rar-compressed",
544
+ ".7z": "application/x-7z-compressed",
545
+ ".tar": "application/x-tar",
546
+ ".gz": "application/gzip",
547
+ ".bz2": "application/x-bzip2",
548
+ ".xz": "application/x-xz",
549
+ ".exe": "application/x-msdownload",
550
+ ".dll": "application/x-msdownload",
551
+ ".so": "application/x-sharedlib",
552
+ ".dylib": "application/x-dynamiclib",
553
+ ".bin": "application/octet-stream",
554
+ ".txt": "text/plain",
555
+ ".csv": "text/csv",
556
+ ".tsv": "text/tab-separated-values",
557
+ ".xml": "application/xml",
558
+ ".xsl": "application/xml",
559
+ ".xslt": "application/xml",
560
+ ".xlt": "application/xml",
561
+ ".xltx": "application/xml",
562
+ ".xltm": "application/xml",
563
+ ".yaml": "text/yaml",
564
+ ".yml": "text/yaml",
565
+ ".toml": "text/toml",
566
+ ".php": "application/x-httpd-php",
567
+ }[ext] || "application/octet-stream";
568
+
569
+ console.debug("\x1b[32mserving file\x1b[0m", filePath);
570
+ await route.fulfill({
571
+ body: fileContent,
572
+ headers: { "Content-Type": contentType },
573
+ });
574
+ } else {
575
+ console.debug("\x1b[31mmissing mock data, falling back\x1b[0m", url);
576
+ await route.fallback();
577
+ }
500
578
  }
579
+ } catch (e) {
580
+ console.error(e);
581
+ console.error(
582
+ "\x1b[31merror at initiatePlaywrightRoutes\x1b[0m",
583
+ url,
584
+ options
585
+ );
501
586
  }
502
587
  });
503
588
  }
@@ -749,7 +834,7 @@ const getTestByName = async (ftmocksConifg, testName) => {
749
834
  const etest = tests.find((tst) => tst.name === testName);
750
835
  return etest;
751
836
  } catch (error) {
752
- console.error(`Error reading tests.json:`, error);
837
+ console.error(`\x1b[31mError reading tests.json:\x1b[0m`, error);
753
838
  return null;
754
839
  }
755
840
  };
@@ -776,13 +861,13 @@ const createTest = async (ftmocksConifg, testName) => {
776
861
  const mockListFilePath = path.join(folderPath, "_mock_list.json");
777
862
  fs.mkdir(folderPath, { recursive: true }, (err) => {
778
863
  if (err) {
779
- console.error("Error creating directory:", err);
864
+ console.error("\x1b[31mError creating directory:\x1b[0m", err);
780
865
  } else {
781
- console.log("Directory created successfully!");
866
+ console.log("\x1b[32mDirectory created successfully!\x1b[0m");
782
867
  }
783
868
  });
784
869
  await fs.appendFile(mockListFilePath, "[]", () => {
785
- console.log("mock list file created successfully");
870
+ console.log("\x1b[32mmock list file created successfully\x1b[0m");
786
871
  });
787
872
 
788
873
  return newTest;
@@ -790,7 +875,7 @@ const createTest = async (ftmocksConifg, testName) => {
790
875
  throw "Test already exists";
791
876
  }
792
877
  } catch (error) {
793
- console.error(`Error reading tests.json:`, error);
878
+ console.error(`\x1b[31mError reading tests.json:\x1b[0m`, error);
794
879
  return null;
795
880
  }
796
881
  };