-
Notifications
You must be signed in to change notification settings - Fork 5
/
tests.js
233 lines (197 loc) · 8.09 KB
/
tests.js
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
import fetch from 'node-fetch';
const runTests = (apiBase) => {
test('if invalid path, then 404', async () => {
const response = await fetch(`${apiBase}/foo/bar`);
expect(response.status).toEqual(404);
});
/**
* GET /robin/location
* 500: does not have an example
* 200: does not have an example
*/
test('if valid path, but not faked and without examples, then 501', async () => {
const response = await fetch(`${apiBase}/robin/location`);
expect(response.status).toEqual(501);
});
/**
* GET /batman/location
* 500: does not have an example
* 201: example at the wrong level
* 200: example at the right level
*/
test('if not mocked, return 1st example response', async () => {
const response = await fetch(`${apiBase}/batman/location`);
expect(response.status).toEqual(200);
expect(response.text()).resolves.toEqual('batcave');
});
test('only /mock can be used to mock', async () => {
const mock = {
method: 'GET', path: '/batman/location', status: 201, response: { arkham: 'asylum' },
};
let response = await fetch(
`${apiBase}/some/path/to/mock`,
{ method: 'PUT', body: JSON.stringify(mock), headers: { 'content-type': 'application/json' } },
);
expect(response.status).toEqual(404);
expect(response.statusText).toEqual('Not Found');
response = await fetch(
`${apiBase}/mock`,
{ method: 'PUT', body: JSON.stringify(mock), headers: { 'content-type': 'application/json' } },
);
expect(response.status).toEqual(204);
expect(response.statusText).toEqual('No Content');
});
test('if mocked, with body, returns the mocked status and body', async () => {
let response = await fetch(`${apiBase}/batman/location`);
expect(response.text()).resolves.toEqual('batcave');
const mock = {
method: 'GET', path: '/batman/location', status: 201, response: { arkham: 'asylum' },
};
response = await fetch(
`${apiBase}/mock`,
{ method: 'PUT', body: JSON.stringify(mock), headers: { 'content-type': 'application/json' } },
);
expect(response.status).toEqual(204);
expect(response.statusText).toEqual('No Content');
response = await fetch(`${apiBase}/batman/location`, { method: 'GET' });
expect(response.status).toEqual(mock.status);
expect(response.json()).resolves.toEqual(mock.response);
});
test('if mocked, without body, returns the mocked status', async () => {
let response = await fetch(`${apiBase}/batman/location`);
expect(response.text()).resolves.toEqual('batcave');
const mock = {
method: 'GET', path: '/batman/location', status: 201,
};
response = await fetch(
`${apiBase}/mock`,
{ method: 'PUT', body: JSON.stringify(mock), headers: { 'content-type': 'application/json' } },
);
expect(response.status).toEqual(204);
response = await fetch(`${apiBase}/batman/location`, { method: 'GET' });
expect(response.status).toEqual(mock.status);
expect(response.text()).resolves.toEqual('');
});
test('if mocked, subsequent mocks will override', async () => {
const mock = {
method: 'GET', path: '/batman/location', status: 201, response: 'arkham',
};
let response = await fetch(
`${apiBase}/mock`,
{ method: 'PUT', body: JSON.stringify(mock), headers: { 'content-type': 'application/json' } },
);
expect(response.status).toEqual(204);
mock.status = 200;
mock.response = 'wayne manor';
response = await fetch(
`${apiBase}/mock`,
{ method: 'PUT', body: JSON.stringify(mock), headers: { 'content-type': 'application/json' } },
);
expect(response.status).toEqual(204);
response = await fetch(`${apiBase}/batman/location`, { method: 'GET' });
expect(response.status).toEqual(200);
expect(response.text()).resolves.toEqual('wayne manor');
});
test('if mocked, with query string, returns different responses by query', async () => {
let response = await fetch(`${apiBase}/batman/location`);
expect(response.text()).resolves.toEqual('batcave');
const mock = {
method: 'GET',
path: '/robin/location',
qs: 'greeting=hi%20you&foo=1',
status: 201,
response: { arkham: 'asylum' },
};
response = await fetch(
`${apiBase}/mock`,
{ method: 'PUT', body: JSON.stringify(mock), headers: { 'content-type': 'application/json' } },
);
expect(response.status).toEqual(204);
expect(response.statusText).toEqual('No Content');
// works when query is urlencoded
response = await fetch(`${apiBase}/robin/location?greeting=hi%20you&foo=1`, { method: 'GET' });
expect(response.status).toEqual(mock.status);
expect(response.json()).resolves.toEqual(mock.response);
// works when query is NOT urlencoded
response = await fetch(`${apiBase}/robin/location?greeting=hi you&foo=1`, { method: 'GET' });
expect(response.status).toEqual(mock.status);
expect(response.json()).resolves.toEqual(mock.response);
// change query params order
response = await fetch(`${apiBase}/robin/location?foo=1&greeting=hi%20you`, { method: 'GET' });
expect(response.status).toEqual(mock.status);
expect(response.json()).resolves.toEqual(mock.response);
// try unmocked query
response = await fetch(`${apiBase}/robin/location?hello=world`, { method: 'GET' });
expect(response.status).toEqual(501);
expect(response.statusText).toEqual('Not Implemented');
});
test('if mocked, with path params, returns different responses by path', async () => {
const mock = {
method: 'GET',
path: '/robin/catchphrases/1',
status: 200,
response: { catchphrases: 'holy gemini batman!' },
};
let response = await fetch(
`${apiBase}/mock`,
{ method: 'PUT', body: JSON.stringify(mock), headers: { 'content-type': 'application/json' } },
);
expect(response.status).toEqual(204);
expect(response.statusText).toEqual('No Content');
// works when path is mocked
response = await fetch(`${apiBase}/robin/catchphrases/1`, { method: 'GET' });
expect(response.status).toEqual(mock.status);
expect(response.json()).resolves.toEqual(mock.response);
// cannot find unmocked path
response = await fetch(`${apiBase}/robin/catchphrases/2`, { method: 'GET' });
expect(response.status).toEqual(501);
expect(response.statusText).toEqual('Not Implemented');
// cannot find path with variable
response = await fetch(`${apiBase}/robin/catchphrases/{id}`, { method: 'GET' });
expect(response.status).toEqual(400);
expect(response.statusText).toEqual('Bad Request');
});
test('if mocked, but called with invalid path, then 400', async () => {
const mock = {
method: 'GET',
path: '/robin/catchphrases/invalid_id',
status: 200,
response: { catchphrases: 'holy gemini batman!' },
};
// mock with invalid path
let response = await fetch(
`${apiBase}/mock`,
{ method: 'PUT', body: JSON.stringify(mock), headers: { 'content-type': 'application/json' } },
);
expect(response.status).toEqual(204);
expect(response.statusText).toEqual('No Content');
// invoke invalid path
response = await fetch(`${apiBase}/robin/catchphrases/invalid_id`, { method: 'GET' });
expect(response.status).toEqual(400);
expect(response.statusText).toEqual('Bad Request');
});
};
describe('swagger 2', () => {
const apiPort = 15009;
const apiBase = `http://localhost:${apiPort}/parrot-test`;
beforeEach(async () => {
await fetch(`${apiBase}/mock`, { method: 'DELETE' });
});
test('if invalid api base path, then 404', async () => {
const response = await fetch(`http://localhost:${apiPort}/foo`);
expect(response.status).toEqual(404);
});
runTests(apiBase);
});
describe('openapi 3', () => {
const apiPort = 15010;
const apiBase = `http://localhost:${apiPort}/parrot-test`;
beforeEach(async () => {
await fetch(`${apiBase}/mock`, { method: 'DELETE' });
});
test('if invalid api base path, then 404', async () => {
const response = await fetch(`http://localhost:${apiPort}/foo`);
expect(response.status).toEqual(404);
});
runTests(apiBase);
});