-
Notifications
You must be signed in to change notification settings - Fork 404
/
Copy pathcontroller-methods.spec.ts
358 lines (309 loc) · 13.1 KB
/
controller-methods.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
import { Server as HttpServer } from 'http';
import HttpStatusCodes from 'http-status-codes';
import { ContentType } from '../../src/decorator/ContentType';
import { Controller } from '../../src/decorator/Controller';
import { Delete } from '../../src/decorator/Delete';
import { Get } from '../../src/decorator/Get';
import { Head } from '../../src/decorator/Head';
import { JsonController } from '../../src/decorator/JsonController';
import { Method } from '../../src/decorator/Method';
import { Patch } from '../../src/decorator/Patch';
import { Post } from '../../src/decorator/Post';
import { Put } from '../../src/decorator/Put';
import { UnauthorizedError } from '../../src/http-error/UnauthorizedError';
import { createExpressServer, getMetadataArgsStorage } from '../../src/index';
import { axios } from '../utilities/axios';
import DoneCallback = jest.DoneCallback;
describe(``, () => {
let expressServer: HttpServer;
describe('controller methods', () => {
beforeAll((done: DoneCallback) => {
getMetadataArgsStorage().reset();
@Controller()
class UserController {
@Get('/users')
getAll(): string {
return '<html><body>All users</body></html>';
}
@Post('/users')
post(): string {
return '<html><body>Posting user</body></html>';
}
@Put('/users')
put(): string {
return '<html><body>Putting user</body></html>';
}
@Patch('/users')
patch(): string {
return '<html><body>Patching user</body></html>';
}
@Delete('/users')
delete(): string {
return '<html><body>Removing user</body></html>';
}
@Head('/users')
head(): string {
return '<html><body>Removing user</body></html>';
}
@Method('post', '/categories')
postCategories(): string {
return '<html><body>Posting categories</body></html>';
}
@Method('delete', '/categories')
getCategories(): string {
return '<html><body>Get categories</body></html>';
}
@Get('/users/:id')
getUserById(): string {
return '<html><body>One user</body></html>';
}
@Get(/\/categories\/[\d+]/)
getCategoryById(): string {
return '<html><body>One category</body></html>';
}
@Get('/posts/:id(\\d+)')
getPostById(): string {
return '<html><body>One post</body></html>';
}
@Get('/posts-from-db')
getPostFromDb(): Promise<string> {
return new Promise((ok, fail) => {
setTimeout(() => {
ok('<html><body>resolved after half second</body></html>');
}, 500);
});
}
@Get('/posts-from-failed-db')
getPostFromFailedDb(): Promise<string> {
return new Promise((ok, fail) => {
setTimeout(() => {
fail('<html><body>cannot connect to a database</body></html>');
}, 500);
});
}
}
@JsonController('/return/json')
class ReturnJsonController {
@Get('/undefined')
returnUndefined(): undefined {
return undefined;
}
@Get('/null')
returnNull(): null {
return null;
}
}
@Controller('/return/normal')
class ReturnNormalController {
@Get('/undefined')
returnUndefined(): undefined {
return undefined;
}
@Get('/null')
returnNull(): null {
return null;
}
}
@JsonController('/json-controller')
class ContentTypeController {
@Get('/text-html')
@ContentType('text/html')
returnHtml(): string {
return '<html>Test</html>';
}
@Get('/text-plain')
@ContentType('text/plain')
returnString(): string {
return 'Test';
}
@Get('/text-plain-error')
@ContentType('text/plain')
textError(): never {
throw new UnauthorizedError();
}
@Get('/json-error')
jsonError(): never {
throw new UnauthorizedError();
}
}
expressServer = createExpressServer().listen(3001, done);
});
afterAll((done: DoneCallback) => {
expressServer.close(done);
});
it('get should respond with proper status code, headers and body 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>All users</body></html>');
});
it('post respond with proper status code, headers and body content', async () => {
expect.assertions(3);
const response = await axios.post('/users');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>Posting user</body></html>');
});
it('put respond with proper status code, headers and body content', async () => {
expect.assertions(3);
const response = await axios.put('/users');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>Putting user</body></html>');
});
it('patch respond with proper status code, headers and body content', async () => {
expect.assertions(3);
const response = await axios.patch('/users');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>Patching user</body></html>');
});
it('delete respond with proper status code, headers and body content', async () => {
expect.assertions(3);
const response = await axios.delete('/users');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>Removing user</body></html>');
});
it('head respond with proper status code, headers and body content', async () => {
expect.assertions(3);
const response = await axios.head('/users');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('');
});
it('custom method (post) respond with proper status code, headers and body content', async () => {
expect.assertions(3);
const response = await axios.post('/categories');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>Posting categories</body></html>');
});
it('custom method (delete) respond with proper status code, headers and body content', async () => {
expect.assertions(3);
const response = await axios.delete('/categories');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>Get categories</body></html>');
});
it('route should work with parameter', async () => {
expect.assertions(3);
const response = await axios.get('/users/umed');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>One user</body></html>');
});
it('route should work with regexp parameter', async () => {
const response = await axios.get('/categories/1');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>One category</body></html>');
});
it('should respond with 404 when regexp does not match', async () => {
expect.assertions(1);
try {
await axios.get('/categories/umed');
} catch (error) {
expect(error.response.status).toEqual(HttpStatusCodes.NOT_FOUND);
}
});
it('route should work with string regexp parameter', async () => {
expect.assertions(3);
const response = await axios.get('/posts/1');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>One post</body></html>');
});
it('should respond with 404 when regexp does not match', async () => {
expect.assertions(1);
try {
await axios.get('/posts/U');
} catch (error) {
expect(error.response.status).toEqual(HttpStatusCodes.NOT_FOUND);
}
});
it('should return result from a promise', async () => {
expect.assertions(3);
const response = await axios.get('/posts-from-db');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>resolved after half second</body></html>');
});
it('should respond with 500 if promise failed', async () => {
expect.assertions(3);
try {
await axios.get('/posts-from-failed-db');
} catch (error) {
expect(error.response.status).toEqual(HttpStatusCodes.INTERNAL_SERVER_ERROR);
expect(error.response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(error.response.data).toEqual('<html><body>cannot connect to a database</body></html>');
}
});
it('should respond with 204 No Content when null returned in action', async () => {
expect.assertions(6);
let response = await axios.get('/return/normal/null');
expect(response.status).toEqual(HttpStatusCodes.NO_CONTENT);
expect(response.headers['content-type']).toBeUndefined();
expect(response.data).toEqual('');
response = await axios.get('/return/json/null');
expect(response.status).toEqual(HttpStatusCodes.NO_CONTENT);
expect(response.headers['content-type']).toBeUndefined();
expect(response.data).toEqual('');
});
it('should respond with 404 Not Found text when undefined returned in action', async () => {
expect.assertions(2);
try {
await axios.get('/return/normal/undefined');
} catch (error) {
expect(error.response.status).toEqual(HttpStatusCodes.NOT_FOUND);
expect(error.response.headers['content-type']).toEqual('text/html; charset=utf-8');
}
});
it('should respond with 404 Not Found JSON when undefined returned in action', async () => {
expect.assertions(2);
try {
await axios.get('/return/json/undefined');
} catch (error) {
expect(error.response.status).toEqual(HttpStatusCodes.NOT_FOUND);
expect(error.response.headers['content-type']).toEqual('application/json; charset=utf-8');
}
});
it("should respond with 200 and text/html even in json controller's method", async () => {
expect.assertions(3);
const response = await axios.get('/json-controller/text-html');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html>Test</html>');
});
it("should respond with 200 and text/plain even in json controller's method", async () => {
expect.assertions(3);
const response = await axios.get('/json-controller/text-plain');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/plain; charset=utf-8');
expect(response.data).toEqual('Test');
});
it("should respond with 401 and text/html when UnauthorizedError throwed even in json controller's method", async () => {
expect.assertions(4);
try {
await axios.get('/json-controller/text-plain-error');
} catch (error) {
expect(error.response.status).toEqual(HttpStatusCodes.UNAUTHORIZED);
expect(error.response.headers['content-type']).toEqual('text/plain; charset=utf-8');
expect(typeof error.response.data).toEqual('string');
expect(error.response.data).toMatch(/UnauthorizedError/);
}
});
it("should respond with 401 and aplication/json when UnauthorizedError is thrown in standard json controller's method", async () => {
expect.assertions(4);
try {
await axios.get('/json-controller/json-error');
} catch (error) {
expect(error.response.status).toEqual(HttpStatusCodes.UNAUTHORIZED);
expect(error.response.headers['content-type']).toEqual('application/json; charset=utf-8');
expect(typeof error.response.data).toEqual('object');
expect(error.response.data.name).toEqual('UnauthorizedError');
}
});
});
});