ftmocks-utils 1.0.0 → 1.0.1

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 CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "ftmocks-utils",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Util functions for FtMocks",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
7
- "test": "node test.js"
7
+ "test": "node tests/test.js"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
package/src/index.js CHANGED
@@ -1,5 +1,76 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+
4
+ export const nameToFolder = name => {
5
+ return name.replaceAll(' ', '_');
6
+ };
7
+
8
+ const getDefaultMockDataFromConfig = (testConfig) => {
9
+ const defaultPath = path.join(testConfig.MOCK_DIR, testConfig.MOCK_DEFAULT_FILE);
10
+
11
+ try {
12
+ const defaultData = fs.readFileSync(defaultPath, 'utf8');
13
+ let parsedData = JSON.parse(defaultData);
14
+
15
+ // Read and attach mock data for each entry in parsedData
16
+ parsedData.forEach(entry => {
17
+ const mockFilePath = path.join(testConfig.MOCK_DIR, testConfig.MOCK_DEFAULT_DIR, `mock_${entry.id}.json`);;
18
+ try {
19
+ const mockData = fs.readFileSync(mockFilePath, 'utf8');
20
+ entry.fileContent = JSON.parse(mockData);
21
+ } catch (error) {
22
+ console.error(`Error reading mock data for ${entry.path}:`, error);
23
+ return entry; // Return the original entry if there's an error
24
+ }
25
+ });
26
+ return parsedData;
27
+ } catch (error) {
28
+ console.error(`Error reading or parsing ${testConfig.MOCK_DEFAULT_FILE}:`, error);
29
+ return [];
30
+ }
31
+ }
32
+
1
33
  // src/index.js
34
+ const loadMockDataFromConfig = (testConfig, _testName) => {
35
+ try {
36
+ let testName = _testName;
37
+ if(!testName) {
38
+ // Read the test ID from mockServer.config.json
39
+ const configPath = path.join(testConfig.MOCK_DIR, 'mockServer.config.json');
40
+ const configData = fs.readFileSync(configPath, 'utf8');
41
+ const config = JSON.parse(configData);
42
+ testName = config.testName;
43
+ }
44
+ // Read the tests from testConfig.MOCK_TEST_FILE
45
+ const mocksPath = path.join(testConfig.MOCK_DIR, `test_${nameToFolder(testName)}`, '_mock_list.json');
46
+ const mocksData = fs.readFileSync(mocksPath, 'utf8');
47
+ const mocks = JSON.parse(mocksData);
2
48
 
49
+ mocks.forEach(mock => {
50
+ const fileContent = JSON.parse(fs.readFileSync(path.join(testConfig.MOCK_DIR, `test_${nameToFolder(testName)}`, `mock_${mock.id}.json`), 'utf8'));
51
+ mock.fileContent = fileContent;
52
+ });
53
+
54
+ return mocks;
55
+ } catch (error) {
56
+ console.error('Error loading test data:', error.message);
57
+ return [];
58
+ }
59
+ };
60
+
61
+ const isSameRequest = (req1, req2) => {
62
+ let matched = true;
63
+ if(req1.url !== req2.url) {
64
+ matched = false;
65
+ } else if(req1.method !== req2.method) {
66
+ matched = false;
67
+ } else if((!req1.postData && req2.postData) || (req1.postData && !req2.postData)) {
68
+ matched = areJsonEqual(req1.postData || {} , req2.postData || {});
69
+ } else if(req1.postData && req2.postData && !areJsonEqual(req1.postData , req2.postData)) {
70
+ matched = false;
71
+ }
72
+ return matched;
73
+ }
3
74
 
4
75
  const processURL = (url, ignoreParams=[]) => {
5
76
  // Remove the hostname from the URL
@@ -27,8 +98,25 @@ function compareMockToRequest(mock, req) {
27
98
  });
28
99
  }
29
100
 
101
+ function compareMockToFetchRequest(mock, fetchReq) {
102
+ const mockURL = processURL(mock.fileContent.url, mock.fileContent.ignoreParams);
103
+ const reqURL = processURL(fetchReq.url, mock.fileContent.ignoreParams);
104
+ const postData = mock.fileContent.request?.postData?.text ? JSON.parse(mock.fileContent.request?.postData?.text) : mock.fileContent.request?.postData;
105
+ return isSameRequest({url: mockURL, method: mock.fileContent.method, postData}, {
106
+ method: fetchReq.options.method || 'GET',
107
+ postData: fetchReq.options.body,
108
+ url: reqURL,
109
+ });
110
+ }
111
+
112
+
30
113
  // Export functions as a module
31
114
  module.exports = {
32
115
  compareMockToRequest,
33
- processURL
116
+ processURL,
117
+ isSameRequest,
118
+ loadMockDataFromConfig,
119
+ getDefaultMockDataFromConfig,
120
+ nameToFolder,
121
+ compareMockToFetchRequest
34
122
  };
File without changes