mock-config-server 3.2.0 → 3.3.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.
Files changed (89) hide show
  1. package/README.md +153 -3
  2. package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/isDescriptorValueValid.d.ts +2 -1
  3. package/dist/bin/validateMockServerConfig/helpers/isDescriptorValueValid/isDescriptorValueValid.js +30 -7
  4. package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateGraphqlConfig.js +9 -3
  5. package/dist/bin/validateMockServerConfig/validateGraphqlConfig/validateRoutes/validateRoutes.js +67 -18
  6. package/dist/bin/validateMockServerConfig/validateQueue/validateQueue.d.ts +1 -0
  7. package/dist/bin/validateMockServerConfig/validateQueue/validateQueue.js +29 -0
  8. package/dist/bin/validateMockServerConfig/validateRestConfig/validateRoutes/validateRoutes.js +67 -18
  9. package/dist/bin/validateMockServerConfig/validateSettings/validateSettings.d.ts +1 -0
  10. package/dist/bin/validateMockServerConfig/validateSettings/validateSettings.js +34 -0
  11. package/dist/src/core/database/createDatabaseRoutes/createDatabaseRoutes.test.ts +112 -0
  12. package/dist/src/core/database/createDatabaseRoutes/helpers/array/createNewId/createNewId.test.ts +13 -0
  13. package/dist/src/core/database/createDatabaseRoutes/helpers/array/findIndexById/findIndexById.test.ts +17 -0
  14. package/dist/src/core/database/createDatabaseRoutes/helpers/array/isIndex/isIndex.test.ts +30 -0
  15. package/dist/src/core/database/createDatabaseRoutes/helpers/createNestedDatabaseRoutes/createNestedDatabaseRoutes.js +45 -2
  16. package/dist/src/core/database/createDatabaseRoutes/helpers/createNestedDatabaseRoutes/createNestedDatabaseRoutes.test.ts +399 -0
  17. package/dist/src/core/database/createDatabaseRoutes/helpers/createShallowDatabaseRoutes/createShallowDatabaseRoutes.test.ts +118 -0
  18. package/dist/src/core/database/createDatabaseRoutes/helpers/filter/filter.d.ts +2 -1
  19. package/dist/src/core/database/createDatabaseRoutes/helpers/operators/operators.d.ts +3 -0
  20. package/dist/src/core/database/createDatabaseRoutes/helpers/operators/operators.js +30 -0
  21. package/dist/src/core/database/createDatabaseRoutes/helpers/pagination/pagination.d.ts +13 -0
  22. package/dist/src/core/database/createDatabaseRoutes/helpers/pagination/pagination.js +36 -0
  23. package/dist/src/core/database/createDatabaseRoutes/helpers/search/search.d.ts +3 -0
  24. package/dist/src/core/database/createDatabaseRoutes/helpers/search/search.js +31 -0
  25. package/dist/src/core/database/createDatabaseRoutes/helpers/sort/sort.d.ts +2 -0
  26. package/dist/src/core/database/createDatabaseRoutes/helpers/sort/sort.js +42 -0
  27. package/dist/src/core/database/createDatabaseRoutes/helpers/splitDatabaseByNesting/splitDatabaseByNesting.test.ts +25 -0
  28. package/dist/src/core/database/createDatabaseRoutes/storages/File/FileStorage.test.ts +156 -0
  29. package/dist/src/core/database/createDatabaseRoutes/storages/File/FileWriter.test.ts +48 -0
  30. package/dist/src/core/database/createDatabaseRoutes/storages/Memory/MemoryStorage.test.ts +96 -0
  31. package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.d.ts +7 -1
  32. package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.js +65 -24
  33. package/dist/src/core/graphql/createGraphQLRoutes/createGraphQLRoutes.test.ts +851 -0
  34. package/dist/src/core/graphql/createGraphQLRoutes/helpers/prepareGraphQLRequestConfigs/prepareGraphQLRequestConfigs.test.ts +116 -0
  35. package/dist/src/core/middlewares/cookieParseMiddleware/cookieParseMiddleware.test.ts +22 -0
  36. package/dist/src/core/middlewares/cookieParseMiddleware/helpers/parseCookie/parseCookie.test.ts +45 -0
  37. package/dist/src/core/middlewares/corsMiddleware/corsMiddleware.test.ts +152 -0
  38. package/dist/src/core/middlewares/corsMiddleware/helpers/getAllowedOrigins/getAllowedOrigins.test.ts +15 -0
  39. package/dist/src/core/middlewares/errorMiddleware/errorMiddleware.test.ts +29 -0
  40. package/dist/src/core/middlewares/noCorsMiddleware/noCorsMiddleware.test.ts +49 -0
  41. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getGraphqlUrlSuggestions/getGraphqlUrlSuggestions.test.ts +27 -0
  42. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getLevenshteinDistance/getLevenshteinDistance.test.ts +12 -0
  43. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/getRestUrlSuggestions.test.ts +54 -0
  44. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getActualRestUrlMeaningfulString/getActualRestUrlMeaningfulString.test.ts +12 -0
  45. package/dist/src/core/middlewares/notFoundMiddleware/helpers/getRestUrlSuggestions/helpers/getPatternRestUrlMeaningfulString/getPatternRestUrlMeaningfulString.test.ts +10 -0
  46. package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.js +1 -3
  47. package/dist/src/core/middlewares/notFoundMiddleware/notFoundMiddleware.test.ts +285 -0
  48. package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.d.ts +7 -1
  49. package/dist/src/core/middlewares/requestInterceptorMiddleware/requestInterceptorMiddleware.js +6 -2
  50. package/dist/src/core/rest/createRestRoutes/createRestRoutes.d.ts +7 -1
  51. package/dist/src/core/rest/createRestRoutes/createRestRoutes.js +55 -12
  52. package/dist/src/core/rest/createRestRoutes/createRestRoutes.test.ts +648 -0
  53. package/dist/src/core/rest/createRestRoutes/helpers/prepareRestRequestConfigs/prepareRestRequestConfigs.test.ts +154 -0
  54. package/dist/src/server/createDatabaseMockServer/createDatabaseMockServer.js +4 -1
  55. package/dist/src/server/createGraphQLMockServer/createGraphQLMockServer.js +11 -4
  56. package/dist/src/server/createMockServer/createMockServer.js +28 -9
  57. package/dist/src/server/createRestMockServer/createRestMockServer.js +11 -4
  58. package/dist/src/server/index.d.ts +3 -3
  59. package/dist/src/server/index.js +23 -23
  60. package/dist/src/static/views/features/scheme/index.js +31 -31
  61. package/dist/src/utils/helpers/config/resolveEntityValues/resolveEntityValues.test.ts +1452 -0
  62. package/dist/src/utils/helpers/entities/convertToEntityDescriptor/convertToEntityDescriptor.test.ts +27 -0
  63. package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.d.ts +2 -1
  64. package/dist/src/utils/helpers/entities/isEntityDescriptor/isEntityDescriptor.test.ts +15 -0
  65. package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.d.ts +7 -2
  66. package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.js +6 -4
  67. package/dist/src/utils/helpers/graphql/getGraphQLInput/getGraphQLInput.test.ts +140 -0
  68. package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.d.ts +1 -1
  69. package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.js +1 -1
  70. package/dist/src/utils/helpers/graphql/parseQuery/parseQuery.test.ts +32 -0
  71. package/dist/src/utils/helpers/interceptors/callRequestInterceptor/callRequestInterceptors.test.ts +53 -0
  72. package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.js +19 -10
  73. package/dist/src/utils/helpers/interceptors/callResponseInterceptors/callResponseInterceptors.test.ts +262 -0
  74. package/dist/src/utils/helpers/isPlainObject/isPlainObject.test.ts +20 -0
  75. package/dist/src/utils/helpers/isPrimitive/isPrimitive.test.ts +26 -0
  76. package/dist/src/utils/helpers/isRegExp/isRegExp.test.ts +20 -0
  77. package/dist/src/utils/helpers/url/convertWin32PathToUnix/convertWin32PathToUnix.test.ts +21 -0
  78. package/dist/src/utils/helpers/url/getUrlParts/getUrlParts.test.ts +8 -0
  79. package/dist/src/utils/helpers/url/removeLeadingAndTrailingSlashes/removeLeadingAndTrailingSlashes.test.ts +10 -0
  80. package/dist/src/utils/helpers/url/urlJoin/urlJoin.test.ts +9 -0
  81. package/dist/src/utils/types/graphql.d.ts +60 -51
  82. package/dist/src/utils/types/index.d.ts +1 -0
  83. package/dist/src/utils/types/index.js +11 -0
  84. package/dist/src/utils/types/interceptors.d.ts +1 -1
  85. package/dist/src/utils/types/rest.d.ts +47 -40
  86. package/dist/src/utils/types/utils.d.ts +8 -0
  87. package/dist/src/utils/types/utils.js +1 -0
  88. package/dist/src/utils/types/values.d.ts +0 -1
  89. package/package.json +2 -3
