mock-config-server 1.0.3 → 1.0.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mock-config-server",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Tool that easily and quickly imitates server operation, create full fake api in few steps",
5
5
  "author": {
6
6
  "name": "SIBERIA CAN CODE 🧊",
@@ -1,3 +0,0 @@
1
- export = jestConfig;
2
- /** @type {import('ts-jest').JestConfigWithTsJest} */
3
- declare const jestConfig: import('ts-jest').JestConfigWithTsJest;
@@ -1,7 +0,0 @@
1
- "use strict";
2
- /** @type {import('ts-jest').JestConfigWithTsJest} */
3
- const jestConfig = {
4
- preset: 'ts-jest',
5
- testEnvironment: 'node'
6
- };
7
- module.exports = jestConfig;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const isEntityValuesEqual_1 = require("./isEntityValuesEqual");
4
- describe('isEntityValuesEqual', () => {
5
- test('Primitive values should compare independent of their types', () => {
6
- expect((0, isEntityValuesEqual_1.isEntityValuesEqual)(13, '13')).toBe(true);
7
- expect((0, isEntityValuesEqual_1.isEntityValuesEqual)(true, 'true')).toBe(true);
8
- });
9
- test('Arrays should be full equal with nested objects (independent of primitive values types)', () => {
10
- expect((0, isEntityValuesEqual_1.isEntityValuesEqual)([1, 2, 3], ['1', '2', '3'])).toBe(true);
11
- expect((0, isEntityValuesEqual_1.isEntityValuesEqual)([{ key1: 'value1', key2: 'value2' }], [{ key2: 'value2', key1: 'value1' }])).toBe(true);
12
- expect((0, isEntityValuesEqual_1.isEntityValuesEqual)([{ key1: 'value1', key2: 'value2' }], [{ key1: 'value1', key2: 'value2', key3: 'value3' }])).toBe(false);
13
- expect((0, isEntityValuesEqual_1.isEntityValuesEqual)([{ key1: 'value1', key2: { nestedKey1: 'nestedValue1' } }], [{ key1: 'value1', key2: { nestedKey1: 'nestedValue1' } }])).toBe(true);
14
- expect((0, isEntityValuesEqual_1.isEntityValuesEqual)([{ key1: 'value1', key2: { nestedKey1: 'nestedValue1' } }], [{ key1: 'value1', key2: { nestedKey1: 'nestedValue1', nestedKey2: 'nestedValue2' } }])).toBe(false);
15
- });
16
- test('Plain objects should compare by "second includes first" with nested objects', () => {
17
- expect((0, isEntityValuesEqual_1.isEntityValuesEqual)({ key1: 'value1', key2: 'value2' }, { key1: 'value1', key2: 'value2', key3: 'value3' })).toBe(true);
18
- expect((0, isEntityValuesEqual_1.isEntityValuesEqual)({ key1: 'value1', key2: 'value2', key3: 'value3' }, { key1: 'value1', key2: 'value2' })).toBe(false);
19
- expect((0, isEntityValuesEqual_1.isEntityValuesEqual)({ key1: 'value1', key2: { nestedKey1: 'nestedValue1' } }, {
20
- key1: 'value1',
21
- key2: { nestedKey1: 'nestedValue1', nestedKey2: 'nestedValue2' },
22
- key3: 'value3'
23
- })).toBe(true);
24
- });
25
- });
@@ -1,151 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const prepareRequestConfigs_1 = require("./prepareRequestConfigs");
4
- describe('prepareRequestConfigs', () => {
5
- test('Should not sort routes if they does not contain entities', () => {
6
- const requestConfigs = [
7
- {
8
- path: '/user',
9
- method: 'get',
10
- routes: [
11
- {
12
- data: { name: 'John', surname: 'Doe' }
13
- },
14
- {
15
- data: { name: 'John', surname: 'Smith' }
16
- },
17
- {
18
- data: { name: 'John', surname: 'John' }
19
- }
20
- ]
21
- }
22
- ];
23
- expect((0, prepareRequestConfigs_1.prepareRequestConfigs)(requestConfigs)).toStrictEqual(requestConfigs);
24
- });
25
- test('Should sort routes by their specificity of entities', () => {
26
- const requestConfigs = [
27
- {
28
- path: '/user',
29
- method: 'get',
30
- routes: [
31
- {
32
- entities: {
33
- headers: {
34
- header1: 'value'
35
- }
36
- },
37
- data: { name: 'John', surname: 'Doe' }
38
- },
39
- {
40
- entities: {
41
- headers: {
42
- header1: 'value',
43
- header2: 'value'
44
- }
45
- },
46
- data: { name: 'John', surname: 'Doe' }
47
- },
48
- {
49
- entities: {
50
- headers: {
51
- header1: 'value'
52
- },
53
- query: {
54
- query1: 'value',
55
- query2: 'value'
56
- }
57
- },
58
- data: { name: 'John', surname: 'Doe' }
59
- }
60
- ]
61
- }
62
- ];
63
- const expectedRequestConfigs = [
64
- {
65
- path: '/user',
66
- method: 'get',
67
- routes: [
68
- {
69
- entities: {
70
- headers: {
71
- header1: 'value'
72
- },
73
- query: {
74
- query1: 'value',
75
- query2: 'value'
76
- }
77
- },
78
- data: { name: 'John', surname: 'Doe' }
79
- },
80
- {
81
- entities: {
82
- headers: {
83
- header1: 'value',
84
- header2: 'value'
85
- }
86
- },
87
- data: { name: 'John', surname: 'Doe' }
88
- },
89
- {
90
- entities: {
91
- headers: {
92
- header1: 'value'
93
- }
94
- },
95
- data: { name: 'John', surname: 'Doe' }
96
- }
97
- ]
98
- }
99
- ];
100
- expect((0, prepareRequestConfigs_1.prepareRequestConfigs)(requestConfigs)).toStrictEqual(expectedRequestConfigs);
101
- });
102
- test('Should set not object body weight equals to one', () => {
103
- const requestConfigs = [
104
- {
105
- path: '/user',
106
- method: 'post',
107
- routes: [
108
- {
109
- entities: {
110
- body: ['value', 'value', 'value']
111
- },
112
- data: { name: 'John', surname: 'Doe' }
113
- },
114
- {
115
- entities: {
116
- headers: {
117
- header1: 'value',
118
- header2: 'value'
119
- }
120
- },
121
- data: { name: 'John', surname: 'Doe' }
122
- }
123
- ]
124
- }
125
- ];
126
- const expectedRequestConfigs = [
127
- {
128
- path: '/user',
129
- method: 'post',
130
- routes: [
131
- {
132
- entities: {
133
- headers: {
134
- header1: 'value',
135
- header2: 'value'
136
- }
137
- },
138
- data: { name: 'John', surname: 'Doe' }
139
- },
140
- {
141
- entities: {
142
- body: ['value', 'value', 'value']
143
- },
144
- data: { name: 'John', surname: 'Doe' }
145
- }
146
- ]
147
- }
148
- ];
149
- expect((0, prepareRequestConfigs_1.prepareRequestConfigs)(requestConfigs)).toStrictEqual(expectedRequestConfigs);
150
- });
151
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,86 +0,0 @@
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
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,24 +0,0 @@
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
- });
@@ -1,22 +0,0 @@
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
- });
@@ -1,28 +0,0 @@
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
- });
@@ -1,36 +0,0 @@
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
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,276 +0,0 @@
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 createRoutes_1 = require("./createRoutes");
9
- describe('createRoutes', () => {
10
- const createServer = (mockServerConfig) => {
11
- const server = (0, express_1.default)();
12
- const routerBase = express_1.default.Router();
13
- const routerWithRoutes = (0, createRoutes_1.createRoutes)(routerBase, mockServerConfig);
14
- server.use(express_1.default.json());
15
- server.use(mockServerConfig.baseUrl ?? '/', routerWithRoutes);
16
- return server;
17
- };
18
- test('Should match config by entities "includes" behavior', async () => {
19
- const server = createServer({
20
- configs: [
21
- {
22
- path: '/users',
23
- method: 'get',
24
- routes: [
25
- {
26
- entities: {
27
- headers: { key1: 'value1', key2: 'value2' },
28
- query: { key1: 'value1' }
29
- },
30
- data: { name: 'John', surname: 'Doe' }
31
- }
32
- ]
33
- }
34
- ]
35
- });
36
- const response = await (0, supertest_1.default)(server)
37
- .get('/users')
38
- .set({ key1: 'value1', key2: 'value2' })
39
- .query({ key1: 'value1', key2: 'value2' });
40
- expect(response.statusCode).toBe(200);
41
- expect(response.body).toStrictEqual({ name: 'John', surname: 'Doe' });
42
- });
43
- test('Should give priority to more specific route config', async () => {
44
- const server = createServer({
45
- configs: [
46
- {
47
- path: '/users',
48
- method: 'get',
49
- routes: [
50
- {
51
- entities: {
52
- headers: { key1: 'value1', key2: 'value2' },
53
- query: { key1: 'value1' }
54
- },
55
- data: { name: 'John', surname: 'Doe' }
56
- },
57
- {
58
- entities: {
59
- headers: { key1: 'value1', key2: 'value2' },
60
- query: { key1: 'value1', key2: 'value2' }
61
- },
62
- data: { name: 'John', surname: 'Smith' }
63
- }
64
- ]
65
- }
66
- ]
67
- });
68
- const response = await (0, supertest_1.default)(server)
69
- .get('/users')
70
- .set({ key1: 'value1', key2: 'value2', key3: 'value3' })
71
- .query({ key1: 'value1', key2: 'value2', key3: 'value3' });
72
- expect(response.statusCode).toBe(200);
73
- expect(response.body).toStrictEqual({ name: 'John', surname: 'Smith' });
74
- });
75
- test('Should return 404 and description text for no matched request entities', async () => {
76
- const server = createServer({
77
- configs: [
78
- {
79
- path: '/users',
80
- method: 'get',
81
- routes: [
82
- {
83
- entities: {
84
- headers: { key1: 'value1' }
85
- },
86
- data: { name: 'John', surname: 'Doe' }
87
- }
88
- ]
89
- }
90
- ]
91
- });
92
- const response = await (0, supertest_1.default)(server).get('/users').set({ key2: 'value2' });
93
- expect(response.statusCode).toBe(404);
94
- expect(response.body).toBe('No data for GET:/users');
95
- });
96
- test('Should compare non plain object body by full equal behavior', async () => {
97
- const server = createServer({
98
- configs: [
99
- {
100
- path: '/users',
101
- method: 'post',
102
- routes: [
103
- {
104
- entities: {
105
- body: [
106
- {
107
- key1: 'value1',
108
- key2: { nestedKey1: 'nestedValue1' }
109
- }
110
- ]
111
- },
112
- data: { name: 'John', surname: 'Doe' }
113
- }
114
- ]
115
- }
116
- ]
117
- });
118
- const successResponse = await (0, supertest_1.default)(server)
119
- .post('/users')
120
- .set('Content-Type', 'application/json')
121
- .send([{ key1: 'value1', key2: { nestedKey1: 'nestedValue1' } }]);
122
- expect(successResponse.statusCode).toBe(200);
123
- expect(successResponse.body).toStrictEqual({ name: 'John', surname: 'Doe' });
124
- const failedResponse = await (0, supertest_1.default)(server)
125
- .post('/users')
126
- .set('Content-Type', 'application/json')
127
- .send([{ key1: 'value1', key2: { nestedKey1: 'nestedValue1', nestedKey2: 'nestedValue2' } }]);
128
- expect(failedResponse.statusCode).toBe(404);
129
- expect(failedResponse.body).toBe('No data for POST:/users');
130
- });
131
- test('Should compare plain object body by "includes" behavior', async () => {
132
- const server = createServer({
133
- configs: [
134
- {
135
- path: '/users',
136
- method: 'post',
137
- routes: [
138
- {
139
- entities: {
140
- body: {
141
- key1: 'value1',
142
- key2: { nestedKey1: 'nestedValue1' }
143
- }
144
- },
145
- data: { name: 'John', surname: 'Doe' }
146
- }
147
- ]
148
- }
149
- ]
150
- });
151
- const response = await (0, supertest_1.default)(server)
152
- .post('/users')
153
- .set('Content-Type', 'application/json')
154
- .send({ key1: 'value1', key2: { nestedKey1: 'nestedValue1', nestedKey2: 'nestedValue2' } });
155
- expect(response.statusCode).toBe(200);
156
- expect(response.body).toStrictEqual({ name: 'John', surname: 'Doe' });
157
- });
158
- test('Should call request interceptors in order: request -> server', async () => {
159
- const requestInterceptor = jest.fn();
160
- const serverInterceptor = jest.fn();
161
- const server = createServer({
162
- configs: [
163
- {
164
- path: '/users',
165
- method: 'post',
166
- routes: [
167
- {
168
- entities: {
169
- body: {
170
- key1: 'value1',
171
- key2: 'value2'
172
- }
173
- },
174
- data: { name: 'John', surname: 'Doe' }
175
- }
176
- ],
177
- interceptors: { request: requestInterceptor }
178
- },
179
- {
180
- path: '/settings',
181
- method: 'post',
182
- routes: [
183
- {
184
- entities: {
185
- body: {
186
- key1: 'value1',
187
- key2: 'value2'
188
- }
189
- },
190
- data: { name: 'John', surname: 'Smith' }
191
- }
192
- ]
193
- }
194
- ],
195
- interceptors: { request: serverInterceptor }
196
- });
197
- await (0, supertest_1.default)(server)
198
- .post('/users')
199
- .set('Content-Type', 'application/json')
200
- .send({ key1: 'value1', key2: 'value2' });
201
- expect(requestInterceptor.mock.calls.length).toBe(1);
202
- expect(serverInterceptor.mock.calls.length).toBe(1);
203
- expect(requestInterceptor.mock.invocationCallOrder[0]).toBeLessThan(serverInterceptor.mock.invocationCallOrder[0]);
204
- await (0, supertest_1.default)(server)
205
- .post('/settings')
206
- .set('Content-Type', 'application/json')
207
- .send({ key1: 'value1', key2: 'value2' });
208
- expect(requestInterceptor.mock.calls.length).toBe(1);
209
- expect(serverInterceptor.mock.calls.length).toBe(2);
210
- });
211
- test('Should call response interceptors in order: route -> request -> server', async () => {
212
- const routeInterceptor = jest.fn();
213
- const requestInterceptor = jest.fn();
214
- const serverInterceptor = jest.fn();
215
- const server = createServer({
216
- configs: [
217
- {
218
- path: '/users',
219
- method: 'post',
220
- routes: [
221
- {
222
- entities: {
223
- body: {
224
- key1: 'value1',
225
- key2: 'value2'
226
- }
227
- },
228
- data: { name: 'John', surname: 'Doe' },
229
- interceptors: { response: routeInterceptor }
230
- }
231
- ],
232
- interceptors: { response: requestInterceptor }
233
- },
234
- {
235
- path: '/settings',
236
- method: 'post',
237
- routes: [
238
- {
239
- entities: {
240
- body: {
241
- key1: 'value1',
242
- key2: 'value2'
243
- }
244
- },
245
- data: { name: 'John', surname: 'Smith' }
246
- }
247
- ]
248
- }
249
- ],
250
- interceptors: { response: serverInterceptor }
251
- });
252
- await (0, supertest_1.default)(server)
253
- .post('/users')
254
- .set('Content-Type', 'application/json')
255
- .send({ key1: 'value1', key2: 'value2' });
256
- expect(routeInterceptor.mock.calls.length).toBe(1);
257
- expect(requestInterceptor.mock.calls.length).toBe(1);
258
- expect(serverInterceptor.mock.calls.length).toBe(1);
259
- expect(routeInterceptor.mock.invocationCallOrder[0]).toBeLessThan(requestInterceptor.mock.invocationCallOrder[0]);
260
- expect(requestInterceptor.mock.invocationCallOrder[0]).toBeLessThan(serverInterceptor.mock.invocationCallOrder[0]);
261
- await (0, supertest_1.default)(server)
262
- .post('/settings')
263
- .set('Content-Type', 'application/json')
264
- .send({ key1: 'value1', key2: 'value2' });
265
- expect(routeInterceptor.mock.calls.length).toBe(1);
266
- expect(requestInterceptor.mock.calls.length).toBe(1);
267
- expect(serverInterceptor.mock.calls.length).toBe(2);
268
- await (0, supertest_1.default)(server)
269
- .post('/messages')
270
- .set('Content-Type', 'application/json')
271
- .send({ key1: 'value1', key2: 'value2' });
272
- expect(routeInterceptor.mock.calls.length).toBe(1);
273
- expect(requestInterceptor.mock.calls.length).toBe(1);
274
- expect(serverInterceptor.mock.calls.length).toBe(2);
275
- });
276
- });