1
|
|
|
import { expect, describe, it } from '@jest/globals'; |
2
|
|
|
import { buildResponse } from '../response.js'; |
3
|
|
|
|
4
|
|
|
const TestCases = [ |
5
|
|
|
{ |
6
|
|
|
description: 'Test a simple request', |
7
|
|
|
params: { body: {} }, |
8
|
|
|
specification: {}, |
9
|
|
|
expectedResult: { |
10
|
|
|
headers: { |
11
|
|
|
'Content-Type': 'application/json', |
12
|
|
|
'Cache-Control': 'no-store, max-age=0', |
13
|
|
|
}, |
14
|
|
|
statusCode: 200, |
15
|
|
|
body: {}, |
16
|
|
|
attachment: false, |
17
|
|
|
}, |
18
|
|
|
}, |
19
|
|
|
{ |
20
|
|
|
description: 'It should overwrite the status code', |
21
|
|
|
params: { |
22
|
|
|
statusCode: 204, |
23
|
|
|
body: {}, |
24
|
|
|
}, |
25
|
|
|
specification: { |
26
|
|
|
info: { |
27
|
|
|
version: '1.2.3', |
28
|
|
|
}, |
29
|
|
|
}, |
30
|
|
|
expectedResult: { |
31
|
|
|
headers: { |
32
|
|
|
'Content-Type': 'application/json', |
33
|
|
|
'Cache-Control': 'no-store, max-age=0', |
34
|
|
|
}, |
35
|
|
|
statusCode: 204, |
36
|
|
|
body: {}, |
37
|
|
|
attachment: false, |
38
|
|
|
}, |
39
|
|
|
}, |
40
|
|
|
{ |
41
|
|
|
description: 'It should add the extra header', |
42
|
|
|
params: { |
43
|
|
|
statusCode: 201, |
44
|
|
|
headers: { |
45
|
|
|
test: 'ok', |
46
|
|
|
}, |
47
|
|
|
body: {}, |
48
|
|
|
}, |
49
|
|
|
specification: {}, |
50
|
|
|
expectedResult: { |
51
|
|
|
headers: { |
52
|
|
|
'Content-Type': 'application/json', |
53
|
|
|
'Cache-Control': 'no-store, max-age=0', |
54
|
|
|
test: 'ok', |
55
|
|
|
}, |
56
|
|
|
statusCode: 201, |
57
|
|
|
body: {}, |
58
|
|
|
attachment: false, |
59
|
|
|
}, |
60
|
|
|
}, |
61
|
|
|
{ |
62
|
|
|
description: 'It should overwrite the default headers', |
63
|
|
|
params: { |
64
|
|
|
statusCode: 201, |
65
|
|
|
headers: { |
66
|
|
|
'Content-Type': 'text/csv', |
67
|
|
|
'Cache-Control': 'private', |
68
|
|
|
test: 'ok', |
69
|
|
|
}, |
70
|
|
|
body: { |
71
|
|
|
example: 42, |
72
|
|
|
}, |
73
|
|
|
attachment: true, |
74
|
|
|
}, |
75
|
|
|
specification: {}, |
76
|
|
|
expectedResult: { |
77
|
|
|
headers: { |
78
|
|
|
'Content-Type': 'text/csv', |
79
|
|
|
'Cache-Control': 'private', |
80
|
|
|
test: 'ok', |
81
|
|
|
}, |
82
|
|
|
statusCode: 201, |
83
|
|
|
body: { |
84
|
|
|
example: 42, |
85
|
|
|
}, |
86
|
|
|
attachment: true, |
87
|
|
|
}, |
88
|
|
|
}, |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
describe.each(TestCases)( |
92
|
|
|
'Response entity', |
93
|
|
|
({ description, params, specification, expectedResult }) => { |
94
|
|
|
it(description, () => { |
95
|
|
|
expect(buildResponse(params, specification)).toEqual( |
96
|
|
|
expectedResult |
97
|
|
|
); |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
describe('Response without a specification', () => { |
103
|
|
|
it('It should generate a response body if no body is send', () => { |
104
|
|
|
const response = buildResponse({ |
105
|
|
|
statusCode: 418, |
106
|
|
|
}); |
107
|
|
|
expect(response.headers).toEqual({ |
108
|
|
|
'Content-Type': 'application/json', |
109
|
|
|
'Cache-Control': 'no-store, max-age=0', |
110
|
|
|
}); |
111
|
|
|
expect(response.statusCode).toEqual(418); |
112
|
|
|
expect(response.body.status).toEqual(true); |
113
|
|
|
expect(response.body.version).toEqual('unknown'); |
114
|
|
|
expect(response.body.message).toEqual('ok'); |
115
|
|
|
}); |
116
|
|
|
}); |
117
|
|
|
|
118
|
|
|
describe('Response with a specification', () => { |
119
|
|
|
it('It shouldnt generate a response body if no body is send for status < 400', () => { |
120
|
|
|
const response = buildResponse( |
121
|
|
|
{ |
122
|
|
|
statusCode: 201, |
123
|
|
|
}, |
124
|
|
|
{ |
125
|
|
|
info: { |
126
|
|
|
version: '1.2.3', |
127
|
|
|
}, |
128
|
|
|
} |
129
|
|
|
); |
130
|
|
|
expect(response.headers).toEqual({ |
131
|
|
|
'Content-Type': 'application/json', |
132
|
|
|
'Cache-Control': 'no-store, max-age=0', |
133
|
|
|
}); |
134
|
|
|
expect(response.statusCode).toEqual(201); |
135
|
|
|
expect(response.body).toEqual(null); |
136
|
|
|
}); |
137
|
|
|
|
138
|
|
|
it('It should generate a response body if no body is send', () => { |
139
|
|
|
const response = buildResponse( |
140
|
|
|
{ |
141
|
|
|
statusCode: 418, |
142
|
|
|
}, |
143
|
|
|
{ |
144
|
|
|
info: { |
145
|
|
|
version: '1.2.3', |
146
|
|
|
}, |
147
|
|
|
} |
148
|
|
|
); |
149
|
|
|
expect(response.headers).toEqual({ |
150
|
|
|
'Content-Type': 'application/json', |
151
|
|
|
'Cache-Control': 'no-store, max-age=0', |
152
|
|
|
}); |
153
|
|
|
expect(response.statusCode).toEqual(418); |
154
|
|
|
expect(response.body?.status).toEqual(true); |
155
|
|
|
expect(response.body?.version).toEqual('1.2.3'); |
156
|
|
|
expect(response.body?.message).toEqual('ok'); |
157
|
|
|
}); |
158
|
|
|
}); |
159
|
|
|
|