@e22m4u/ts-rest-router 0.0.7 → 0.1.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-ru.md +114 -91
- package/README.md +106 -85
- package/dist/cjs/index.cjs +427 -123
- package/dist/esm/controller-registry.d.ts +53 -11
- package/dist/esm/controller-registry.js +242 -104
- package/dist/esm/debuggable-service.js +1 -1
- package/dist/esm/decorators/after/after-decorator.d.ts +9 -0
- package/dist/esm/decorators/after/after-decorator.js +22 -0
- package/dist/esm/decorators/after/after-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/after/after-decorator.spec.js +115 -0
- package/dist/esm/decorators/after/after-metadata.d.ts +13 -0
- package/dist/esm/decorators/after/after-metadata.js +5 -0
- package/dist/esm/decorators/after/after-reflector.d.ts +22 -0
- package/dist/esm/decorators/after/after-reflector.js +29 -0
- package/dist/esm/decorators/after/after-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/after/after-reflector.spec.js +102 -0
- package/dist/esm/decorators/after/index.d.ts +3 -0
- package/dist/esm/decorators/after/index.js +3 -0
- package/dist/esm/decorators/before/before-decorator.d.ts +9 -0
- package/dist/esm/decorators/before/before-decorator.js +22 -0
- package/dist/esm/decorators/before/before-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/before/before-decorator.spec.js +115 -0
- package/dist/esm/decorators/before/before-metadata.d.ts +13 -0
- package/dist/esm/decorators/before/before-metadata.js +5 -0
- package/dist/esm/decorators/before/before-reflector.d.ts +22 -0
- package/dist/esm/decorators/before/before-reflector.js +29 -0
- package/dist/esm/decorators/before/before-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/before/before-reflector.spec.js +102 -0
- package/dist/esm/decorators/before/index.d.ts +3 -0
- package/dist/esm/decorators/before/index.js +3 -0
- package/dist/esm/decorators/controller/controller-decorator.d.ts +2 -1
- package/dist/esm/decorators/controller/controller-decorator.js +26 -1
- package/dist/esm/decorators/controller/controller-decorator.spec.js +28 -0
- package/dist/esm/decorators/index.d.ts +2 -0
- package/dist/esm/decorators/index.js +2 -0
- package/dist/esm/decorators/request-data/request-data-decorator.d.ts +1 -1
- package/dist/esm/decorators/request-data/request-data-decorator.js +1 -1
- package/dist/esm/decorators/request-data/request-data-decorator.spec.js +5 -5
- package/dist/esm/utils/create-debugger.d.ts +35 -2
- package/dist/esm/utils/create-debugger.js +71 -5
- package/package.json +1 -1
- package/src/controller-registry.spec.ts +597 -275
- package/src/controller-registry.ts +263 -128
- package/src/debuggable-service.ts +1 -1
- package/src/decorators/after/after-decorator.spec.ts +92 -0
- package/src/decorators/after/after-decorator.ts +40 -0
- package/src/decorators/after/after-metadata.ts +17 -0
- package/src/decorators/after/after-reflector.spec.ts +107 -0
- package/src/decorators/after/after-reflector.ts +45 -0
- package/src/decorators/after/index.ts +3 -0
- package/src/decorators/before/before-decorator.spec.ts +92 -0
- package/src/decorators/before/before-decorator.ts +40 -0
- package/src/decorators/before/before-metadata.ts +17 -0
- package/src/decorators/before/before-reflector.spec.ts +111 -0
- package/src/decorators/before/before-reflector.ts +50 -0
- package/src/decorators/before/index.ts +3 -0
- package/src/decorators/controller/controller-decorator.spec.ts +22 -0
- package/src/decorators/controller/controller-decorator.ts +29 -1
- package/src/decorators/index.ts +2 -0
- package/src/decorators/request-data/request-data-decorator.spec.ts +5 -5
- package/src/decorators/request-data/request-data-decorator.ts +1 -1
- package/src/utils/create-debugger.ts +84 -7
@@ -1,59 +1,74 @@
|
|
1
|
+
/* eslint mocha/no-sibling-hooks: 0 */
|
1
2
|
import {expect} from 'chai';
|
2
3
|
import {get} from './decorators/index.js';
|
3
4
|
import {post} from './decorators/index.js';
|
5
|
+
import {body} from './decorators/index.js';
|
6
|
+
import {after} from './decorators/index.js';
|
7
|
+
import {query} from './decorators/index.js';
|
8
|
+
import {param} from './decorators/index.js';
|
9
|
+
import {field} from './decorators/index.js';
|
10
|
+
import {cookie} from './decorators/index.js';
|
11
|
+
import {params} from './decorators/index.js';
|
12
|
+
import {before} from './decorators/index.js';
|
13
|
+
import {header} from './decorators/index.js';
|
14
|
+
import {cookies} from './decorators/index.js';
|
15
|
+
import {queries} from './decorators/index.js';
|
16
|
+
import {headers} from './decorators/index.js';
|
4
17
|
import {HookName} from '@e22m4u/js-trie-router';
|
5
18
|
import {controller} from './decorators/index.js';
|
6
19
|
import {TrieRouter} from '@e22m4u/js-trie-router';
|
7
20
|
import {HttpMethod} from '@e22m4u/js-trie-router';
|
21
|
+
import {ParsedQuery} from '@e22m4u/js-trie-router';
|
22
|
+
import {ParsedCookie} from '@e22m4u/js-trie-router';
|
23
|
+
import {ParsedParams} from '@e22m4u/js-trie-router';
|
24
|
+
import {ParsedHeaders} from '@e22m4u/js-trie-router';
|
8
25
|
import {RouteRegistry} from '@e22m4u/js-trie-router';
|
26
|
+
import {RequestParser} from '@e22m4u/js-trie-router';
|
27
|
+
import {RequestContext} from '@e22m4u/js-trie-router';
|
9
28
|
import {createRequestMock} from '@e22m4u/js-trie-router';
|
10
|
-
import {
|
29
|
+
import {createResponseMock} from '@e22m4u/js-trie-router';
|
11
30
|
import {ControllerRegistry} from './controller-registry.js';
|
12
31
|
|
13
32
|
const PRE_HANDLER_1 = () => undefined;
|
14
33
|
const PRE_HANDLER_2 = () => undefined;
|
15
|
-
const PRE_HANDLER_3 = () => undefined;
|
16
|
-
const PRE_HANDLER_4 = () => undefined;
|
17
34
|
|
18
35
|
const POST_HANDLER_1 = () => undefined;
|
19
36
|
const POST_HANDLER_2 = () => undefined;
|
20
|
-
const POST_HANDLER_3 = () => undefined;
|
21
|
-
const POST_HANDLER_4 = () => undefined;
|
22
37
|
|
23
38
|
describe('ControllerRegistry', function () {
|
24
39
|
it('has a public property with set of controllers', function () {
|
25
|
-
const
|
26
|
-
expect(
|
27
|
-
expect(
|
40
|
+
const S = new ControllerRegistry();
|
41
|
+
expect(S).to.have.property('controllers');
|
42
|
+
expect(S.controllers).to.be.instanceof(Set);
|
28
43
|
});
|
29
44
|
|
30
45
|
describe('addController', function () {
|
31
46
|
it('returns itself', function () {
|
32
|
-
const
|
47
|
+
const S = new ControllerRegistry();
|
33
48
|
@controller()
|
34
49
|
class MyController {}
|
35
|
-
const res =
|
36
|
-
expect(res).to.be.eq(
|
50
|
+
const res = S.addController(MyController);
|
51
|
+
expect(res).to.be.eq(S);
|
37
52
|
});
|
38
53
|
|
39
54
|
it('adds a given controller to controllers set', function () {
|
40
|
-
const
|
55
|
+
const S = new ControllerRegistry();
|
41
56
|
@controller()
|
42
57
|
class MyController {}
|
43
|
-
expect(
|
44
|
-
|
45
|
-
expect(
|
58
|
+
expect(S.hasController(MyController)).to.be.false;
|
59
|
+
S.addController(MyController);
|
60
|
+
expect(S.hasController(MyController)).to.be.true;
|
46
61
|
});
|
47
62
|
|
48
|
-
it('
|
49
|
-
const
|
63
|
+
it('uses http method and action path for a new route', function () {
|
64
|
+
const S = new ControllerRegistry();
|
50
65
|
@controller()
|
51
66
|
class MyController {
|
52
67
|
@get('/myAction')
|
53
68
|
foo() {}
|
54
69
|
}
|
55
|
-
|
56
|
-
const reg =
|
70
|
+
S.addController(MyController);
|
71
|
+
const reg = S.getService(TrieRouter).getService(RouteRegistry);
|
57
72
|
const req = createRequestMock({
|
58
73
|
method: HttpMethod.GET,
|
59
74
|
path: '/myAction',
|
@@ -64,8 +79,8 @@ describe('ControllerRegistry', function () {
|
|
64
79
|
expect(matching!.route.path).to.be.eq('/myAction');
|
65
80
|
});
|
66
81
|
|
67
|
-
it('
|
68
|
-
const
|
82
|
+
it('adds multiple routes by the given controller', function () {
|
83
|
+
const S = new ControllerRegistry();
|
69
84
|
@controller()
|
70
85
|
class MyController {
|
71
86
|
@get('/foo')
|
@@ -73,8 +88,8 @@ describe('ControllerRegistry', function () {
|
|
73
88
|
@post('/bar')
|
74
89
|
bar() {}
|
75
90
|
}
|
76
|
-
|
77
|
-
const routeReg =
|
91
|
+
S.addController(MyController);
|
92
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
78
93
|
const fooReq = createRequestMock({
|
79
94
|
method: HttpMethod.GET,
|
80
95
|
path: '/foo',
|
@@ -89,16 +104,107 @@ describe('ControllerRegistry', function () {
|
|
89
104
|
expect(barMatching).to.be.not.empty;
|
90
105
|
});
|
91
106
|
|
107
|
+
it('uses path prefix of controller root options', function () {
|
108
|
+
const S = new ControllerRegistry();
|
109
|
+
@controller()
|
110
|
+
class MyController {
|
111
|
+
@get('/myAction')
|
112
|
+
myAction() {}
|
113
|
+
}
|
114
|
+
S.addController(MyController, {pathPrefix: '/myPrefix'});
|
115
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
116
|
+
const req = createRequestMock({
|
117
|
+
method: HttpMethod.GET,
|
118
|
+
path: '/myPrefix/myAction',
|
119
|
+
});
|
120
|
+
const matching = routeReg.matchRouteByRequest(req);
|
121
|
+
expect(matching).to.be.not.empty;
|
122
|
+
});
|
123
|
+
|
124
|
+
it('uses path prefix of @controller metadata', function () {
|
125
|
+
const S = new ControllerRegistry();
|
126
|
+
@controller('/myController')
|
127
|
+
class MyController {
|
128
|
+
@get('/myAction')
|
129
|
+
myAction() {}
|
130
|
+
}
|
131
|
+
S.addController(MyController);
|
132
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
133
|
+
const req = createRequestMock({
|
134
|
+
method: HttpMethod.GET,
|
135
|
+
path: '/myController/myAction',
|
136
|
+
});
|
137
|
+
const matching = routeReg.matchRouteByRequest(req);
|
138
|
+
expect(matching).to.be.not.empty;
|
139
|
+
});
|
140
|
+
|
141
|
+
it('uses path prefix of controller root options and @controller metadata', function () {
|
142
|
+
const S = new ControllerRegistry();
|
143
|
+
@controller('/myController')
|
144
|
+
class MyController {
|
145
|
+
@get('/myAction')
|
146
|
+
myAction() {}
|
147
|
+
}
|
148
|
+
S.addController(MyController, {pathPrefix: '/myPrefix'});
|
149
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
150
|
+
const req = createRequestMock({
|
151
|
+
method: HttpMethod.GET,
|
152
|
+
path: '/myPrefix/myController/myAction',
|
153
|
+
});
|
154
|
+
const matching = routeReg.matchRouteByRequest(req);
|
155
|
+
expect(matching).to.be.not.empty;
|
156
|
+
});
|
157
|
+
|
92
158
|
describe('single pre-handler', function () {
|
93
|
-
it('
|
94
|
-
const
|
159
|
+
it('uses pre-handler of controller root options', function () {
|
160
|
+
const S = new ControllerRegistry();
|
161
|
+
@controller()
|
162
|
+
class MyController {
|
163
|
+
@get('/myAction')
|
164
|
+
myAction() {}
|
165
|
+
}
|
166
|
+
S.addController(MyController, {before: PRE_HANDLER_1});
|
167
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
168
|
+
const req = createRequestMock({
|
169
|
+
method: HttpMethod.GET,
|
170
|
+
path: '/myAction',
|
171
|
+
});
|
172
|
+
const matching = routeReg.matchRouteByRequest(req);
|
173
|
+
expect(matching).to.be.not.empty;
|
174
|
+
const res = matching!.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
|
175
|
+
expect(res).to.be.eql([PRE_HANDLER_1]);
|
176
|
+
});
|
177
|
+
|
178
|
+
it('uses pre-handler of @before metadata applied to controller', function () {
|
179
|
+
const S = new ControllerRegistry();
|
180
|
+
@controller()
|
181
|
+
@before(PRE_HANDLER_1)
|
182
|
+
class MyController {
|
183
|
+
@get('/myAction')
|
184
|
+
myAction() {}
|
185
|
+
}
|
186
|
+
S.addController(MyController);
|
187
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
188
|
+
const req = createRequestMock({
|
189
|
+
method: HttpMethod.GET,
|
190
|
+
path: '/myAction',
|
191
|
+
});
|
192
|
+
const matching = routeReg.matchRouteByRequest(req);
|
193
|
+
expect(matching).to.be.not.empty;
|
194
|
+
const res = matching!.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
|
195
|
+
expect(res).to.be.eql([PRE_HANDLER_1]);
|
196
|
+
});
|
197
|
+
|
198
|
+
it('uses pre-handler of @before metadata applied to action', function () {
|
199
|
+
const S = new ControllerRegistry();
|
95
200
|
@controller()
|
96
201
|
class MyController {
|
97
202
|
@get('/myAction')
|
203
|
+
@before(PRE_HANDLER_1)
|
98
204
|
myAction() {}
|
99
205
|
}
|
100
|
-
|
101
|
-
const routeReg =
|
206
|
+
S.addController(MyController);
|
207
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
102
208
|
const req = createRequestMock({
|
103
209
|
method: HttpMethod.GET,
|
104
210
|
path: '/myAction',
|
@@ -109,15 +215,15 @@ describe('ControllerRegistry', function () {
|
|
109
215
|
expect(res).to.be.eql([PRE_HANDLER_1]);
|
110
216
|
});
|
111
217
|
|
112
|
-
it('
|
113
|
-
const
|
218
|
+
it('uses pre-handler of @controller metadata', function () {
|
219
|
+
const S = new ControllerRegistry();
|
114
220
|
@controller({before: PRE_HANDLER_1})
|
115
221
|
class MyController {
|
116
222
|
@get('/myAction')
|
117
223
|
myAction() {}
|
118
224
|
}
|
119
|
-
|
120
|
-
const routeReg =
|
225
|
+
S.addController(MyController);
|
226
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
121
227
|
const req = createRequestMock({
|
122
228
|
method: HttpMethod.GET,
|
123
229
|
path: '/myAction',
|
@@ -128,15 +234,15 @@ describe('ControllerRegistry', function () {
|
|
128
234
|
expect(res).to.be.eql([PRE_HANDLER_1]);
|
129
235
|
});
|
130
236
|
|
131
|
-
it('
|
132
|
-
const
|
237
|
+
it('uses pre-handler of @action metadata', function () {
|
238
|
+
const S = new ControllerRegistry();
|
133
239
|
@controller()
|
134
240
|
class MyController {
|
135
241
|
@get('/myAction', {before: PRE_HANDLER_1})
|
136
242
|
myAction() {}
|
137
243
|
}
|
138
|
-
|
139
|
-
const routeReg =
|
244
|
+
S.addController(MyController);
|
245
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
140
246
|
const req = createRequestMock({
|
141
247
|
method: HttpMethod.GET,
|
142
248
|
path: '/myAction',
|
@@ -149,17 +255,57 @@ describe('ControllerRegistry', function () {
|
|
149
255
|
});
|
150
256
|
|
151
257
|
describe('multiple pre-handlers', function () {
|
152
|
-
it('
|
153
|
-
const
|
258
|
+
it('uses pre-handlers of controller root options', function () {
|
259
|
+
const S = new ControllerRegistry();
|
154
260
|
@controller()
|
155
261
|
class MyController {
|
156
262
|
@get('/myAction')
|
157
263
|
myAction() {}
|
158
264
|
}
|
159
|
-
|
265
|
+
S.addController(MyController, {
|
160
266
|
before: [PRE_HANDLER_1, PRE_HANDLER_2],
|
161
267
|
});
|
162
|
-
const routeReg =
|
268
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
269
|
+
const req = createRequestMock({
|
270
|
+
method: HttpMethod.GET,
|
271
|
+
path: '/myAction',
|
272
|
+
});
|
273
|
+
const matching = routeReg.matchRouteByRequest(req);
|
274
|
+
expect(matching).to.be.not.empty;
|
275
|
+
const res = matching!.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
|
276
|
+
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
277
|
+
});
|
278
|
+
|
279
|
+
it('uses pre-handlers of @before metadata applied to controller', function () {
|
280
|
+
const S = new ControllerRegistry();
|
281
|
+
@controller()
|
282
|
+
@before([PRE_HANDLER_1, PRE_HANDLER_2])
|
283
|
+
class MyController {
|
284
|
+
@get('/myAction')
|
285
|
+
myAction() {}
|
286
|
+
}
|
287
|
+
S.addController(MyController);
|
288
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
289
|
+
const req = createRequestMock({
|
290
|
+
method: HttpMethod.GET,
|
291
|
+
path: '/myAction',
|
292
|
+
});
|
293
|
+
const matching = routeReg.matchRouteByRequest(req);
|
294
|
+
expect(matching).to.be.not.empty;
|
295
|
+
const res = matching!.route.hookRegistry.getHooks(HookName.PRE_HANDLER);
|
296
|
+
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
297
|
+
});
|
298
|
+
|
299
|
+
it('uses pre-handlers of @before metadata applied to action', function () {
|
300
|
+
const S = new ControllerRegistry();
|
301
|
+
@controller()
|
302
|
+
class MyController {
|
303
|
+
@get('/myAction')
|
304
|
+
@before([PRE_HANDLER_1, PRE_HANDLER_2])
|
305
|
+
myAction() {}
|
306
|
+
}
|
307
|
+
S.addController(MyController);
|
308
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
163
309
|
const req = createRequestMock({
|
164
310
|
method: HttpMethod.GET,
|
165
311
|
path: '/myAction',
|
@@ -170,8 +316,8 @@ describe('ControllerRegistry', function () {
|
|
170
316
|
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
171
317
|
});
|
172
318
|
|
173
|
-
it('
|
174
|
-
const
|
319
|
+
it('uses pre-handlers of @controller metadata', function () {
|
320
|
+
const S = new ControllerRegistry();
|
175
321
|
@controller({
|
176
322
|
before: [PRE_HANDLER_1, PRE_HANDLER_2],
|
177
323
|
})
|
@@ -179,8 +325,8 @@ describe('ControllerRegistry', function () {
|
|
179
325
|
@get('/myAction')
|
180
326
|
myAction() {}
|
181
327
|
}
|
182
|
-
|
183
|
-
const routeReg =
|
328
|
+
S.addController(MyController);
|
329
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
184
330
|
const req = createRequestMock({
|
185
331
|
method: HttpMethod.GET,
|
186
332
|
path: '/myAction',
|
@@ -191,8 +337,8 @@ describe('ControllerRegistry', function () {
|
|
191
337
|
expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
|
192
338
|
});
|
193
339
|
|
194
|
-
it('
|
195
|
-
const
|
340
|
+
it('uses pre-handlers of @action metadata', function () {
|
341
|
+
const S = new ControllerRegistry();
|
196
342
|
@controller()
|
197
343
|
class MyController {
|
198
344
|
@get('/myAction', {
|
@@ -200,8 +346,8 @@ describe('ControllerRegistry', function () {
|
|
200
346
|
})
|
201
347
|
myAction() {}
|
202
348
|
}
|
203
|
-
|
204
|
-
const routeReg =
|
349
|
+
S.addController(MyController);
|
350
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
205
351
|
const req = createRequestMock({
|
206
352
|
method: HttpMethod.GET,
|
207
353
|
path: '/myAction',
|
@@ -214,15 +360,59 @@ describe('ControllerRegistry', function () {
|
|
214
360
|
});
|
215
361
|
|
216
362
|
describe('single post-handler', function () {
|
217
|
-
it('
|
218
|
-
const
|
363
|
+
it('uses post-handler of controller root options', function () {
|
364
|
+
const S = new ControllerRegistry();
|
365
|
+
@controller()
|
366
|
+
class MyController {
|
367
|
+
@get('/myAction')
|
368
|
+
myAction() {}
|
369
|
+
}
|
370
|
+
S.addController(MyController, {after: POST_HANDLER_1});
|
371
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
372
|
+
const req = createRequestMock({
|
373
|
+
method: HttpMethod.GET,
|
374
|
+
path: '/myAction',
|
375
|
+
});
|
376
|
+
const matching = routeReg.matchRouteByRequest(req);
|
377
|
+
expect(matching).to.be.not.empty;
|
378
|
+
const res = matching!.route.hookRegistry.getHooks(
|
379
|
+
HookName.POST_HANDLER,
|
380
|
+
);
|
381
|
+
expect(res).to.be.eql([POST_HANDLER_1]);
|
382
|
+
});
|
383
|
+
|
384
|
+
it('uses pre-handler of @after metadata applied to controller', function () {
|
385
|
+
const S = new ControllerRegistry();
|
386
|
+
@controller()
|
387
|
+
@after(POST_HANDLER_1)
|
388
|
+
class MyController {
|
389
|
+
@get('/myAction')
|
390
|
+
myAction() {}
|
391
|
+
}
|
392
|
+
S.addController(MyController);
|
393
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
394
|
+
const req = createRequestMock({
|
395
|
+
method: HttpMethod.GET,
|
396
|
+
path: '/myAction',
|
397
|
+
});
|
398
|
+
const matching = routeReg.matchRouteByRequest(req);
|
399
|
+
expect(matching).to.be.not.empty;
|
400
|
+
const res = matching!.route.hookRegistry.getHooks(
|
401
|
+
HookName.POST_HANDLER,
|
402
|
+
);
|
403
|
+
expect(res).to.be.eql([POST_HANDLER_1]);
|
404
|
+
});
|
405
|
+
|
406
|
+
it('uses pre-handler of @after metadata applied to action', function () {
|
407
|
+
const S = new ControllerRegistry();
|
219
408
|
@controller()
|
220
409
|
class MyController {
|
221
410
|
@get('/myAction')
|
411
|
+
@after(POST_HANDLER_1)
|
222
412
|
myAction() {}
|
223
413
|
}
|
224
|
-
|
225
|
-
const routeReg =
|
414
|
+
S.addController(MyController);
|
415
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
226
416
|
const req = createRequestMock({
|
227
417
|
method: HttpMethod.GET,
|
228
418
|
path: '/myAction',
|
@@ -235,15 +425,15 @@ describe('ControllerRegistry', function () {
|
|
235
425
|
expect(res).to.be.eql([POST_HANDLER_1]);
|
236
426
|
});
|
237
427
|
|
238
|
-
it('
|
239
|
-
const
|
428
|
+
it('uses post-handler of @controller metadata', function () {
|
429
|
+
const S = new ControllerRegistry();
|
240
430
|
@controller({after: POST_HANDLER_1})
|
241
431
|
class MyController {
|
242
432
|
@get('/myAction')
|
243
433
|
myAction() {}
|
244
434
|
}
|
245
|
-
|
246
|
-
const routeReg =
|
435
|
+
S.addController(MyController);
|
436
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
247
437
|
const req = createRequestMock({
|
248
438
|
method: HttpMethod.GET,
|
249
439
|
path: '/myAction',
|
@@ -256,15 +446,15 @@ describe('ControllerRegistry', function () {
|
|
256
446
|
expect(res).to.be.eql([POST_HANDLER_1]);
|
257
447
|
});
|
258
448
|
|
259
|
-
it('
|
260
|
-
const
|
449
|
+
it('uses post-handler of @action metadata', function () {
|
450
|
+
const S = new ControllerRegistry();
|
261
451
|
@controller()
|
262
452
|
class MyController {
|
263
453
|
@get('/myAction', {after: POST_HANDLER_1})
|
264
454
|
myAction() {}
|
265
455
|
}
|
266
|
-
|
267
|
-
const routeReg =
|
456
|
+
S.addController(MyController);
|
457
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
268
458
|
const req = createRequestMock({
|
269
459
|
method: HttpMethod.GET,
|
270
460
|
path: '/myAction',
|
@@ -279,17 +469,61 @@ describe('ControllerRegistry', function () {
|
|
279
469
|
});
|
280
470
|
|
281
471
|
describe('multiple post-handlers', function () {
|
282
|
-
it('
|
283
|
-
const
|
472
|
+
it('uses post-handlers of controller root options', function () {
|
473
|
+
const S = new ControllerRegistry();
|
284
474
|
@controller()
|
285
475
|
class MyController {
|
286
476
|
@get('/myAction')
|
287
477
|
myAction() {}
|
288
478
|
}
|
289
|
-
|
479
|
+
S.addController(MyController, {
|
290
480
|
after: [POST_HANDLER_1, POST_HANDLER_2],
|
291
481
|
});
|
292
|
-
const routeReg =
|
482
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
483
|
+
const req = createRequestMock({
|
484
|
+
method: HttpMethod.GET,
|
485
|
+
path: '/myAction',
|
486
|
+
});
|
487
|
+
const matching = routeReg.matchRouteByRequest(req);
|
488
|
+
expect(matching).to.be.not.empty;
|
489
|
+
const res = matching!.route.hookRegistry.getHooks(
|
490
|
+
HookName.POST_HANDLER,
|
491
|
+
);
|
492
|
+
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
493
|
+
});
|
494
|
+
|
495
|
+
it('uses pre-handlers of @after metadata applied to controller', function () {
|
496
|
+
const S = new ControllerRegistry();
|
497
|
+
@controller()
|
498
|
+
@after([POST_HANDLER_1, POST_HANDLER_2])
|
499
|
+
class MyController {
|
500
|
+
@get('/myAction')
|
501
|
+
myAction() {}
|
502
|
+
}
|
503
|
+
S.addController(MyController);
|
504
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
505
|
+
const req = createRequestMock({
|
506
|
+
method: HttpMethod.GET,
|
507
|
+
path: '/myAction',
|
508
|
+
});
|
509
|
+
const matching = routeReg.matchRouteByRequest(req);
|
510
|
+
expect(matching).to.be.not.empty;
|
511
|
+
const res = matching!.route.hookRegistry.getHooks(
|
512
|
+
HookName.POST_HANDLER,
|
513
|
+
);
|
514
|
+
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
515
|
+
});
|
516
|
+
|
517
|
+
it('uses pre-handlers of @after metadata applied to action', function () {
|
518
|
+
const S = new ControllerRegistry();
|
519
|
+
@controller()
|
520
|
+
class MyController {
|
521
|
+
@get('/myAction')
|
522
|
+
@after([POST_HANDLER_1, POST_HANDLER_2])
|
523
|
+
myAction() {}
|
524
|
+
}
|
525
|
+
S.addController(MyController);
|
526
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
293
527
|
const req = createRequestMock({
|
294
528
|
method: HttpMethod.GET,
|
295
529
|
path: '/myAction',
|
@@ -302,8 +536,8 @@ describe('ControllerRegistry', function () {
|
|
302
536
|
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
303
537
|
});
|
304
538
|
|
305
|
-
it('
|
306
|
-
const
|
539
|
+
it('uses post-handlers of @controller metadata', function () {
|
540
|
+
const S = new ControllerRegistry();
|
307
541
|
@controller({
|
308
542
|
after: [POST_HANDLER_1, POST_HANDLER_2],
|
309
543
|
})
|
@@ -311,8 +545,8 @@ describe('ControllerRegistry', function () {
|
|
311
545
|
@get('/myAction')
|
312
546
|
myAction() {}
|
313
547
|
}
|
314
|
-
|
315
|
-
const routeReg =
|
548
|
+
S.addController(MyController);
|
549
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
316
550
|
const req = createRequestMock({
|
317
551
|
method: HttpMethod.GET,
|
318
552
|
path: '/myAction',
|
@@ -325,8 +559,8 @@ describe('ControllerRegistry', function () {
|
|
325
559
|
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
326
560
|
});
|
327
561
|
|
328
|
-
it('
|
329
|
-
const
|
562
|
+
it('uses post-handlers of @action metadata', function () {
|
563
|
+
const S = new ControllerRegistry();
|
330
564
|
@controller()
|
331
565
|
class MyController {
|
332
566
|
@get('/myAction', {
|
@@ -334,8 +568,8 @@ describe('ControllerRegistry', function () {
|
|
334
568
|
})
|
335
569
|
myAction() {}
|
336
570
|
}
|
337
|
-
|
338
|
-
const routeReg =
|
571
|
+
S.addController(MyController);
|
572
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
339
573
|
const req = createRequestMock({
|
340
574
|
method: HttpMethod.GET,
|
341
575
|
path: '/myAction',
|
@@ -348,245 +582,333 @@ describe('ControllerRegistry', function () {
|
|
348
582
|
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
349
583
|
});
|
350
584
|
});
|
351
|
-
});
|
352
585
|
|
353
|
-
|
354
|
-
|
355
|
-
const
|
586
|
+
it('injects parsed parameters', async function () {
|
587
|
+
let checked = false;
|
588
|
+
const S = new ControllerRegistry();
|
356
589
|
@controller()
|
357
|
-
class MyController {
|
358
|
-
|
590
|
+
class MyController {
|
591
|
+
@get('/myAction/:id')
|
592
|
+
myAction(
|
593
|
+
@params()
|
594
|
+
params: ParsedParams,
|
595
|
+
) {
|
596
|
+
expect(params).to.be.eql({id: '123'});
|
597
|
+
checked = true;
|
598
|
+
}
|
599
|
+
}
|
600
|
+
S.addController(MyController);
|
601
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
602
|
+
const req = createRequestMock({
|
603
|
+
method: HttpMethod.GET,
|
604
|
+
path: '/myAction/123',
|
605
|
+
});
|
606
|
+
const matching = routeReg.matchRouteByRequest(req);
|
607
|
+
expect(matching).to.be.not.empty;
|
608
|
+
const res = createResponseMock();
|
609
|
+
const ctx = new RequestContext(S.container, req, res);
|
610
|
+
ctx.params = matching!.params;
|
611
|
+
await matching!.route.handler(ctx);
|
612
|
+
expect(checked).to.be.true;
|
359
613
|
});
|
360
614
|
|
361
|
-
it('
|
362
|
-
|
615
|
+
it('injects parsed parameter', async function () {
|
616
|
+
let checked = false;
|
617
|
+
const S = new ControllerRegistry();
|
363
618
|
@controller()
|
364
|
-
class MyController {
|
365
|
-
|
366
|
-
|
367
|
-
|
619
|
+
class MyController {
|
620
|
+
@get('/myAction/:id')
|
621
|
+
myAction(
|
622
|
+
@param('id')
|
623
|
+
param: string,
|
624
|
+
) {
|
625
|
+
expect(param).to.be.eq('123');
|
626
|
+
checked = true;
|
627
|
+
}
|
628
|
+
}
|
629
|
+
S.addController(MyController);
|
630
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
631
|
+
const req = createRequestMock({
|
632
|
+
method: HttpMethod.GET,
|
633
|
+
path: '/myAction/123',
|
634
|
+
});
|
635
|
+
const matching = routeReg.matchRouteByRequest(req);
|
636
|
+
expect(matching).to.be.not.empty;
|
637
|
+
const res = createResponseMock();
|
638
|
+
const ctx = new RequestContext(S.container, req, res);
|
639
|
+
ctx.params = matching!.params;
|
640
|
+
await matching!.route.handler(ctx);
|
641
|
+
expect(checked).to.be.true;
|
368
642
|
});
|
369
|
-
});
|
370
643
|
|
371
|
-
|
372
|
-
|
373
|
-
const
|
644
|
+
it('injects parsed queries', async function () {
|
645
|
+
let checked = false;
|
646
|
+
const S = new ControllerRegistry();
|
374
647
|
@controller()
|
375
|
-
class MyController {
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
const
|
387
|
-
|
648
|
+
class MyController {
|
649
|
+
@get('/myAction')
|
650
|
+
myAction(
|
651
|
+
@queries()
|
652
|
+
queries: ParsedQuery,
|
653
|
+
) {
|
654
|
+
expect(queries).to.be.eql({foo: 'bar'});
|
655
|
+
checked = true;
|
656
|
+
}
|
657
|
+
}
|
658
|
+
S.addController(MyController);
|
659
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
660
|
+
const req = createRequestMock({
|
661
|
+
method: HttpMethod.GET,
|
662
|
+
path: '/myAction?foo=bar',
|
663
|
+
});
|
664
|
+
const matching = routeReg.matchRouteByRequest(req);
|
665
|
+
expect(matching).to.be.not.empty;
|
666
|
+
const res = createResponseMock();
|
667
|
+
const ctx = new RequestContext(S.container, req, res);
|
668
|
+
const reqData = await S.getService(RequestParser).parse(req);
|
669
|
+
Object.assign(ctx, reqData);
|
670
|
+
await matching!.route.handler(ctx);
|
671
|
+
expect(checked).to.be.true;
|
388
672
|
});
|
389
673
|
|
390
|
-
it('
|
391
|
-
|
674
|
+
it('injects parsed query', async function () {
|
675
|
+
let checked = false;
|
676
|
+
const S = new ControllerRegistry();
|
392
677
|
@controller()
|
393
|
-
class MyController {
|
394
|
-
|
395
|
-
|
396
|
-
|
678
|
+
class MyController {
|
679
|
+
@get('/myAction')
|
680
|
+
myAction(
|
681
|
+
@query('foo')
|
682
|
+
foo: string,
|
683
|
+
) {
|
684
|
+
expect(foo).to.be.eql('bar');
|
685
|
+
checked = true;
|
686
|
+
}
|
687
|
+
}
|
688
|
+
S.addController(MyController);
|
689
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
690
|
+
const req = createRequestMock({
|
691
|
+
method: HttpMethod.GET,
|
692
|
+
path: '/myAction?foo=bar',
|
397
693
|
});
|
398
|
-
|
694
|
+
const matching = routeReg.matchRouteByRequest(req);
|
695
|
+
expect(matching).to.be.not.empty;
|
696
|
+
const res = createResponseMock();
|
697
|
+
const ctx = new RequestContext(S.container, req, res);
|
698
|
+
const reqData = await S.getService(RequestParser).parse(req);
|
699
|
+
Object.assign(ctx, reqData);
|
700
|
+
await matching!.route.handler(ctx);
|
701
|
+
expect(checked).to.be.true;
|
399
702
|
});
|
400
703
|
|
401
|
-
it('
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
704
|
+
it('injects parsed headers', async function () {
|
705
|
+
let checked = false;
|
706
|
+
const S = new ControllerRegistry();
|
707
|
+
@controller()
|
708
|
+
class MyController {
|
709
|
+
@get('/myAction')
|
710
|
+
myAction(
|
711
|
+
@headers()
|
712
|
+
headers: ParsedHeaders,
|
713
|
+
) {
|
714
|
+
expect(headers).to.be.eql({
|
715
|
+
host: 'localhost',
|
716
|
+
foo: 'bar',
|
717
|
+
});
|
718
|
+
checked = true;
|
719
|
+
}
|
720
|
+
}
|
721
|
+
S.addController(MyController);
|
722
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
723
|
+
const req = createRequestMock({
|
724
|
+
host: 'localhost',
|
725
|
+
method: HttpMethod.GET,
|
726
|
+
path: '/myAction',
|
727
|
+
headers: {foo: 'bar'},
|
408
728
|
});
|
409
|
-
|
729
|
+
const matching = routeReg.matchRouteByRequest(req);
|
730
|
+
expect(matching).to.be.not.empty;
|
731
|
+
const res = createResponseMock();
|
732
|
+
const ctx = new RequestContext(S.container, req, res);
|
733
|
+
const reqData = await S.getService(RequestParser).parse(req);
|
734
|
+
Object.assign(ctx, reqData);
|
735
|
+
await matching!.route.handler(ctx);
|
736
|
+
expect(checked).to.be.true;
|
410
737
|
});
|
411
|
-
});
|
412
738
|
|
413
|
-
|
414
|
-
|
415
|
-
const
|
739
|
+
it('injects parsed header', async function () {
|
740
|
+
let checked = false;
|
741
|
+
const S = new ControllerRegistry();
|
416
742
|
@controller()
|
417
|
-
class MyController {
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
});
|
434
|
-
|
435
|
-
it('returns pre-handlers from a given options', function () {
|
436
|
-
const s = new ControllerRegistry();
|
437
|
-
@controller()
|
438
|
-
class MyController {}
|
439
|
-
const md = ControllerReflector.getMetadata(MyController)!;
|
440
|
-
const res = s.getPreHandlersByControllerMetadata(md, {
|
441
|
-
before: PRE_HANDLER_1,
|
442
|
-
});
|
443
|
-
expect(res).to.be.eql([PRE_HANDLER_1]);
|
743
|
+
class MyController {
|
744
|
+
@get('/myAction')
|
745
|
+
myAction(
|
746
|
+
@header('foo')
|
747
|
+
foo: string,
|
748
|
+
) {
|
749
|
+
expect(foo).to.be.eq('bar');
|
750
|
+
checked = true;
|
751
|
+
}
|
752
|
+
}
|
753
|
+
S.addController(MyController);
|
754
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
755
|
+
const req = createRequestMock({
|
756
|
+
method: HttpMethod.GET,
|
757
|
+
path: '/myAction',
|
758
|
+
headers: {foo: 'bar'},
|
444
759
|
});
|
760
|
+
const matching = routeReg.matchRouteByRequest(req);
|
761
|
+
expect(matching).to.be.not.empty;
|
762
|
+
const res = createResponseMock();
|
763
|
+
const ctx = new RequestContext(S.container, req, res);
|
764
|
+
const reqData = await S.getService(RequestParser).parse(req);
|
765
|
+
Object.assign(ctx, reqData);
|
766
|
+
await matching!.route.handler(ctx);
|
767
|
+
expect(checked).to.be.true;
|
768
|
+
});
|
445
769
|
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
770
|
+
it('injects parsed cookies', async function () {
|
771
|
+
let checked = false;
|
772
|
+
const S = new ControllerRegistry();
|
773
|
+
@controller()
|
774
|
+
class MyController {
|
775
|
+
@get('/myAction')
|
776
|
+
myAction(
|
777
|
+
@cookies()
|
778
|
+
cookies: ParsedCookie,
|
779
|
+
) {
|
780
|
+
expect(cookies).to.be.eql({foo: 'bar'});
|
781
|
+
checked = true;
|
782
|
+
}
|
783
|
+
}
|
784
|
+
S.addController(MyController);
|
785
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
786
|
+
const req = createRequestMock({
|
787
|
+
method: HttpMethod.GET,
|
788
|
+
path: '/myAction',
|
789
|
+
headers: {cookie: 'foo=bar;'},
|
457
790
|
});
|
791
|
+
const matching = routeReg.matchRouteByRequest(req);
|
792
|
+
expect(matching).to.be.not.empty;
|
793
|
+
const res = createResponseMock();
|
794
|
+
const ctx = new RequestContext(S.container, req, res);
|
795
|
+
const reqData = await S.getService(RequestParser).parse(req);
|
796
|
+
Object.assign(ctx, reqData);
|
797
|
+
await matching!.route.handler(ctx);
|
798
|
+
expect(checked).to.be.true;
|
458
799
|
});
|
459
800
|
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
801
|
+
it('injects parsed cookie', async function () {
|
802
|
+
let checked = false;
|
803
|
+
const S = new ControllerRegistry();
|
804
|
+
@controller()
|
805
|
+
class MyController {
|
806
|
+
@get('/myAction')
|
807
|
+
myAction(
|
808
|
+
@cookie('foo')
|
809
|
+
foo: string,
|
810
|
+
) {
|
811
|
+
expect(foo).to.be.eq('bar');
|
812
|
+
checked = true;
|
813
|
+
}
|
814
|
+
}
|
815
|
+
S.addController(MyController);
|
816
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
817
|
+
const req = createRequestMock({
|
818
|
+
method: HttpMethod.GET,
|
819
|
+
path: '/myAction',
|
820
|
+
headers: {cookie: 'foo=bar;'},
|
470
821
|
});
|
822
|
+
const matching = routeReg.matchRouteByRequest(req);
|
823
|
+
expect(matching).to.be.not.empty;
|
824
|
+
const res = createResponseMock();
|
825
|
+
const ctx = new RequestContext(S.container, req, res);
|
826
|
+
const reqData = await S.getService(RequestParser).parse(req);
|
827
|
+
Object.assign(ctx, reqData);
|
828
|
+
await matching!.route.handler(ctx);
|
829
|
+
expect(checked).to.be.true;
|
830
|
+
});
|
471
831
|
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
832
|
+
it('injects parsed body', async function () {
|
833
|
+
let checked = false;
|
834
|
+
const S = new ControllerRegistry();
|
835
|
+
@controller()
|
836
|
+
class MyController {
|
837
|
+
@post('/myAction')
|
838
|
+
myAction(
|
839
|
+
@body()
|
840
|
+
body: object,
|
841
|
+
) {
|
842
|
+
expect(body).to.be.eql({foo: 'bar'});
|
843
|
+
checked = true;
|
844
|
+
}
|
845
|
+
}
|
846
|
+
S.addController(MyController);
|
847
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
848
|
+
const req = createRequestMock({
|
849
|
+
method: HttpMethod.POST,
|
850
|
+
path: '/myAction',
|
851
|
+
headers: {'content-type': 'application/json'},
|
852
|
+
body: '{"foo":"bar"}',
|
481
853
|
});
|
854
|
+
const matching = routeReg.matchRouteByRequest(req);
|
855
|
+
expect(matching).to.be.not.empty;
|
856
|
+
const res = createResponseMock();
|
857
|
+
const ctx = new RequestContext(S.container, req, res);
|
858
|
+
const reqData = await S.getService(RequestParser).parse(req);
|
859
|
+
Object.assign(ctx, reqData);
|
860
|
+
await matching!.route.handler(ctx);
|
861
|
+
expect(checked).to.be.true;
|
862
|
+
});
|
482
863
|
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
864
|
+
it('injects parsed body parameter', async function () {
|
865
|
+
let checked = false;
|
866
|
+
const S = new ControllerRegistry();
|
867
|
+
@controller()
|
868
|
+
class MyController {
|
869
|
+
@post('/myAction')
|
870
|
+
myAction(
|
871
|
+
@field('foo')
|
872
|
+
foo: object,
|
873
|
+
) {
|
874
|
+
expect(foo).to.be.eq('bar');
|
875
|
+
checked = true;
|
876
|
+
}
|
877
|
+
}
|
878
|
+
S.addController(MyController);
|
879
|
+
const routeReg = S.getService(TrieRouter).getService(RouteRegistry);
|
880
|
+
const req = createRequestMock({
|
881
|
+
method: HttpMethod.POST,
|
882
|
+
path: '/myAction',
|
883
|
+
headers: {'content-type': 'application/json'},
|
884
|
+
body: '{"foo":"bar"}',
|
499
885
|
});
|
886
|
+
const matching = routeReg.matchRouteByRequest(req);
|
887
|
+
expect(matching).to.be.not.empty;
|
888
|
+
const res = createResponseMock();
|
889
|
+
const ctx = new RequestContext(S.container, req, res);
|
890
|
+
const reqData = await S.getService(RequestParser).parse(req);
|
891
|
+
Object.assign(ctx, reqData);
|
892
|
+
await matching!.route.handler(ctx);
|
893
|
+
expect(checked).to.be.true;
|
500
894
|
});
|
501
895
|
});
|
502
896
|
|
503
|
-
describe('
|
504
|
-
it('returns
|
505
|
-
const
|
897
|
+
describe('hasController', function () {
|
898
|
+
it('returns false if a given controller is not registered', function () {
|
899
|
+
const S = new ControllerRegistry();
|
506
900
|
@controller()
|
507
901
|
class MyController {}
|
508
|
-
|
509
|
-
const res = s.getPostHandlersByControllerMetadata(md);
|
510
|
-
expect(res).to.be.eql([]);
|
511
|
-
});
|
512
|
-
|
513
|
-
describe('single handler', function () {
|
514
|
-
it('returns pre-handlers from a given controller', function () {
|
515
|
-
const s = new ControllerRegistry();
|
516
|
-
@controller({
|
517
|
-
after: POST_HANDLER_1,
|
518
|
-
})
|
519
|
-
class MyController {}
|
520
|
-
const md = ControllerReflector.getMetadata(MyController)!;
|
521
|
-
const res = s.getPostHandlersByControllerMetadata(md);
|
522
|
-
expect(res).to.be.eql([POST_HANDLER_1]);
|
523
|
-
});
|
524
|
-
|
525
|
-
it('returns pre-handlers from a given options', function () {
|
526
|
-
const s = new ControllerRegistry();
|
527
|
-
@controller()
|
528
|
-
class MyController {}
|
529
|
-
const md = ControllerReflector.getMetadata(MyController)!;
|
530
|
-
const res = s.getPostHandlersByControllerMetadata(md, {
|
531
|
-
after: POST_HANDLER_1,
|
532
|
-
});
|
533
|
-
expect(res).to.be.eql([POST_HANDLER_1]);
|
534
|
-
});
|
535
|
-
|
536
|
-
it('combines pre-handlers from a given options and controller', function () {
|
537
|
-
const s = new ControllerRegistry();
|
538
|
-
@controller({
|
539
|
-
after: POST_HANDLER_2,
|
540
|
-
})
|
541
|
-
class MyController {}
|
542
|
-
const md = ControllerReflector.getMetadata(MyController)!;
|
543
|
-
const res = s.getPostHandlersByControllerMetadata(md, {
|
544
|
-
after: POST_HANDLER_1,
|
545
|
-
});
|
546
|
-
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
547
|
-
});
|
902
|
+
expect(S.hasController(MyController)).to.be.false;
|
548
903
|
});
|
549
904
|
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
const md = ControllerReflector.getMetadata(MyController)!;
|
558
|
-
const res = s.getPostHandlersByControllerMetadata(md);
|
559
|
-
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
560
|
-
});
|
561
|
-
|
562
|
-
it('returns pre-handlers from a given options', function () {
|
563
|
-
const s = new ControllerRegistry();
|
564
|
-
@controller()
|
565
|
-
class MyController {}
|
566
|
-
const md = ControllerReflector.getMetadata(MyController)!;
|
567
|
-
const res = s.getPostHandlersByControllerMetadata(md, {
|
568
|
-
after: [POST_HANDLER_1, POST_HANDLER_2],
|
569
|
-
});
|
570
|
-
expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
|
571
|
-
});
|
572
|
-
|
573
|
-
it('combines pre-handlers from a given options and controller', function () {
|
574
|
-
const s = new ControllerRegistry();
|
575
|
-
@controller({
|
576
|
-
after: [POST_HANDLER_3, POST_HANDLER_4],
|
577
|
-
})
|
578
|
-
class MyController {}
|
579
|
-
const md = ControllerReflector.getMetadata(MyController)!;
|
580
|
-
const res = s.getPostHandlersByControllerMetadata(md, {
|
581
|
-
after: [POST_HANDLER_1, POST_HANDLER_2],
|
582
|
-
});
|
583
|
-
expect(res).to.be.eql([
|
584
|
-
POST_HANDLER_1,
|
585
|
-
POST_HANDLER_2,
|
586
|
-
POST_HANDLER_3,
|
587
|
-
POST_HANDLER_4,
|
588
|
-
]);
|
589
|
-
});
|
905
|
+
it('returns true if a given controller is registered', function () {
|
906
|
+
const S = new ControllerRegistry();
|
907
|
+
@controller()
|
908
|
+
class MyController {}
|
909
|
+
expect(S.hasController(MyController)).to.be.false;
|
910
|
+
S.addController(MyController);
|
911
|
+
expect(S.hasController(MyController)).to.be.true;
|
590
912
|
});
|
591
913
|
});
|
592
914
|
});
|