1
|
|
|
import { buildJsonResponse } from '../response'; |
2
|
|
|
|
3
|
|
|
const TestCases = [ |
4
|
|
|
{ |
5
|
|
|
description: 'Test a simple request', |
6
|
|
|
params: { body: {} }, |
7
|
|
|
specification: {}, |
8
|
|
|
expectedResult: { |
9
|
|
|
headers: { |
10
|
|
|
'Content-Type': 'application/json', |
11
|
|
|
'Cache-Control': 'no-store, max-age=0', |
12
|
|
|
}, |
13
|
|
|
statusCode: 200, |
14
|
|
|
body: {}, |
15
|
|
|
}, |
16
|
|
|
}, |
17
|
|
|
{ |
18
|
|
|
description: 'It should overwrite the status code', |
19
|
|
|
params: { |
20
|
|
|
statusCode: 204, |
21
|
|
|
body: {}, |
22
|
|
|
}, |
23
|
|
|
specification: { |
24
|
|
|
info: { |
25
|
|
|
version: '1.2.3', |
26
|
|
|
}, |
27
|
|
|
}, |
28
|
|
|
expectedResult: { |
29
|
|
|
headers: { |
30
|
|
|
'Content-Type': 'application/json', |
31
|
|
|
'Cache-Control': 'no-store, max-age=0', |
32
|
|
|
}, |
33
|
|
|
statusCode: 204, |
34
|
|
|
body: {}, |
35
|
|
|
}, |
36
|
|
|
}, |
37
|
|
|
{ |
38
|
|
|
description: 'It should add the extra header', |
39
|
|
|
params: { |
40
|
|
|
statusCode: 201, |
41
|
|
|
headers: { |
42
|
|
|
test: 'ok', |
43
|
|
|
}, |
44
|
|
|
body: {}, |
45
|
|
|
}, |
46
|
|
|
specification: {}, |
47
|
|
|
expectedResult: { |
48
|
|
|
headers: { |
49
|
|
|
'Content-Type': 'application/json', |
50
|
|
|
'Cache-Control': 'no-store, max-age=0', |
51
|
|
|
test: 'ok', |
52
|
|
|
}, |
53
|
|
|
statusCode: 201, |
54
|
|
|
body: {}, |
55
|
|
|
}, |
56
|
|
|
}, |
57
|
|
|
{ |
58
|
|
|
description: 'It should overwrite the default headers', |
59
|
|
|
params: { |
60
|
|
|
statusCode: 201, |
61
|
|
|
headers: { |
62
|
|
|
'Content-Type': 'text/html', |
63
|
|
|
'Cache-Control': 'private', |
64
|
|
|
test: 'ok', |
65
|
|
|
}, |
66
|
|
|
body: { |
67
|
|
|
example: 42, |
68
|
|
|
}, |
69
|
|
|
}, |
70
|
|
|
specification: {}, |
71
|
|
|
expectedResult: { |
72
|
|
|
headers: { |
73
|
|
|
'Content-Type': 'text/html', |
74
|
|
|
'Cache-Control': 'private', |
75
|
|
|
test: 'ok', |
76
|
|
|
}, |
77
|
|
|
statusCode: 201, |
78
|
|
|
body: { |
79
|
|
|
example: 42, |
80
|
|
|
}, |
81
|
|
|
}, |
82
|
|
|
}, |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
describe.each(TestCases)( |
|
|
|
|
86
|
|
|
'Response entity', |
87
|
|
|
({ description, params, specification, expectedResult }) => { |
88
|
|
|
it(description, () => { |
89
|
|
|
expect(buildJsonResponse(params, specification)).toEqual( |
90
|
|
|
expectedResult |
91
|
|
|
); |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
describe('Response without a specification', () => { |
97
|
|
|
it('It should generate a response body if no body is send', () => { |
98
|
|
|
const response = buildJsonResponse({}); |
99
|
|
|
expect(response.headers).toEqual({ |
100
|
|
|
'Content-Type': 'application/json', |
101
|
|
|
'Cache-Control': 'no-store, max-age=0', |
102
|
|
|
}); |
103
|
|
|
expect(response.statusCode).toEqual(200); |
104
|
|
|
expect(response.body.status).toEqual(true); |
105
|
|
|
expect(response.body.version).toEqual('unknown'); |
106
|
|
|
expect(response.body.message).toEqual('ok'); |
107
|
|
|
}); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
describe('Response with a specification', () => { |
111
|
|
|
it('It should generate a response body if no body is send', () => { |
112
|
|
|
const response = buildJsonResponse( |
113
|
|
|
{}, |
114
|
|
|
{ |
115
|
|
|
info: { |
116
|
|
|
version: '1.2.3', |
117
|
|
|
}, |
118
|
|
|
} |
119
|
|
|
); |
120
|
|
|
expect(response.headers).toEqual({ |
121
|
|
|
'Content-Type': 'application/json', |
122
|
|
|
'Cache-Control': 'no-store, max-age=0', |
123
|
|
|
}); |
124
|
|
|
expect(response.statusCode).toEqual(200); |
125
|
|
|
expect(response.body.status).toEqual(true); |
126
|
|
|
expect(response.body.version).toEqual('1.2.3'); |
127
|
|
|
expect(response.body.message).toEqual('ok'); |
128
|
|
|
}); |
129
|
|
|
}); |
130
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.