@centeredgesoft/runtime-mock-routes 2.7.0 → 2.8.0
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/build/src/appV2.js +1 -1
- package/package.json +1 -1
- package/src/appV2.test.ts +53 -2
- package/src/appV2.ts +1 -1
package/build/src/appV2.js
CHANGED
|
@@ -47,7 +47,7 @@ const isSupportedMethod = (obj) => exports.SupportedMethodsColection.includes(ob
|
|
|
47
47
|
exports.isSupportedMethod = isSupportedMethod;
|
|
48
48
|
const isRuntimeRequestMethodBody = (obj) => {
|
|
49
49
|
try {
|
|
50
|
-
return !!obj.body && (!obj.status || !isNaN(Number(obj.status)));
|
|
50
|
+
return !!obj.body && (!obj.status || typeof obj.status === 'function' || !isNaN(Number(obj.status)));
|
|
51
51
|
}
|
|
52
52
|
catch (_) {
|
|
53
53
|
return false;
|
package/package.json
CHANGED
package/src/appV2.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import request from 'supertest';
|
|
2
|
-
import { appFactory, isRuntimeRequestCollection, RuntimeRequestBody, RuntimeRequestCollection, SupportedMethodsColection, RequestParameters } from './appV2';
|
|
2
|
+
import { appFactory, isRuntimeRequestCollection, RuntimeRequestBody, RuntimeRequestCollection, SupportedMethodsColection, RequestParameters, RuntimeRequestMethodBody, isRuntimeRequestMethodBody } from './appV2';
|
|
3
3
|
|
|
4
4
|
describe('appFactory', () => {
|
|
5
5
|
test('Creates app with no seed', () => {
|
|
@@ -318,4 +318,55 @@ describe('isRuntimeRequestMethodBodyCollection', () => {
|
|
|
318
318
|
const valid = isRuntimeRequestCollection(seed);
|
|
319
319
|
expect(valid).toBeTruthy();
|
|
320
320
|
})
|
|
321
|
-
})
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
describe('Status as a function', () => {
|
|
324
|
+
test('is true for status that is function', () => {
|
|
325
|
+
const seed: RuntimeRequestMethodBody = {
|
|
326
|
+
body: "test",
|
|
327
|
+
status: () => {
|
|
328
|
+
return 200;
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
const valid = isRuntimeRequestMethodBody(seed);
|
|
333
|
+
expect(valid).toBeTruthy();
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
test('Executes body and status functions', async () => {
|
|
337
|
+
const seed: RuntimeRequestCollection = {
|
|
338
|
+
"/test": {
|
|
339
|
+
path: "/test",
|
|
340
|
+
methods: {
|
|
341
|
+
GET: {
|
|
342
|
+
body: function(rp: RequestParameters) {
|
|
343
|
+
if(rp.query.id == "2") {
|
|
344
|
+
return {id: 2};
|
|
345
|
+
} else {
|
|
346
|
+
return {id: 3};
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
status: function(rp: RequestParameters) {
|
|
350
|
+
if(rp.query.id == "2") {
|
|
351
|
+
return 200;
|
|
352
|
+
} else {
|
|
353
|
+
return 400;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const app = appFactory(seed);
|
|
362
|
+
expect(app).toBeDefined();
|
|
363
|
+
|
|
364
|
+
var response = await request(app).get('/test?id=2');
|
|
365
|
+
expect(response.body).toEqual({id: 2});
|
|
366
|
+
expect(response.status).toEqual(200);
|
|
367
|
+
|
|
368
|
+
var secondResponse = await request(app).get('/test?id=3');
|
|
369
|
+
expect(secondResponse.body).toEqual({id: 3});
|
|
370
|
+
expect(secondResponse.status).toEqual(400);
|
|
371
|
+
})
|
|
372
|
+
})
|
package/src/appV2.ts
CHANGED
|
@@ -48,7 +48,7 @@ export interface RequestParameters {
|
|
|
48
48
|
|
|
49
49
|
export const isRuntimeRequestMethodBody = (obj: any): obj is RuntimeRequestMethodBody => {
|
|
50
50
|
try {
|
|
51
|
-
return !!obj.body && (!obj.status || !isNaN(Number(obj.status)))
|
|
51
|
+
return !!obj.body && (!obj.status || typeof obj.status === 'function' || !isNaN(Number(obj.status)))
|
|
52
52
|
} catch (_) {
|
|
53
53
|
return false;
|
|
54
54
|
}
|