-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
middlewares-module.spec.ts
131 lines (104 loc) 路 4.78 KB
/
middlewares-module.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
import { expect } from 'chai';
import * as sinon from 'sinon';
import { NestMiddleware } from '../../../common/interfaces/middlewares/nest-middleware.interface';
import { Component } from '../../../common/utils/decorators/component.decorator';
import { MiddlewareBuilder } from '../../middlewares/builder';
import { MiddlewaresModule } from '../../middlewares/middlewares-module';
import { InvalidMiddlewareException } from '../../errors/exceptions/invalid-middleware.exception';
import { RequestMethod } from '../../../common/enums/request-method.enum';
import { Controller } from '../../../common/utils/decorators/controller.decorator';
import { RequestMapping } from '../../../common/utils/decorators/request-mapping.decorator';
import { RuntimeException } from '../../errors/exceptions/runtime.exception';
import { RoutesMapper } from '../../middlewares/routes-mapper';
import { RouterExceptionFilters } from '../../router/router-exception-filters';
import { ApplicationConfig } from '../../application-config';
describe('MiddlewaresModule', () => {
@Controller({ path: 'test' })
class AnotherRoute { }
@Controller({ path: 'test' })
class TestRoute {
@RequestMapping({ path: 'test' })
public getTest() {}
@RequestMapping({ path: 'another', method: RequestMethod.DELETE })
public getAnother() {}
}
@Component()
class TestMiddleware implements NestMiddleware {
public resolve() {
return (req, res, next) => {};
}
}
beforeEach(() => {
(MiddlewaresModule as any).routerExceptionFilter = new RouterExceptionFilters(
new ApplicationConfig(),
);
});
describe('loadConfiguration', () => {
it('should call "configure" method if method is implemented', () => {
const configureSpy = sinon.spy();
const mockModule = {
configure: configureSpy,
};
MiddlewaresModule.loadConfiguration(mockModule as any, 'Test' as any);
expect(configureSpy.calledOnce).to.be.true;
expect(configureSpy.calledWith(new MiddlewareBuilder(new RoutesMapper()))).to.be.true;
});
});
describe('setupRouteMiddleware', () => {
it('should throw "RuntimeException" exception when middlewares is not stored in container', () => {
const route = { path: 'Test' };
const configuration = {
middlewares: [ TestMiddleware ],
forRoutes: [ TestRoute ]
};
const useSpy = sinon.spy();
const app = { use: useSpy };
expect(MiddlewaresModule.setupRouteMiddleware.bind(
MiddlewaresModule, route, configuration, 'Test' as any, app as any,
)).throws(RuntimeException);
});
it('should throw "InvalidMiddlewareException" exception when middlewares does not have "resolve" method', () => {
@Component()
class InvalidMiddleware {}
const route = { path: 'Test' };
const configuration = {
middlewares: [ InvalidMiddleware ],
forRoutes: [ TestRoute ],
};
const useSpy = sinon.spy();
const app = { use: useSpy };
const container = MiddlewaresModule.getContainer();
const moduleKey = 'Test' as any;
container.addConfig([ configuration as any ], moduleKey);
const instance = new InvalidMiddleware();
container.getMiddlewares(moduleKey).set('InvalidMiddleware', {
metatype: InvalidMiddleware,
instance,
} as any);
expect(MiddlewaresModule.setupRouteMiddleware.bind(
MiddlewaresModule, route, configuration, moduleKey, app as any,
)).throws(InvalidMiddlewareException);
});
it('should store middlewares when middleware is stored in container', () => {
const route = { path: 'Test', method: RequestMethod.GET };
const configuration = {
middlewares: [ TestMiddleware ],
forRoutes: [ { path: 'test' }, AnotherRoute, TestRoute ],
};
const useSpy = sinon.spy();
const app = {
get: useSpy,
};
const container = MiddlewaresModule.getContainer();
const moduleKey = 'Test' as any;
container.addConfig([ configuration ], moduleKey);
const instance = new TestMiddleware();
container.getMiddlewares(moduleKey).set('TestMiddleware', {
metatype: TestMiddleware,
instance,
});
MiddlewaresModule.setupRouteMiddleware(route, configuration, moduleKey, app as any);
expect(useSpy.calledOnce).to.be.true;
});
});
});