-
Notifications
You must be signed in to change notification settings - Fork 405
/
Copy pathinterceptors.spec.ts
130 lines (112 loc) · 4.63 KB
/
interceptors.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
import { Server as HttpServer } from 'http';
import HttpStatusCodes from 'http-status-codes';
import { Action } from '../../src/Action';
import { Controller } from '../../src/decorator/Controller';
import { Get } from '../../src/decorator/Get';
import { Interceptor } from '../../src/decorator/Interceptor';
import { UseInterceptor } from '../../src/decorator/UseInterceptor';
import { createExpressServer, getMetadataArgsStorage } from '../../src/index';
import { InterceptorInterface } from '../../src/InterceptorInterface';
import { axios } from '../utilities/axios';
import DoneCallback = jest.DoneCallback;
describe(``, () => {
let expressServer: HttpServer;
describe('interceptor', () => {
beforeAll((done: DoneCallback) => {
getMetadataArgsStorage().reset();
@Interceptor()
class NumbersInterceptor implements InterceptorInterface {
intercept(action: Action, result: any): any {
return result.replace(/[0-9]/gi, '');
}
}
class ByeWordInterceptor implements InterceptorInterface {
intercept(action: Action, result: any): any {
return result.replace(/bye/gi, 'hello');
}
}
class BadWordsInterceptor implements InterceptorInterface {
intercept(action: Action, result: any): any {
return result.replace(/damn/gi, '***');
}
}
class AsyncInterceptor implements InterceptorInterface {
intercept(action: Action, result: any): any {
return new Promise(ok => {
setTimeout(() => {
ok(result.replace(/hello/gi, 'bye'));
}, 1000);
});
}
}
@Controller()
@UseInterceptor(ByeWordInterceptor)
class HandledController {
@Get('/users')
@UseInterceptor((action: Action, result: any) => {
return result.replace(/hello/gi, 'hello world');
})
getUsers(): any {
return '<html><body>damn hello</body></html>';
}
@Get('/posts')
@UseInterceptor(BadWordsInterceptor)
posts(): any {
return '<html><body>this post contains damn bad words</body></html>';
}
@Get('/questions')
questions(): any {
return '<html><body>bye world</body></html>';
}
@Get('/files')
files(): any {
return '<html><body>hello1234567890 world</body></html>';
}
@Get('/photos')
@UseInterceptor(AsyncInterceptor)
photos(): any {
return '<html><body>hello world</body></html>';
}
}
expressServer = createExpressServer().listen(3001, done);
});
afterAll((done: DoneCallback) => {
expressServer.close(done);
});
it('custom interceptor function should replace returned content', async () => {
expect.assertions(3);
const response = await axios.get('/users');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>damn hello world</body></html>');
});
it('custom interceptor class should replace returned content', async () => {
expect.assertions(3);
const response = await axios.get('/posts');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>this post contains *** bad words</body></html>');
});
it('custom interceptor class used on the whole controller should replace returned content', async () => {
expect.assertions(3);
const response = await axios.get('/questions');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>hello world</body></html>');
});
it('global interceptor class should replace returned content', async () => {
expect.assertions(3);
const response = await axios.get('/files');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>hello world</body></html>');
});
it('interceptors should support promises', async () => {
expect.assertions(3);
const response = await axios.get('/photos');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>bye world</body></html>');
});
});
});