-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
utils.test.ts
188 lines (168 loc) · 5.16 KB
/
utils.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
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
import OpenAPIUtils from './utils';
describe('OpenAPIUtils', () => {
describe('.findStatusCodeMatch', () => {
test('mismatches', async () => {
const value = OpenAPIUtils.findStatusCodeMatch(302, {
'200': 'OK',
'201': 'Created',
});
expect(value).toEqual(undefined);
});
test('matches 200', async () => {
const value = OpenAPIUtils.findStatusCodeMatch(200, {
'200': 'OK',
'201': 'Created',
});
expect(value).toEqual('OK');
});
test('matches 201', async () => {
const value = OpenAPIUtils.findStatusCodeMatch(201, {
'200': 'OK',
'201': 'Created',
});
expect(value).toEqual('Created');
});
test('matches 404', async () => {
const value = OpenAPIUtils.findStatusCodeMatch(404, {
'200': 'OK',
'404': 'Not Found',
'201': 'Created',
});
expect(value).toEqual('Not Found');
});
test('matches 500 (not string)', async () => {
const value = OpenAPIUtils.findStatusCodeMatch(500, {
'200': 'OK',
'500': ['a', { test: 'it works' }, 'b'],
'201': 'Created',
});
expect(value[1].test).toEqual('it works');
});
test('matches 500 (null)', async () => {
const value = OpenAPIUtils.findStatusCodeMatch(500, {
'200': 'OK',
'500': null,
'201': 'Created',
});
expect(value).toEqual(null);
});
test('matches 500 (undefined)', async () => {
const value = OpenAPIUtils.findStatusCodeMatch(500, {
'200': 'OK',
'500': undefined,
'201': 'Created',
});
expect(value).toEqual(undefined);
});
test('matches 400 (when pattern present)', async () => {
const value = OpenAPIUtils.findStatusCodeMatch(400, {
'200': 'OK',
'401': 'Unauthorized',
'4XX': 'Error (patterned)',
'400': 'Bad Request',
});
expect(value).toEqual('Bad Request');
});
test('matches 401 (when pattern present)', async () => {
const value = OpenAPIUtils.findStatusCodeMatch(401, {
'200': 'OK',
'401': 'Unauthorized',
'4XX': 'Error (patterned)',
'400': 'Bad Request',
});
expect(value).toEqual('Unauthorized');
});
test('matches 403 (via pattern)', async () => {
const value = OpenAPIUtils.findStatusCodeMatch(403, {
'200': 'OK',
'401': 'Unauthorized',
'4XX': 'Error (patterned)',
'400': 'Bad Request',
});
expect(value).toEqual('Error (patterned)');
});
test('not matches default (on pattern when default present)', async () => {
const value = OpenAPIUtils.findStatusCodeMatch(402, {
'200': 'OK',
default: 'Default value',
'401': 'Unauthorized',
'4XX': 'Error (patterned)',
'400': 'Bad Request',
});
expect(value).toEqual('Error (patterned)');
});
test('matches default', async () => {
const value = OpenAPIUtils.findStatusCodeMatch(500, {
'200': 'OK',
default: 'Default value',
'401': 'Unauthorized',
'4XX': 'Error (patterned)',
'400': 'Bad Request',
});
expect(value).toEqual('Default value');
});
test('wrong pattern fallback', async () => {
// not even sure this is a relevent test, but let's make sure this doesn't return '200-299 codes'
const value = OpenAPIUtils.findStatusCodeMatch(23456, {
'1XX': '100-199 codes',
'2XX': '200-299 codes',
'3XX': '300-399 codes',
});
expect(value).toEqual(undefined);
});
});
describe('.findDefaultStatusCodeMatch', () => {
test('matches 200', async () => {
const value = OpenAPIUtils.findDefaultStatusCodeMatch({
'200': '200',
'201': '201',
});
expect(value.res).toEqual('200');
});
test('matches 201', async () => {
const value = OpenAPIUtils.findDefaultStatusCodeMatch({
'201': '201',
'300': '300',
});
expect(value.res).toEqual('201');
});
test('matches 201 with 2XX fallback', async () => {
const value = OpenAPIUtils.findDefaultStatusCodeMatch({
'201': '201',
'2XX': '2XX',
'300': '300',
});
expect(value.res).toEqual('201');
});
test('matches 201 with default fallback', async () => {
const value = OpenAPIUtils.findDefaultStatusCodeMatch({
default: 'default',
'201': '201',
'2XX': '2XX',
'300': '300',
});
expect(value.res).toEqual('201');
});
test('matches 2XX', async () => {
const value = OpenAPIUtils.findDefaultStatusCodeMatch({
'2XX': '2XX',
'300': '300',
});
expect(value.res).toEqual('2XX');
});
test('matches 2XX with default fallback', async () => {
const value = OpenAPIUtils.findDefaultStatusCodeMatch({
'2XX': '2XX',
default: 'default',
});
expect(value.res).toEqual('2XX');
});
test('matches first one', async () => {
const value = OpenAPIUtils.findDefaultStatusCodeMatch({
'305': '305',
'3XX': '3XX',
});
expect(value.res).toEqual('305');
});
});
});