forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router-explorer.spec.ts
408 lines (342 loc) · 12.8 KB
/
router-explorer.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import { expect } from 'chai';
import * as sinon from 'sinon';
import { Controller } from '../../../common/decorators/core/controller.decorator';
import {
All,
Get,
Post,
} from '../../../common/decorators/http/request-mapping.decorator';
import { RequestMethod } from '../../../common/enums/request-method.enum';
import { VersioningType } from '../../../common/enums/version-type.enum';
import { Injector } from '../../../core/injector/injector';
import { ApplicationConfig } from '../../application-config';
import { ExecutionContextHost } from '../../helpers/execution-context-host';
import { NestContainer } from '../../injector/container';
import { InstanceWrapper } from '../../injector/instance-wrapper';
import { MetadataScanner } from '../../metadata-scanner';
import { RoutePathMetadata } from '../../router/interfaces/route-path-metadata.interface';
import { RoutePathFactory } from '../../router/route-path-factory';
import { RouterExceptionFilters } from '../../router/router-exception-filters';
import { RouterExplorer } from '../../router/router-explorer';
import { NoopHttpAdapter } from '../utils/noop-adapter.spec';
import { UnknownRequestMappingException } from '../../errors/exceptions/unknown-request-mapping.exception';
describe('RouterExplorer', () => {
@Controller('global')
class TestRoute {
@Get('test')
public getTest() {}
@Post('test')
public postTest() {}
@All('another-test')
public anotherTest() {}
@Get(['foo', 'bar'])
public getTestUsingArray() {}
}
@Controller(['global', 'global-alias'])
class TestRouteAlias {
@Get('test')
public getTest() {}
@Post('test')
public postTest() {}
@All('another-test')
public anotherTest() {}
@Get(['foo', 'bar'])
public getTestUsingArray() {}
}
class ClassWithMissingControllerDecorator {}
let routerBuilder: RouterExplorer;
let injector: Injector;
let exceptionsFilter: RouterExceptionFilters;
let applicationConfig: ApplicationConfig;
let routePathFactory: RoutePathFactory;
beforeEach(() => {
const container = new NestContainer();
applicationConfig = new ApplicationConfig();
injector = new Injector();
routePathFactory = new RoutePathFactory(applicationConfig);
exceptionsFilter = new RouterExceptionFilters(
container,
applicationConfig,
null,
);
routerBuilder = new RouterExplorer(
new MetadataScanner(),
container,
injector,
null,
exceptionsFilter,
applicationConfig,
routePathFactory,
);
});
describe('scanForPaths', () => {
it('should method return expected list of route paths', () => {
const paths = routerBuilder.scanForPaths(new TestRoute());
expect(paths).to.have.length(4);
expect(paths[0].path).to.eql(['/test']);
expect(paths[1].path).to.eql(['/test']);
expect(paths[2].path).to.eql(['/another-test']);
expect(paths[3].path).to.eql(['/foo', '/bar']);
expect(paths[0].requestMethod).to.eql(RequestMethod.GET);
expect(paths[1].requestMethod).to.eql(RequestMethod.POST);
expect(paths[2].requestMethod).to.eql(RequestMethod.ALL);
expect(paths[3].requestMethod).to.eql(RequestMethod.GET);
});
it('should method return expected list of route paths alias', () => {
const paths = routerBuilder.scanForPaths(new TestRouteAlias());
expect(paths).to.have.length(4);
expect(paths[0].path).to.eql(['/test']);
expect(paths[1].path).to.eql(['/test']);
expect(paths[2].path).to.eql(['/another-test']);
expect(paths[3].path).to.eql(['/foo', '/bar']);
expect(paths[0].requestMethod).to.eql(RequestMethod.GET);
expect(paths[1].requestMethod).to.eql(RequestMethod.POST);
expect(paths[2].requestMethod).to.eql(RequestMethod.ALL);
expect(paths[3].requestMethod).to.eql(RequestMethod.GET);
});
});
describe('exploreMethodMetadata', () => {
it('should method return expected object which represent single route', () => {
const instance = new TestRoute();
const instanceProto = Object.getPrototypeOf(instance);
const route = routerBuilder.exploreMethodMetadata(
instance,
instanceProto,
'getTest',
);
expect(route.path).to.eql(['/test']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
expect(route.targetCallback).to.eq(instance.getTest);
});
it('should method return expected object which represent single route with alias', () => {
const instance = new TestRouteAlias();
const instanceProto = Object.getPrototypeOf(instance);
const route = routerBuilder.exploreMethodMetadata(
instance,
instanceProto,
'getTest',
);
expect(route.path).to.eql(['/test']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
expect(route.targetCallback).to.eq(instance.getTest);
});
it('should method return expected object which represent multiple routes', () => {
const instance = new TestRoute();
const instanceProto = Object.getPrototypeOf(instance);
const route = routerBuilder.exploreMethodMetadata(
instance,
instanceProto,
'getTestUsingArray',
);
expect(route.path).to.eql(['/foo', '/bar']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
expect(route.targetCallback).to.eq(instance.getTestUsingArray);
});
it('should method return expected object which represent multiple routes with alias', () => {
const instance = new TestRouteAlias();
const instanceProto = Object.getPrototypeOf(instance);
const route = routerBuilder.exploreMethodMetadata(
instance,
instanceProto,
'getTestUsingArray',
);
expect(route.path).to.eql(['/foo', '/bar']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
expect(route.targetCallback).to.eq(instance.getTestUsingArray);
});
describe('when new implementation is injected into router', () => {
it('should method return changed impl of single route', () => {
const instance = new TestRoute();
const instanceProto = Object.getPrototypeOf(instance);
const newImpl = function () {};
instance.getTest = newImpl;
const route = routerBuilder.exploreMethodMetadata(
instance,
instanceProto,
'getTest',
);
expect(route.targetCallback).to.eq(newImpl);
expect(route.path).to.eql(['/test']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
});
it('should method return changed impl of single route which alias applied', () => {
const instance = new TestRouteAlias();
const instanceProto = Object.getPrototypeOf(instance);
const newImpl = function () {};
instance.getTest = newImpl;
const route = routerBuilder.exploreMethodMetadata(
instance,
instanceProto,
'getTest',
);
expect(route.targetCallback).to.eq(newImpl);
expect(route.path).to.eql(['/test']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
});
it('should method return changed impl of multiple routes', () => {
const instance = new TestRoute();
const instanceProto = Object.getPrototypeOf(instance);
const newImpl = function () {};
instance.getTestUsingArray = newImpl;
const route = routerBuilder.exploreMethodMetadata(
instance,
instanceProto,
'getTestUsingArray',
);
expect(route.targetCallback).to.eq(newImpl);
expect(route.path).to.eql(['/foo', '/bar']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
});
it('should method return changed impl of multiple routes which alias applied', () => {
const instance = new TestRouteAlias();
const instanceProto = Object.getPrototypeOf(instance);
const newImpl = function () {};
instance.getTestUsingArray = newImpl;
const route = routerBuilder.exploreMethodMetadata(
instance,
instanceProto,
'getTestUsingArray',
);
expect(route.targetCallback).to.eq(newImpl);
expect(route.path).to.eql(['/foo', '/bar']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
});
});
});
describe('applyPathsToRouterProxy', () => {
it('should method return expected object which represent single route', () => {
const bindStub = sinon.stub(
routerBuilder,
'applyCallbackToRouter' as any,
);
const paths = [
{ path: [''], requestMethod: RequestMethod.GET },
{ path: ['test'], requestMethod: RequestMethod.GET },
{ path: ['foo', 'bar'], requestMethod: RequestMethod.GET },
];
routerBuilder.applyPathsToRouterProxy(
null,
paths as any,
null,
'',
{},
'',
);
expect(bindStub.calledWith(null, paths[0], null)).to.be.true;
expect(bindStub.callCount).to.be.eql(paths.length);
});
it('should method return expected object which represents a single versioned route', () => {
const bindStub = sinon.stub(
routerBuilder,
'applyCallbackToRouter' as any,
);
const paths = [
{ path: [''], requestMethod: RequestMethod.GET },
{ path: ['test'], requestMethod: RequestMethod.GET },
{ path: ['foo', 'bar'], requestMethod: RequestMethod.GET },
];
const routePathMetadata: RoutePathMetadata = {
versioningOptions: { type: VersioningType.URI },
};
routerBuilder.applyPathsToRouterProxy(
null,
paths as any,
null,
'',
routePathMetadata,
'1',
);
expect(
bindStub.calledWith(null, paths[0], null, '', routePathMetadata, '1'),
).to.be.true;
expect(bindStub.callCount).to.be.eql(paths.length);
});
});
describe('extractRouterPath', () => {
it('should return expected path', () => {
expect(routerBuilder.extractRouterPath(TestRoute)).to.be.eql(['/global']);
});
it('should return expected path with alias', () => {
expect(routerBuilder.extractRouterPath(TestRouteAlias)).to.be.eql([
'/global',
'/global-alias',
]);
});
it("should throw UnknownRequestMappingException when missing the `@Controller()` decorator in the class, displaying class's name", () => {
expect(() =>
routerBuilder.extractRouterPath(ClassWithMissingControllerDecorator),
).to.throw(
UnknownRequestMappingException,
/ClassWithMissingControllerDecorator/,
);
});
});
describe('createRequestScopedHandler', () => {
let nextSpy: sinon.SinonSpy;
beforeEach(() => {
sinon.stub(injector, 'loadPerContext').callsFake(() => {
throw new Error();
});
nextSpy = sinon.spy();
sinon.stub(exceptionsFilter, 'create').callsFake(
() =>
({
next: nextSpy,
} as any),
);
});
describe('when "loadPerContext" throws', () => {
const moduleKey = 'moduleKey';
const methodKey = 'methodKey';
const module = {
controllers: new Map(),
} as any;
const wrapper = new InstanceWrapper({
instance: { [methodKey]: {} },
});
it('should delegete error to exception filters', async () => {
const handler = routerBuilder.createRequestScopedHandler(
wrapper,
RequestMethod.ALL,
module,
moduleKey,
methodKey,
);
await handler(null, null, null);
expect(nextSpy.called).to.be.true;
expect(nextSpy.getCall(0).args[0]).to.be.instanceOf(Error);
expect(nextSpy.getCall(0).args[1]).to.be.instanceOf(
ExecutionContextHost,
);
});
});
});
describe('applyVersionFilter', () => {
it('should call and return the `applyVersionFilter` from the underlying http server', () => {
const router = sinon.spy(new NoopHttpAdapter({}));
const routePathMetadata: RoutePathMetadata = {
methodVersion:
sinon.fake() as unknown as RoutePathMetadata['methodVersion'],
versioningOptions:
sinon.fake() as unknown as RoutePathMetadata['versioningOptions'],
};
const handler = sinon.stub();
// We're using type assertion here because `applyVersionFilter` is private
const versionFilter = (routerBuilder as any).applyVersionFilter(
router,
routePathMetadata,
handler,
);
expect(
router.applyVersionFilter.calledOnceWithExactly(
handler,
routePathMetadata.methodVersion,
routePathMetadata.versioningOptions,
),
).to.be.true;
expect(router.applyVersionFilter.returnValues[0]).to.be.equal(
versionFilter,
);
});
});
});