-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
Copy pathcontent_type_checker.test.ts
96 lines (88 loc) · 3.16 KB
/
content_type_checker.test.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
import type { Request, Response } from 'express';
import requireContentType from './content_type_checker';
const mockRequest: (contentType: string) => Request = (contentType) => ({
// @ts-ignore
header: (name) => {
if (name === 'Content-Type') {
return contentType;
}
return '';
},
});
const returns415: (t: jest.Mock) => Response = (t) => ({
// @ts-ignore
status: (code) => {
expect(415).toBe(code);
return {
json: () => ({
// @ts-ignore
end: t,
}),
};
},
});
const expectNoCall: (t: jest.Mock) => Response = (t) => ({
// @ts-ignore
status: () => ({
// @ts-ignore
json: () => ({
// @ts-ignore
end: () => expect(t).toHaveBeenCalledTimes(0),
}),
}),
});
test('Content-type middleware should by default only support application/json', () => {
const middleware = requireContentType();
const t = jest.fn();
const fail = jest.fn();
middleware(mockRequest('application/json'), expectNoCall(fail), t);
middleware(mockRequest('text/plain'), returns415(t), fail);
expect(t).toHaveBeenCalledTimes(2);
expect(fail).toHaveBeenCalledTimes(0);
});
test('Content-type middleware should by default only support application/json with charset', () => {
const middleware = requireContentType();
const t = jest.fn();
const fail = jest.fn();
middleware(
mockRequest('application/json; charset=UTF-8'),
expectNoCall(fail),
t,
);
middleware(mockRequest('text/plain'), returns415(t), fail);
expect(t).toHaveBeenCalledTimes(2);
expect(fail).toHaveBeenCalledTimes(0);
});
test('Content-type middleware should allow adding custom supported types', () => {
const middleware = requireContentType('application/yaml');
const t = jest.fn();
const fail = jest.fn();
middleware(mockRequest('application/yaml'), expectNoCall(fail), t);
middleware(mockRequest('text/html'), returns415(t), fail);
middleware(mockRequest('text/plain'), returns415(t), fail);
expect(t).toHaveBeenCalledTimes(3);
expect(fail).toHaveBeenCalledTimes(0);
});
test('adding custom supported types no longer supports default type', () => {
const middleware = requireContentType('application/yaml');
const t = jest.fn();
const fail = jest.fn();
middleware(mockRequest('application/json'), returns415(t), fail);
expect(t).toHaveBeenCalledTimes(1);
expect(fail).toHaveBeenCalledTimes(0);
});
test('Should be able to add multiple content-types supported', () => {
const middleware = requireContentType(
'application/json',
'application/yaml',
'form/multipart',
);
const fail = jest.fn();
const succeed = jest.fn();
middleware(mockRequest('application/json'), expectNoCall(fail), succeed);
middleware(mockRequest('application/yaml'), expectNoCall(fail), succeed);
middleware(mockRequest('form/multipart'), expectNoCall(fail), succeed);
middleware(mockRequest('text/plain'), returns415(succeed), fail);
expect(succeed).toHaveBeenCalledTimes(4);
expect(fail).toHaveBeenCalledTimes(0);
});