-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset-up-mocked-resource.js
251 lines (226 loc) · 7.11 KB
/
set-up-mocked-resource.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
var express = require('express')
var fs = require('fs');
var path = require('path');
var _ = require('underscore');
var checkJSONSchema = require("./check-json-schema.js");
var imageTypes = [".png", ".jpeg", ".jpg", ".gif"];
module.exports = {
setUpMockedResource: function(dirPath, mockDirectory, app, resourcesExposed, reportingDirectory, httpCodes) {
// declare and get resource variables
var resourcePathWithAPIVersion;
var methodName;
getResourceParameters();
// get mocked content
var mockedContent = getMockedContent();
if(mockedContent) {
mockedContent = JSON.parse(mockedContent);
}
var mockedPayloadContent = getMockedPayloadContent();
if(mockedPayloadContent) {
mockedPayloadContent = JSON.parse(mockedPayloadContent);
}
var options = getOptions();
// get readme
var readmeContent = getReadme();
// get schemas
var schemas = {mock:"",payload:""};
var mockedSchema;
var payloadSchema;
getSchemas();
// get images
getImages();
// get http codes
var resourceHttpCodes = getResourceHttpCodes();
if (resourceHttpCodes) {
var matchedHttpCodes = getHttpCodes(resourceHttpCodes);
}
// expose resources
exposeWS();
console.log("setting up " + (isJSONValid ? 'valid' : 'invalid') + " resource on path : " + resourcePathWithAPIVersion + ", method : " + methodName + ", version " + versionApi);
// fill array with mocks
resourcesExposed.push({
resourcePath: "/" + versionApi + resourcePath,
content: mockedContent,
contentPayload : mockedPayloadContent,
readme: readmeContent,
method: methodName,
version: versionApi,
valid: isJSONValid,
validPayload : isJSONPayloadValid,
images: imageFiles,
schemas: schemas,
options: options,
httpCodes : matchedHttpCodes
});
return resourcesExposed;
function getResourceParameters() {
// remove base path
// resourcePath = 1.0\articles\GET
if (path.sep === '\\') {
resourcePath = dirPath.replace(mockDirectory + "\\", '');
} else {
resourcePath = dirPath.replace(mockDirectory + path.sep, '');
}
// get first param : 1.0, 1.1 ...
// pathArray = ["1.0", "articles", "GET"]
if (path.sep === '\\') {
pathArray = resourcePath.split( '\\' );
} else {
pathArray = resourcePath.split( path.sep );
}
versionApi = pathArray[0];
// replace '\' with '/'
// resourcePath = 1.0/articles/GET
fullResourcePath = resourcePath.replace(new RegExp('\\' + path.sep, 'g'), '/');
// remove API version from path
// resourcePath = articles/GET
resourcePath = fullResourcePath.replace(versionApi + '/', '');
// get directory name : articles/user ...
// dirName = articles
dirName = path.dirname(resourcePath);
// get method name : GET, POST...
methodName = path.basename(dirPath);
// resourcePath = /articles/GET
resourcePath = "/" + dirName;
// resourcePathWithAPIVersion = /1.0/articles
resourcePathWithAPIVersion = "/" + versionApi + resourcePath;
}
function getMockedContent(){
try {
return mockedContent = fs.readFileSync(path.join(dirPath, "mock.json"), "utf8");
//catch exceptions
} catch (e) {
}
}
function getMockedPayloadContent(){
try {
return mockedPayloadContent = fs.readFileSync(path.join(dirPath, "mock-payload.json"), "utf8");
//catch exceptions
} catch (e) {
}
}
function getOptions(){
try {
return options = JSON.parse(fs.readFileSync(path.join(dirPath, "config.json"), "utf8"));
//catch exceptions
} catch (e) {
// config.json not found
}
}
function getReadme(){
// try to fetch README.md
try {
return readmeContent = fs.readFileSync(path.join(dirPath, "README.md"), "utf8");
//catch exceptions
} catch (e) {
// README.md not found
}
}
function getSchemas() {
try {
mockedSchema = fs.readFileSync(path.join(dirPath, "schema-mock.json"), "utf8");
//catch exceptions
} catch (e) {
// schema-mock.json not found
}
try {
payloadSchema = fs.readFileSync(path.join(dirPath, "schema-payload.json"), "utf8");
//catch exceptions
} catch (e) {
// schema-payload.json not found
}
if (mockedSchema) {
mockedSchema = JSON.parse(mockedSchema);
schemas.mock = mockedSchema;
}
if (payloadSchema) {
payloadSchema = JSON.parse(payloadSchema);
schemas.payload = payloadSchema;
}
if (mockedContent && mockedSchema) {
isJSONValid = checkJSONSchema.checkJSONSchema(mockedContent, mockedSchema).valid;
}
if (mockedPayloadContent && payloadSchema) {
isJSONPayloadValid = checkJSONSchema.checkJSONSchema(mockedPayloadContent, payloadSchema).valid;
}
}
function exposeMockedSchema(mockedSchema) {
// expose our WS, using the appropriate method
app.get("/schema-mock"+resourcePathWithAPIVersion, function(req, res) {
// send response
res.send(mockedSchema);
});
}
function exposePayloadSchema(payloadSchema) {
// expose our WS, using the appropriate method
console.log("schemas payload exposed at : " + "/schema-payload"+resourcePathWithAPIVersion);
app.get("/schema-payload"+resourcePathWithAPIVersion, function(req, res) {
// send response
res.send(payloadSchema);
});
}
function getImages() {
// get images in folder
imageFiles = [];
var listImageFiles = fs.readdirSync(dirPath);
for (var file in listImageFiles) {
if (_.contains(imageTypes, path.extname(listImageFiles[file]))) {
imageFiles.push(path.join("/", "reporting", "mocks", fullResourcePath, listImageFiles[file]));
}
}
if (imageFiles) {
app.use('/reporting/mocks/' + fullResourcePath, express.static(path.join(mockDirectory, fullResourcePath)));
}
}
function getResourceHttpCodes() {
// try to fetch http-codes.json
try {
return JSON.parse(fs.readFileSync(path.join(dirPath, "http-codes.json"), "utf8"));
//catch exceptions
} catch (e) {
// README.md not found
}
}
function getHttpCodes(resourceHttpCodes) {
return _.map(resourceHttpCodes, function(httpCode){
return _.findWhere(httpCodes, {code: httpCode});
})
}
function exposeWS() {
if (mockedSchema) {
exposeMockedSchema(mockedSchema);
}
if (payloadSchema) {
exposePayloadSchema(payloadSchema);
}
//Add optional url parameters
var paramString = "";
var params = options.parametres;
if (params){
params.forEach(function(param){
var space = new RegExp(' ', 'g');
paramString += "/:" + param.nom.replace(space, "");
});
}
resourcePathWithAPIVersion += paramString;
// expose our WS, using the appropriate method
app[methodName.toLowerCase()](resourcePathWithAPIVersion, function(req, res) {
// send response
res.send(mockedContent);
if (methodName == "POST") {
// compare POST payload to mock JSON schema
console.log(resourcePathWithAPIVersion, " payload body : ",req.body);
if (payloadSchema) {
if (checkJSONSchema.checkJSONSchema(req.body, payloadSchema).valid) {
console.log("Valid payload !");
}
else {
console.log("Invalid payload !\n");
console.log(checkJSONSchema.checkJSONSchema(req.body, payloadSchema).errorLog);
}
}
}
});
}
}
};