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