mock-config-server 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/README.md +213 -0
- package/dist/bin/mock-config-server.d.ts +2 -0
- package/dist/bin/mock-config-server.js +65 -0
- package/dist/bin/resolveExportsFromSourceCode.d.ts +1 -0
- package/dist/bin/resolveExportsFromSourceCode.js +11 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/dist/jest.config.d.ts +3 -0
- package/dist/jest.config.js +7 -0
- package/dist/src/configs/isEntitiesEqual/isEntityValuesEqual.d.ts +2 -0
- package/dist/src/configs/isEntitiesEqual/isEntityValuesEqual.js +29 -0
- package/dist/src/configs/isEntitiesEqual/isEntityValuesEqual.test.d.ts +1 -0
- package/dist/src/configs/isEntitiesEqual/isEntityValuesEqual.test.js +25 -0
- package/dist/src/configs/prepareRequestConfigs/prepareRequestConfigs.d.ts +2 -0
- package/dist/src/configs/prepareRequestConfigs/prepareRequestConfigs.js +30 -0
- package/dist/src/configs/prepareRequestConfigs/prepareRequestConfigs.test.d.ts +1 -0
- package/dist/src/configs/prepareRequestConfigs/prepareRequestConfigs.test.js +151 -0
- package/dist/src/cors/corsMiddleware/corsMiddleware.d.ts +3 -0
- package/dist/src/cors/corsMiddleware/corsMiddleware.js +32 -0
- package/dist/src/cors/corsMiddleware/corsMiddleware.test.d.ts +1 -0
- package/dist/src/cors/corsMiddleware/corsMiddleware.test.js +86 -0
- package/dist/src/cors/getOrigins/getAllowedOrigins.d.ts +2 -0
- package/dist/src/cors/getOrigins/getAllowedOrigins.js +16 -0
- package/dist/src/cors/getOrigins/getAllowedOrigins.test.d.ts +1 -0
- package/dist/src/cors/getOrigins/getAllowedOrigins.test.js +24 -0
- package/dist/src/cors/noCorsMiddleware/noCorsMiddleware.d.ts +2 -0
- package/dist/src/cors/noCorsMiddleware/noCorsMiddleware.js +15 -0
- package/dist/src/cors/noCorsMiddleware/noCorsMiddleware.test.d.ts +1 -0
- package/dist/src/cors/noCorsMiddleware/noCorsMiddleware.test.js +22 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.js +19 -0
- package/dist/src/routes/callRequestInterceptors/callRequestInterceptors.d.ts +12 -0
- package/dist/src/routes/callRequestInterceptors/callRequestInterceptors.js +13 -0
- package/dist/src/routes/callRequestInterceptors/callRequestInterceptors.test.d.ts +1 -0
- package/dist/src/routes/callRequestInterceptors/callRequestInterceptors.test.js +28 -0
- package/dist/src/routes/callResponseInterceptors/callResponseInterceptors.d.ts +14 -0
- package/dist/src/routes/callResponseInterceptors/callResponseInterceptors.js +31 -0
- package/dist/src/routes/callResponseInterceptors/callResponseInterceptors.test.d.ts +1 -0
- package/dist/src/routes/callResponseInterceptors/callResponseInterceptors.test.js +36 -0
- package/dist/src/routes/createRoutes/createRoutes.d.ts +3 -0
- package/dist/src/routes/createRoutes/createRoutes.js +43 -0
- package/dist/src/routes/createRoutes/createRoutes.test.d.ts +1 -0
- package/dist/src/routes/createRoutes/createRoutes.test.js +276 -0
- package/dist/src/server/createMockServer/createMockServer.d.ts +3 -0
- package/dist/src/server/createMockServer/createMockServer.js +35 -0
- package/dist/src/server/startMockServer/startMockServer.d.ts +2 -0
- package/dist/src/server/startMockServer/startMockServer.js +13 -0
- package/dist/src/static/staticMiddleware/staticMiddleware.d.ts +3 -0
- package/dist/src/static/staticMiddleware/staticMiddleware.js +29 -0
- package/dist/src/utils/constants/appPath.d.ts +1 -0
- package/dist/src/utils/constants/appPath.js +4 -0
- package/dist/src/utils/constants/default.d.ts +10 -0
- package/dist/src/utils/constants/default.js +13 -0
- package/dist/src/utils/constants/index.d.ts +2 -0
- package/dist/src/utils/constants/index.js +18 -0
- package/dist/src/utils/helpers/index.d.ts +2 -0
- package/dist/src/utils/helpers/index.js +18 -0
- package/dist/src/utils/helpers/isPlainObject.d.ts +1 -0
- package/dist/src/utils/helpers/isPlainObject.js +5 -0
- package/dist/src/utils/helpers/sleep.d.ts +1 -0
- package/dist/src/utils/helpers/sleep.js +7 -0
- package/dist/src/utils/types/configs.d.ts +34 -0
- package/dist/src/utils/types/configs.js +2 -0
- package/dist/src/utils/types/index.d.ts +3 -0
- package/dist/src/utils/types/index.js +19 -0
- package/dist/src/utils/types/interceptors.d.ts +15 -0
- package/dist/src/utils/types/interceptors.js +2 -0
- package/dist/src/utils/types/server.d.ts +24 -0
- package/dist/src/utils/types/server.js +2 -0
- package/package.json +99 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const express_1 = __importDefault(require("express"));
|
|
7
|
+
const supertest_1 = __importDefault(require("supertest"));
|
|
8
|
+
const corsMiddleware_1 = require("./corsMiddleware");
|
|
9
|
+
describe('corsMiddleware', () => {
|
|
10
|
+
test('Should set default cors for request if does not set custom cors settings', async () => {
|
|
11
|
+
const server = (0, express_1.default)();
|
|
12
|
+
const cors = {
|
|
13
|
+
origin: ['https://test.com', 'https://uncorrectDomain.com']
|
|
14
|
+
};
|
|
15
|
+
(0, corsMiddleware_1.corsMiddleware)(server, cors);
|
|
16
|
+
const response = await (0, supertest_1.default)(server).get('/').set({ origin: 'https://test.com' });
|
|
17
|
+
expect(response.headers).toMatchObject({
|
|
18
|
+
'access-control-allow-headers': '*',
|
|
19
|
+
'access-control-allow-methods': '*',
|
|
20
|
+
'access-control-allow-origin': 'https://test.com',
|
|
21
|
+
'access-control-max-age': '3600',
|
|
22
|
+
'access-control-allow-credentials': 'true'
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
test('Should not set cors for request if origin does not match', async () => {
|
|
26
|
+
const server = (0, express_1.default)();
|
|
27
|
+
const cors = {
|
|
28
|
+
origin: 'https://uncorrectDomain.com'
|
|
29
|
+
};
|
|
30
|
+
(0, corsMiddleware_1.corsMiddleware)(server, cors);
|
|
31
|
+
const response = await (0, supertest_1.default)(server).get('/').set({ origin: 'https://test.com' });
|
|
32
|
+
expect(response.headers).not.toHaveProperty('access-control-allow-headers');
|
|
33
|
+
expect(response.headers).not.toHaveProperty('access-control-allow-methods');
|
|
34
|
+
expect(response.headers).not.toHaveProperty('access-control-allow-origin');
|
|
35
|
+
expect(response.headers).not.toHaveProperty('access-control-max-age');
|
|
36
|
+
expect(response.headers).not.toHaveProperty('access-control-allow-credentials');
|
|
37
|
+
});
|
|
38
|
+
test('Should set allow headers to access-control-allow-headers', async () => {
|
|
39
|
+
const server = (0, express_1.default)();
|
|
40
|
+
const cors = {
|
|
41
|
+
origin: 'https://test.com',
|
|
42
|
+
headers: ['header1', 'header2']
|
|
43
|
+
};
|
|
44
|
+
(0, corsMiddleware_1.corsMiddleware)(server, cors);
|
|
45
|
+
const response = await (0, supertest_1.default)(server).get('/').set({ origin: 'https://test.com' });
|
|
46
|
+
expect(response.headers).toMatchObject({
|
|
47
|
+
'access-control-allow-headers': 'header1, header2'
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
test('Should set methods to access-control-allow-methods', async () => {
|
|
51
|
+
const server = (0, express_1.default)();
|
|
52
|
+
const cors = {
|
|
53
|
+
origin: 'https://test.com',
|
|
54
|
+
methods: ['GET', 'POST']
|
|
55
|
+
};
|
|
56
|
+
(0, corsMiddleware_1.corsMiddleware)(server, cors);
|
|
57
|
+
const response = await (0, supertest_1.default)(server).get('/').set({ origin: 'https://test.com' });
|
|
58
|
+
expect(response.headers).toMatchObject({
|
|
59
|
+
'access-control-allow-methods': 'GET, POST'
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
test('Should set methods to access-control-allow-credentials', async () => {
|
|
63
|
+
const server = (0, express_1.default)();
|
|
64
|
+
const cors = {
|
|
65
|
+
origin: 'https://test.com',
|
|
66
|
+
credentials: false
|
|
67
|
+
};
|
|
68
|
+
(0, corsMiddleware_1.corsMiddleware)(server, cors);
|
|
69
|
+
const response = await (0, supertest_1.default)(server).get('/').set({ origin: 'https://test.com' });
|
|
70
|
+
expect(response.headers).toMatchObject({
|
|
71
|
+
'access-control-allow-credentials': 'false'
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
test('Should set max-age to access-control-max-age', async () => {
|
|
75
|
+
const server = (0, express_1.default)();
|
|
76
|
+
const cors = {
|
|
77
|
+
origin: 'https://test.com',
|
|
78
|
+
maxAge: 10000
|
|
79
|
+
};
|
|
80
|
+
(0, corsMiddleware_1.corsMiddleware)(server, cors);
|
|
81
|
+
const response = await (0, supertest_1.default)(server).get('/').set({ origin: 'https://test.com' });
|
|
82
|
+
expect(response.headers).toMatchObject({
|
|
83
|
+
'access-control-max-age': '10000'
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAllowedOrigins = void 0;
|
|
4
|
+
const getAllowedOrigins = async (origin) => {
|
|
5
|
+
if (Array.isArray(origin)) {
|
|
6
|
+
return origin;
|
|
7
|
+
}
|
|
8
|
+
if (typeof origin === 'string' || origin instanceof RegExp) {
|
|
9
|
+
return [origin];
|
|
10
|
+
}
|
|
11
|
+
if (typeof origin === 'function') {
|
|
12
|
+
return (0, exports.getAllowedOrigins)(await origin());
|
|
13
|
+
}
|
|
14
|
+
throw new Error('Invalid cors origin format');
|
|
15
|
+
};
|
|
16
|
+
exports.getAllowedOrigins = getAllowedOrigins;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const getAllowedOrigins_1 = require("./getAllowedOrigins");
|
|
4
|
+
describe('getAllowedOrigins', () => {
|
|
5
|
+
test('Function should return array if get string or RegExp parameter', async () => {
|
|
6
|
+
expect((0, getAllowedOrigins_1.getAllowedOrigins)(/origin/g)).resolves.toEqual([/origin/g]);
|
|
7
|
+
expect((0, getAllowedOrigins_1.getAllowedOrigins)('https://origin.com')).resolves.toEqual(['https://origin.com']);
|
|
8
|
+
});
|
|
9
|
+
test('Function should return array if get array parameter', () => {
|
|
10
|
+
expect((0, getAllowedOrigins_1.getAllowedOrigins)([/origin/g, 'https://origin.com'])).resolves.toEqual([
|
|
11
|
+
/origin/g,
|
|
12
|
+
'https://origin.com'
|
|
13
|
+
]);
|
|
14
|
+
});
|
|
15
|
+
test('Function should return array if get function parameter', () => {
|
|
16
|
+
expect((0, getAllowedOrigins_1.getAllowedOrigins)(() => [/origin/g, 'https://origin.com'])).resolves.toEqual([
|
|
17
|
+
/origin/g,
|
|
18
|
+
'https://origin.com'
|
|
19
|
+
]);
|
|
20
|
+
});
|
|
21
|
+
test('Function should return array if get promise parameter', () => {
|
|
22
|
+
expect((0, getAllowedOrigins_1.getAllowedOrigins)(() => Promise.resolve([/origin/g, 'https://origin.com']))).resolves.toEqual([/origin/g, 'https://origin.com']);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noCorsMiddleware = void 0;
|
|
4
|
+
const constants_1 = require("../../utils/constants");
|
|
5
|
+
const noCorsMiddleware = async (server) => {
|
|
6
|
+
server.use(async (_req, res, next) => {
|
|
7
|
+
res.setHeader('Access-Control-Allow-Origin', constants_1.DEFAULT.CORS.ORIGIN);
|
|
8
|
+
res.setHeader('Access-Control-Allow-Methods', constants_1.DEFAULT.CORS.METHODS);
|
|
9
|
+
res.setHeader('Access-Control-Allow-Headers', constants_1.DEFAULT.CORS.HEADERS);
|
|
10
|
+
res.setHeader('Access-Control-Allow-Credentials', `${constants_1.DEFAULT.CORS.CREDENTIALS}`);
|
|
11
|
+
res.setHeader('Access-Control-Max-Age', constants_1.DEFAULT.CORS.MAX_AGE);
|
|
12
|
+
return next();
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
exports.noCorsMiddleware = noCorsMiddleware;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const express_1 = __importDefault(require("express"));
|
|
7
|
+
const supertest_1 = __importDefault(require("supertest"));
|
|
8
|
+
const noCorsMiddleware_1 = require("./noCorsMiddleware");
|
|
9
|
+
describe('noCorsMiddleware', () => {
|
|
10
|
+
test('Should set no cors settings for request', async () => {
|
|
11
|
+
const server = (0, express_1.default)();
|
|
12
|
+
(0, noCorsMiddleware_1.noCorsMiddleware)(server);
|
|
13
|
+
const response = await (0, supertest_1.default)(server).get('/');
|
|
14
|
+
expect(response.headers).toMatchObject({
|
|
15
|
+
'access-control-allow-headers': '*',
|
|
16
|
+
'access-control-allow-methods': '*',
|
|
17
|
+
'access-control-allow-origin': '*',
|
|
18
|
+
'access-control-max-age': '3600',
|
|
19
|
+
'access-control-allow-credentials': 'true'
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./server/createMockServer/createMockServer"), exports);
|
|
18
|
+
__exportStar(require("./server/startMockServer/startMockServer"), exports);
|
|
19
|
+
__exportStar(require("./utils/types"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Request } from 'express';
|
|
2
|
+
import { InterceptorRequest } from '../../utils/types';
|
|
3
|
+
interface CallRequestInterceptorsParams {
|
|
4
|
+
request: Request;
|
|
5
|
+
interceptors?: {
|
|
6
|
+
routeInterceptor?: InterceptorRequest | undefined;
|
|
7
|
+
requestInterceptor?: InterceptorRequest | undefined;
|
|
8
|
+
serverInterceptor?: InterceptorRequest | undefined;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export declare const callRequestInterceptors: (params: CallRequestInterceptorsParams) => void;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.callRequestInterceptors = void 0;
|
|
4
|
+
const callRequestInterceptors = (params) => {
|
|
5
|
+
const { request, interceptors } = params;
|
|
6
|
+
if (interceptors?.routeInterceptor)
|
|
7
|
+
interceptors.routeInterceptor({ request });
|
|
8
|
+
if (interceptors?.requestInterceptor)
|
|
9
|
+
interceptors.requestInterceptor({ request });
|
|
10
|
+
if (interceptors?.serverInterceptor)
|
|
11
|
+
interceptors.serverInterceptor({ request });
|
|
12
|
+
};
|
|
13
|
+
exports.callRequestInterceptors = callRequestInterceptors;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const callRequestInterceptors_1 = require("./callRequestInterceptors");
|
|
4
|
+
describe('callRequestInterceptors', () => {
|
|
5
|
+
test('Should call all passed request interceptors in order: route -> request -> server', () => {
|
|
6
|
+
const request = {};
|
|
7
|
+
const routeInterceptor = jest.fn();
|
|
8
|
+
const requestInterceptor = jest.fn();
|
|
9
|
+
const serverInterceptor = jest.fn();
|
|
10
|
+
(0, callRequestInterceptors_1.callRequestInterceptors)({ request });
|
|
11
|
+
expect(routeInterceptor.mock.calls.length).toBe(0);
|
|
12
|
+
expect(requestInterceptor.mock.calls.length).toBe(0);
|
|
13
|
+
expect(serverInterceptor.mock.calls.length).toBe(0);
|
|
14
|
+
(0, callRequestInterceptors_1.callRequestInterceptors)({
|
|
15
|
+
request,
|
|
16
|
+
interceptors: {
|
|
17
|
+
routeInterceptor,
|
|
18
|
+
requestInterceptor,
|
|
19
|
+
serverInterceptor
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
expect(routeInterceptor.mock.calls.length).toBe(1);
|
|
23
|
+
expect(requestInterceptor.mock.calls.length).toBe(1);
|
|
24
|
+
expect(serverInterceptor.mock.calls.length).toBe(1);
|
|
25
|
+
expect(routeInterceptor.mock.invocationCallOrder[0]).toBeLessThan(requestInterceptor.mock.invocationCallOrder[0]);
|
|
26
|
+
expect(requestInterceptor.mock.invocationCallOrder[0]).toBeLessThan(serverInterceptor.mock.invocationCallOrder[0]);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Request, Response } from 'express';
|
|
2
|
+
import type { InterceptorResponse } from '../../utils/types';
|
|
3
|
+
interface CallResponseInterceptorsParams<T> {
|
|
4
|
+
data: T;
|
|
5
|
+
request: Request;
|
|
6
|
+
response: Response;
|
|
7
|
+
interceptors?: {
|
|
8
|
+
routeInterceptor?: InterceptorResponse | undefined;
|
|
9
|
+
requestInterceptor?: InterceptorResponse | undefined;
|
|
10
|
+
serverInterceptor?: InterceptorResponse | undefined;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export declare const callResponseInterceptors: <T = any>(params: CallResponseInterceptorsParams<T>) => T;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.callResponseInterceptors = void 0;
|
|
4
|
+
const helpers_1 = require("../../utils/helpers");
|
|
5
|
+
const callResponseInterceptors = (params) => {
|
|
6
|
+
const { data, request, response, interceptors } = params;
|
|
7
|
+
const setDelay = async (delay) => {
|
|
8
|
+
await (0, helpers_1.sleep)(delay === Infinity ? 100000 : delay);
|
|
9
|
+
};
|
|
10
|
+
const setStatusCode = (statusCode) => {
|
|
11
|
+
response.statusCode = statusCode;
|
|
12
|
+
};
|
|
13
|
+
const interceptorResponseParams = {
|
|
14
|
+
request,
|
|
15
|
+
response,
|
|
16
|
+
setDelay,
|
|
17
|
+
setStatusCode
|
|
18
|
+
};
|
|
19
|
+
let updatedData = data;
|
|
20
|
+
if (interceptors?.routeInterceptor) {
|
|
21
|
+
updatedData = interceptors.routeInterceptor(updatedData, interceptorResponseParams);
|
|
22
|
+
}
|
|
23
|
+
if (interceptors?.requestInterceptor) {
|
|
24
|
+
updatedData = interceptors.requestInterceptor(updatedData, interceptorResponseParams);
|
|
25
|
+
}
|
|
26
|
+
if (interceptors?.serverInterceptor) {
|
|
27
|
+
updatedData = interceptors.serverInterceptor(updatedData, interceptorResponseParams);
|
|
28
|
+
}
|
|
29
|
+
return updatedData;
|
|
30
|
+
};
|
|
31
|
+
exports.callResponseInterceptors = callResponseInterceptors;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const callResponseInterceptors_1 = require("./callResponseInterceptors");
|
|
4
|
+
describe('callResponseInterceptors', () => {
|
|
5
|
+
test('Should call all passed response interceptors in order: route -> request -> server', () => {
|
|
6
|
+
const initialData = '';
|
|
7
|
+
const request = {};
|
|
8
|
+
const response = {};
|
|
9
|
+
const routeInterceptor = jest.fn((data) => `${data}routeInterceptor;`);
|
|
10
|
+
const requestInterceptor = jest.fn((data) => `${data}requestInterceptor;`);
|
|
11
|
+
const serverInterceptor = jest.fn((data) => `${data}serverInterceptor`);
|
|
12
|
+
expect((0, callResponseInterceptors_1.callResponseInterceptors)({
|
|
13
|
+
data: initialData,
|
|
14
|
+
request,
|
|
15
|
+
response
|
|
16
|
+
})).toBe('');
|
|
17
|
+
expect(routeInterceptor.mock.calls.length).toBe(0);
|
|
18
|
+
expect(requestInterceptor.mock.calls.length).toBe(0);
|
|
19
|
+
expect(serverInterceptor.mock.calls.length).toBe(0);
|
|
20
|
+
expect((0, callResponseInterceptors_1.callResponseInterceptors)({
|
|
21
|
+
data: initialData,
|
|
22
|
+
request,
|
|
23
|
+
response,
|
|
24
|
+
interceptors: {
|
|
25
|
+
routeInterceptor,
|
|
26
|
+
requestInterceptor,
|
|
27
|
+
serverInterceptor
|
|
28
|
+
}
|
|
29
|
+
})).toBe('routeInterceptor;requestInterceptor;serverInterceptor');
|
|
30
|
+
expect(routeInterceptor.mock.calls.length).toBe(1);
|
|
31
|
+
expect(requestInterceptor.mock.calls.length).toBe(1);
|
|
32
|
+
expect(serverInterceptor.mock.calls.length).toBe(1);
|
|
33
|
+
expect(routeInterceptor.mock.invocationCallOrder[0]).toBeLessThan(requestInterceptor.mock.invocationCallOrder[0]);
|
|
34
|
+
expect(requestInterceptor.mock.invocationCallOrder[0]).toBeLessThan(serverInterceptor.mock.invocationCallOrder[0]);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createRoutes = void 0;
|
|
4
|
+
const isEntityValuesEqual_1 = require("../../configs/isEntitiesEqual/isEntityValuesEqual");
|
|
5
|
+
const prepareRequestConfigs_1 = require("../../configs/prepareRequestConfigs/prepareRequestConfigs");
|
|
6
|
+
const callRequestInterceptors_1 = require("../callRequestInterceptors/callRequestInterceptors");
|
|
7
|
+
const callResponseInterceptors_1 = require("../callResponseInterceptors/callResponseInterceptors");
|
|
8
|
+
const createRoutes = (router, mockServerConfig) => {
|
|
9
|
+
(0, prepareRequestConfigs_1.prepareRequestConfigs)(mockServerConfig.configs).forEach((requestConfig) => {
|
|
10
|
+
router.route(requestConfig.path)[requestConfig.method]((request, response) => {
|
|
11
|
+
(0, callRequestInterceptors_1.callRequestInterceptors)({
|
|
12
|
+
request,
|
|
13
|
+
interceptors: {
|
|
14
|
+
requestInterceptor: requestConfig.interceptors?.request,
|
|
15
|
+
serverInterceptor: mockServerConfig.interceptors?.request
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
const matchedRouteConfig = requestConfig.routes.find(({ entities }) => {
|
|
19
|
+
if (!entities)
|
|
20
|
+
return true;
|
|
21
|
+
return Object.entries(entities).every(([entity, entityValue]) => (0, isEntityValuesEqual_1.isEntityValuesEqual)(entityValue, request[entity]));
|
|
22
|
+
});
|
|
23
|
+
if (!matchedRouteConfig) {
|
|
24
|
+
return response
|
|
25
|
+
.status(404)
|
|
26
|
+
.json(`No data for ${request.method}:${request.baseUrl}${request.path}`);
|
|
27
|
+
}
|
|
28
|
+
const data = (0, callResponseInterceptors_1.callResponseInterceptors)({
|
|
29
|
+
data: matchedRouteConfig.data,
|
|
30
|
+
request,
|
|
31
|
+
response,
|
|
32
|
+
interceptors: {
|
|
33
|
+
routeInterceptor: matchedRouteConfig.interceptors?.response,
|
|
34
|
+
requestInterceptor: requestConfig.interceptors?.response,
|
|
35
|
+
serverInterceptor: mockServerConfig.interceptors?.response
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return response.status(response.statusCode).json(data);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
return router;
|
|
42
|
+
};
|
|
43
|
+
exports.createRoutes = createRoutes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|