@@ -0,0 +1,648 @@
1
+ import express from 'express';
2
+ import request from 'supertest';
3
+
4
+ import { urlJoin } from '@/utils/helpers';
5
+ import type { MockServerConfig, RestConfig } from '@/utils/types';
6
+
7
+ import { createRestRoutes } from './createRestRoutes';
8
+
9
+ const createServer = (
10
+ mockServerConfig: Pick<MockServerConfig, 'interceptors' | 'baseUrl'> & {
11
+ rest: RestConfig;
12
+ }
13
+ ) => {
14
+ const { baseUrl, rest, interceptors } = mockServerConfig;
15
+ const server = express();
16
+ const routerBase = express.Router();
17
+ const routerWithRoutes = createRestRoutes({
18
+ router: routerBase,
19
+ restConfig: rest,
20
+ serverResponseInterceptor: interceptors?.response
21
+ });
22
+
23
+ const restBaseUrl = urlJoin(baseUrl ?? '/', rest?.baseUrl ?? '/');
24
+
25
+ server.use(express.json());
26
+ server.use(restBaseUrl, routerWithRoutes);
27
+ return server;
28
+ };
29
+
30
+ describe('createRestRoutes', () => {
31
+ test('Should return 404 for no matched request configs', async () => {
32
+ const server = createServer({
33
+ rest: {
34
+ configs: [
35
+ {
36
+ path: '/users',
37
+ method: 'get',
38
+ routes: [
39
+ {
40
+ entities: {
41
+ headers: {
42
+ key1: 'value1'
43
+ }
44
+ },
45
+ data: { name: 'John', surname: 'Doe' }
46
+ }
47
+ ]
48
+ }
49
+ ]
50
+ }
51
+ });
52
+
53
+ const response = await request(server).get('/users').set({ key2: 'value2' });
54
+
55
+ expect(response.statusCode).toBe(404);
56
+ });
57
+
58
+ test('Should have response Cache-Control header equals to max-age=0, must-revalidate', async () => {
59
+ const server = createServer({
60
+ rest: {
61
+ configs: [
62
+ {
63
+ path: '/users',
64
+ method: 'get',
65
+ routes: [{ data: { name: 'John', surname: 'Doe' } }]
66
+ }
67
+ ]
68
+ }
69
+ });
70
+
71
+ const response = await request(server).get('/users');
72
+ expect(response.headers['cache-control']).toBe('max-age=0, must-revalidate');
73
+ });
74
+ });
75
+
76
+ describe('createRestRoutes: content', () => {
77
+ test('Should correctly use data function', async () => {
78
+ const server = createServer({
79
+ rest: {
80
+ configs: [
81
+ {
82
+ path: '/users',
83
+ method: 'get',
84
+ routes: [
85
+ {
86
+ entities: {
87
+ query: {
88
+ key1: 'value1'
89
+ }
90
+ },
91
+ data: ({ url }, { query }) => ({
92
+ url,
93
+ query
94
+ })
95
+ }
96
+ ]
97
+ }
98
+ ]
99
+ }
100
+ });
101
+
102
+ const response = await request(server).get('/users').query({ key1: 'value1' });
103
+
104
+ expect(response.statusCode).toBe(200);
105
+ expect(response.body).toEqual({
106
+ url: '/users?key1=value1',
107
+ query: {
108
+ key1: 'value1'
109
+ }
110
+ });
111
+ });
112
+
113
+ test('Should correctly use data function with polling setting', async () => {
114
+ const server = createServer({
115
+ rest: {
116
+ configs: [
117
+ {
118
+ path: '/users',
119
+ method: 'get',
120
+ routes: [
121
+ {
122
+ settings: { polling: true },
123
+ entities: {
124
+ query: {
125
+ key1: 'value1'
126
+ }
127
+ },
128
+ queue: [
129
+ {
130
+ data: ({ url }, { query }) => ({
131
+ url,
132
+ query
133
+ })
134
+ }
135
+ ]
136
+ }
137
+ ]
138
+ }
139
+ ]
140
+ }
141
+ });
142
+
143
+ const response = await request(server).get('/users').query({ key1: 'value1' });
144
+
145
+ expect(response.statusCode).toBe(200);
146
+ expect(response.body).toEqual({
147
+ url: '/users?key1=value1',
148
+ query: {
149
+ key1: 'value1'
150
+ }
151
+ });
152
+ });
153
+
154
+ test('Should return same polling data with time param', async () => {
155
+ jest.useFakeTimers();
156
+ const server = createServer({
157
+ rest: {
158
+ configs: [
159
+ {
160
+ path: '/users',
161
+ method: 'get',
162
+ routes: [
163
+ {
164
+ settings: { polling: true },
165
+ queue: [
166
+ { time: 2000, data: { name: 'John', surname: 'Doe' } },
167
+ { data: { name: 'John', surname: 'Smith' } }
168
+ ]
169
+ }
170
+ ]
171
+ }
172
+ ]
173
+ }
174
+ });
175
+
176
+ const query = { key1: 'value1' };
177
+
178
+ const firstResponse = await request(server).get('/users').query(query);
179
+ expect(firstResponse.statusCode).toBe(200);
180
+ expect(firstResponse.body).toEqual({ name: 'John', surname: 'Doe' });
181
+
182
+ jest.advanceTimersByTime(1000);
183
+
184
+ const secondResponse = await request(server).get('/users').query(query);
185
+ expect(secondResponse.statusCode).toBe(200);
186
+ expect(secondResponse.body).toEqual({ name: 'John', surname: 'Doe' });
187
+
188
+ jest.advanceTimersByTime(1000);
189
+
190
+ const thirdResponse = await request(server).get('/users').query(query);
191
+ expect(thirdResponse.statusCode).toBe(200);
192
+ expect(thirdResponse.body).toEqual({ name: 'John', surname: 'Smith' });
193
+
194
+ jest.useRealTimers();
195
+ });
196
+ });
197
+
198
+ describe('createRestRoutes: settings', () => {
199
+ test('Should correctly process the request with polling', async () => {
200
+ const server = createServer({
201
+ rest: {
202
+ configs: [
203
+ {
204
+ path: '/users',
205
+ method: 'get',
206
+ routes: [
207
+ {
208
+ settings: { polling: true },
209
+ queue: [
210
+ { data: { name: 'John', surname: 'Doe' } },
211
+ { data: { name: 'John', surname: 'Smith' } }
212
+ ]
213
+ }
214
+ ]
215
+ }
216
+ ]
217
+ }
218
+ });
219
+
220
+ const firstResponse = await request(server).get('/users');
221
+ expect(firstResponse.statusCode).toBe(200);
222
+ expect(firstResponse.body).toEqual({ name: 'John', surname: 'Doe' });
223
+
224
+ const secondResponse = await request(server).get('/users');
225
+ expect(secondResponse.statusCode).toBe(200);
226
+ expect(secondResponse.body).toEqual({ name: 'John', surname: 'Smith' });
227
+
228
+ const thirdResponse = await request(server).get('/users');
229
+ expect(thirdResponse.statusCode).toBe(200);
230
+ expect(thirdResponse.body).toEqual({ name: 'John', surname: 'Doe' });
231
+ });
232
+
233
+ test('Should correct handle empty queue', async () => {
234
+ const server = createServer({
235
+ rest: {
236
+ configs: [
237
+ {
238
+ path: '/users',
239
+ method: 'get',
240
+ routes: [
241
+ {
242
+ settings: { polling: true },
243
+ queue: []
244
+ }
245
+ ]
246
+ }
247
+ ]
248
+ }
249
+ });
250
+
251
+ const response = await request(server).get('/users');
252
+ expect(response.statusCode).toBe(404);
253
+ });
254
+ });
255
+
256
+ describe('createRestRoutes: entities', () => {
257
+ test('Should match config by entities "includes" behavior', async () => {
258
+ const server = createServer({
259
+ rest: {
260
+ configs: [
261
+ {
262
+ path: '/users',
263
+ method: 'get',
264
+ routes: [
265
+ {
266
+ entities: {
267
+ headers: {
268
+ key1: 'value1',
269
+ key2: 'value2'
270
+ },
271
+ query: {
272
+ key1: 'value1'
273
+ }
274
+ },
275
+ data: { name: 'John', surname: 'Doe' }
276
+ }
277
+ ]
278
+ }
279
+ ]
280
+ }
281
+ });
282
+
283
+ const response = await request(server)
284
+ .get('/users')
285
+ .set({ key1: 'value1', key2: 'value2' })
286
+ .query({ key1: 'value1', key2: 'value2' });
287
+
288
+ expect(response.statusCode).toBe(200);
289
+ expect(response.body).toStrictEqual({ name: 'John', surname: 'Doe' });
290
+ });
291
+
292
+ test('Should match config by entities "includes" behavior with path regexp', async () => {
293
+ const server = createServer({
294
+ rest: {
295
+ configs: [
296
+ {
297
+ path: /^\/us(.+?)rs$/,
298
+ method: 'get',
299
+ routes: [
300
+ {
301
+ entities: {
302
+ headers: {
303
+ key1: 'value1',
304
+ key2: 'value2'
305
+ },
306
+ query: {
307
+ key1: 'value1'
308
+ }
309
+ },
310
+ data: { name: 'John', surname: 'Doe' }
311
+ }
312
+ ]
313
+ }
314
+ ]
315
+ }
316
+ });
317
+
318
+ const response = await request(server)
319
+ .get('/users')
320
+ .set({ key1: 'value1', key2: 'value2' })
321
+ .query({ key1: 'value1', key2: 'value2' });
322
+
323
+ expect(response.statusCode).toBe(200);
324
+ expect(response.body).toStrictEqual({ name: 'John', surname: 'Doe' });
325
+ });
326
+
327
+ test('Should give priority to more specific route config', async () => {
328
+ const server = createServer({
329
+ rest: {
330
+ configs: [
331
+ {
332
+ path: '/users',
333
+ method: 'get',
334
+ routes: [
335
+ {
336
+ entities: {
337
+ headers: {
338
+ key1: 'value1',
339
+ key2: 'value2'
340
+ },
341
+ query: {
342
+ key1: 'value1'
343
+ }
344
+ },
345
+ data: { name: 'John', surname: 'Doe' }
346
+ },
347
+ {
348
+ entities: {
349
+ headers: {
350
+ key1: 'value1',
351
+ key2: 'value2'
352
+ },
353
+ query: {
354
+ key1: 'value1',
355
+ key2: 'value2'
356
+ }
357
+ },
358
+ data: { name: 'John', surname: 'Smith' }
359
+ }
360
+ ]
361
+ }
362
+ ]
363
+ }
364
+ });
365
+
366
+ const response = await request(server)
367
+ .get('/users')
368
+ .set({ key1: 'value1', key2: 'value2' })
369
+ .query({ key1: 'value1', key2: 'value2' });
370
+
371
+ expect(response.statusCode).toBe(200);
372
+ expect(response.body).toStrictEqual({ name: 'John', surname: 'Smith' });
373
+ });
374
+
375
+ test('Should strictly compare plain array body if top level descriptor used', async () => {
376
+ const server = createServer({
377
+ rest: {
378
+ configs: [
379
+ {
380
+ path: '/users',
381
+ method: 'post',
382
+ routes: [
383
+ {
384
+ entities: {
385
+ body: {
386
+ checkMode: 'equals',
387
+ value: [
388
+ {
389
+ key1: 'value1',
390
+ key2: { nestedKey1: 'nestedValue1' }
391
+ }
392
+ ]
393
+ }
394
+ },
395
+ data: { name: 'John', surname: 'Doe' }
396
+ }
397
+ ]
398
+ }
399
+ ]
400
+ }
401
+ });
402
+
403
+ const successResponse = await request(server)
404
+ .post('/users')
405
+ .set('Content-Type', 'application/json')
406
+ .send([{ key1: 'value1', key2: { nestedKey1: 'nestedValue1' } }]);
407
+ expect(successResponse.statusCode).toBe(200);
408
+ expect(successResponse.body).toStrictEqual({ name: 'John', surname: 'Doe' });
409
+
410
+ const failedResponse = await request(server)
411
+ .post('/users')
412
+ .set('Content-Type', 'application/json')
413
+ .send([{ key1: 'value1', key2: { nestedKey1: 'nestedValue1', nestedKey2: 'nestedValue2' } }]);
414
+ expect(failedResponse.statusCode).toBe(404);
415
+ });
416
+
417
+ test('Should strictly compare plain object body if top level descriptor used', async () => {
418
+ const server = createServer({
419
+ rest: {
420
+ configs: [
421
+ {
422
+ path: '/users',
423
+ method: 'post',
424
+ routes: [
425
+ {
426
+ entities: {
427
+ body: {
428
+ checkMode: 'equals',
429
+ value: {
430
+ key1: 'value1',
431
+ key2: { nestedKey1: 'nestedValue1' }
432
+ }
433
+ }
434
+ },
435
+ data: { name: 'John', surname: 'Doe' }
436
+ }
437
+ ]
438
+ }
439
+ ]
440
+ }
441
+ });
442
+
443
+ const response = await request(server)
444
+ .post('/users')
445
+ .set('Content-Type', 'application/json')
446
+ .send({ key1: 'value1', key2: { nestedKey1: 'nestedValue1' } });
447
+
448
+ expect(response.statusCode).toBe(200);
449
+ expect(response.body).toStrictEqual({ name: 'John', surname: 'Doe' });
450
+ });
451
+
452
+ test('Should correctly resolve flat object body with descriptors', async () => {
453
+ const server = createServer({
454
+ rest: {
455
+ configs: [
456
+ {
457
+ path: '/users',
458
+ method: 'post',
459
+ routes: [
460
+ {
461
+ entities: {
462
+ body: {
463
+ key1: 'value1',
464
+ 'key2.nestedKey1': {
465
+ checkMode: 'equals',
466
+ value: 'nestedValue1'
467
+ }
468
+ }
469
+ },
470
+ data: { name: 'John', surname: 'Doe' }
471
+ }
472
+ ]
473
+ }
474
+ ]
475
+ }
476
+ });
477
+
478
+ const response = await request(server)
479
+ .post('/users')
480
+ .set('Content-Type', 'application/json')
481
+ .send({ key1: 'value1', key2: { nestedKey1: 'nestedValue1', nestedKey2: 'nestedValue2' } });
482
+
483
+ expect(response.statusCode).toBe(200);
484
+ expect(response.body).toStrictEqual({ name: 'John', surname: 'Doe' });
485
+ });
486
+
487
+ test('Should be case-insensitive for header keys', async () => {
488
+ const server = createServer({
489
+ rest: {
490
+ configs: [
491
+ {
492
+ path: '/users',
493
+ method: 'get',
494
+ routes: [
495
+ {
496
+ entities: {
497
+ headers: {
498
+ lowercase: 'lowercase',
499
+ UPPERCASE: 'UPPERCASE'
500
+ }
501
+ },
502
+ data: { name: 'John', surname: 'Doe' }
503
+ }
504
+ ]
505
+ }
506
+ ]
507
+ }
508
+ });
509
+
510
+ const response = await request(server)
511
+ .get('/users')
512
+ .set('Content-Type', 'application/json')
513
+ .set({ LowerCase: 'lowercase', upperCase: 'UPPERCASE' });
514
+
515
+ expect(response.statusCode).toBe(200);
516
+ expect(response.body).toStrictEqual({ name: 'John', surname: 'Doe' });
517
+ });
518
+ });
519
+
520
+ describe('createRestRoutes: interceptors', () => {
521
+ test('Should call request interceptor', async () => {
522
+ const requestInterceptor = jest.fn();
523
+ const server = createServer({
524
+ rest: {
525
+ configs: [
526
+ {
527
+ path: '/users',
528
+ method: 'post',
529
+ routes: [
530
+ {
531
+ entities: {
532
+ body: {
533
+ key1: 'value1',
534
+ key2: 'value2'
535
+ }
536
+ },
537
+ data: { name: 'John', surname: 'Doe' }
538
+ }
539
+ ],
540
+ interceptors: { request: requestInterceptor }
541
+ },
542
+ {
543
+ path: '/settings',
544
+ method: 'post',
545
+ routes: [
546
+ {
547
+ entities: {
548
+ body: {
549
+ key1: 'value1',
550
+ key2: 'value2'
551
+ }
552
+ },
553
+ data: { name: 'John', surname: 'Smith' }
554
+ }
555
+ ]
556
+ }
557
+ ]
558
+ }
559
+ });
560
+
561
+ await request(server)
562
+ .post('/users')
563
+ .set('Content-Type', 'application/json')
564
+ .send({ key1: 'value1', key2: 'value2' });
565
+ expect(requestInterceptor.mock.calls.length).toBe(1);
566
+
567
+ await request(server)
568
+ .post('/settings')
569
+ .set('Content-Type', 'application/json')
570
+ .send({ key1: 'value1', key2: 'value2' });
571
+ expect(requestInterceptor.mock.calls.length).toBe(1);
572
+ });
573
+
574
+ test('Should call response interceptors in order: route -> request -> server', async () => {
575
+ const routeInterceptor = jest.fn();
576
+ const requestInterceptor = jest.fn();
577
+ const serverInterceptor = jest.fn();
578
+ const server = createServer({
579
+ rest: {
580
+ configs: [
581
+ {
582
+ path: '/users',
583
+ method: 'post',
584
+ routes: [
585
+ {
586
+ entities: {
587
+ body: {
588
+ key1: 'value1',
589
+ key2: 'value2'
590
+ }
591
+ },
592
+ data: { name: 'John', surname: 'Doe' },
593
+ interceptors: { response: routeInterceptor }
594
+ }
595
+ ],
596
+ interceptors: { response: requestInterceptor }
597
+ },
598
+ {
599
+ path: '/settings',
600
+ method: 'post',
601
+ routes: [
602
+ {
603
+ entities: {
604
+ body: {
605
+ key1: 'value1',
606
+ key2: 'value2'
607
+ }
608
+ },
609
+ data: { name: 'John', surname: 'Smith' }
610
+ }
611
+ ]
612
+ }
613
+ ]
614
+ },
615
+ interceptors: { response: serverInterceptor }
616
+ });
617
+
618
+ await request(server)
619
+ .post('/users')
620
+ .set('Content-Type', 'application/json')
621
+ .send({ key1: 'value1', key2: 'value2' });
622
+ expect(routeInterceptor.mock.calls.length).toBe(1);
623
+ expect(requestInterceptor.mock.calls.length).toBe(1);
624
+ expect(serverInterceptor.mock.calls.length).toBe(1);
625
+ expect(routeInterceptor.mock.invocationCallOrder[0]).toBeLessThan(
626
+ requestInterceptor.mock.invocationCallOrder[0]
627
+ );
628
+ expect(requestInterceptor.mock.invocationCallOrder[0]).toBeLessThan(
629
+ serverInterceptor.mock.invocationCallOrder[0]
630
+ );
631
+
632
+ await request(server)
633
+ .post('/settings')
634
+ .set('Content-Type', 'application/json')
635
+ .send({ key1: 'value1', key2: 'value2' });
636
+ expect(routeInterceptor.mock.calls.length).toBe(1);
637
+ expect(requestInterceptor.mock.calls.length).toBe(1);
638
+ expect(serverInterceptor.mock.calls.length).toBe(2);
639
+
640
+ await request(server)
641
+ .post('/messages')
642
+ .set('Content-Type', 'application/json')
643
+ .send({ key1: 'value1', key2: 'value2' });
644
+ expect(routeInterceptor.mock.calls.length).toBe(1);
645
+ expect(requestInterceptor.mock.calls.length).toBe(1);
646
+ expect(serverInterceptor.mock.calls.length).toBe(2);
647
+ });
648
+ });