-
Notifications
You must be signed in to change notification settings - Fork 7
/
route-matching-general-tests.ts
344 lines (272 loc) · 13 KB
/
route-matching-general-tests.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import fetch from 'node-fetch';
import {FakeServer} from '../src';
const port = 4444;
let fakeServer: FakeServer;
beforeEach(() => {
fakeServer = new FakeServer(port);
fakeServer.start();
});
afterEach(() => {
fakeServer.stop();
});
[
{method: 'willSucceed', defaultStatus: 200, useNewApi: false},
{method: 'willFail', defaultStatus: 500, useNewApi: false},
{method: 'willSucceed', defaultStatus: 200, useNewApi: true},
{method: 'willFail', defaultStatus: 500, useNewApi: true},
].forEach(({method, defaultStatus, useNewApi}) => {
describe(`Route Matching - ${method}`, () => {
test('GET route defined, one call matches and two dont, callsReceived returns only the matching call', async () => {
const path = '/somePath';
let route;
if (useNewApi) {
route = fakeServer.get(path)[method]();
} else {
route = fakeServer.get().to(path)[method]();
}
await fetch(`http://localhost:${port}${path}`, {method: 'GET'});
await fetch(`http://localhost:${port}${path}`, {method: 'PUT'});
await fetch(`http://localhost:${port}${path}`, {method: 'POST'});
expect(fakeServer.callsReceived(route.call).length).toEqual(1);
});
test('GET route defined and called - match', async () => {
const path = '/somePath';
let route;
if (useNewApi) {
route = fakeServer.get(path)[method]();
} else {
route = fakeServer.get().to(path)[method]();
}
const res = await fetch(`http://localhost:${port}${path}`, {method: 'GET'});
expect(res.status).toEqual(defaultStatus);
expect(fakeServer.didReceive(route.call)).toEqual(true);
});
test('GET route with query params - match', async () => {
const path = '/someQueryPath';
const queryObject = {k1: 'v1', k2: 'v2'};
let route;
if (useNewApi) {
route = fakeServer.get(path).withQueryParams(queryObject)[method]();
} else {
route = fakeServer.get().to(path).withQueryParams(queryObject)[method]();
}
const queryParams = '?k1=v1&k2=v2';
const res = await fetch(`http://localhost:${port}${path}${queryParams}`, {method: 'GET'});
expect(res.status).toEqual(defaultStatus);
expect(fakeServer.didReceive(route.call)).toEqual(true);
});
test('GET route with extra query params - no match', async () => {
const path = '/someQueryPath';
const queryObject = {k1: 'v1'};
let route;
if (useNewApi) {
route = fakeServer.get(path).withQueryParams(queryObject)[method]();
} else {
route = fakeServer.get().to(path).withQueryParams(queryObject)[method]();
}
const queryParams = '?k1=v1&k2=v2';
const res = await fetch(`http://localhost:${port}${path}${queryParams}`, {method: 'GET'});
expect(res.status).toEqual(400);
expect(fakeServer.didReceive(route.call)).toEqual(false);
});
test('GET route with wrong query params - no match', async () => {
const path = '/someQueryPath';
const queryObject = {k1: 'v1', k2: 'v2'};
let route;
if (useNewApi) {
route = fakeServer.get(path).withQueryParams(queryObject)[method]();
} else {
route = fakeServer.get().to(path).withQueryParams(queryObject)[method]();
}
const wrongQueryParams = '?k1=v1&k3=v3';
const res = await fetch(`http://localhost:${port}${path}${wrongQueryParams}`, {method: 'GET'});
expect(res.status).toEqual(400);
expect(fakeServer.didReceive(route.call)).toEqual(false);
});
test('GET route with partial query params - no match', async () => {
const path = '/someQueryPath';
const queryObject = {k1: 'v1', k2: 'v2'};
let route;
if (useNewApi) {
route = fakeServer.get(path).withQueryParams(queryObject)[method]();
} else {
route = fakeServer.get().to(path).withQueryParams(queryObject)[method]();
}
const wrongQueryParams = '?k1=v1';
const res = await fetch(`http://localhost:${port}${path}${wrongQueryParams}`, {method: 'GET'});
expect(res.status).toEqual(400);
expect(fakeServer.didReceive(route.call)).toEqual(false);
});
test('GET route with no query params and with query restrictions - no match', async () => {
const path = '/someQueryPath';
const queryObject = {k1: 'v1', k2: 'v2'};
let route;
if (useNewApi) {
route = fakeServer.get(path).withQueryParams(queryObject)[method]();
} else {
route = fakeServer.get().to(path).withQueryParams(queryObject)[method]();
}
const res = await fetch(`http://localhost:${port}${path}`, {method: 'GET'});
expect(res.status).toEqual(400);
expect(fakeServer.didReceive(route.call)).toEqual(false);
});
test('GET route with query paraysm without specifying query restrictions - match', async () => {
const path = '/someQueryPath';
let route;
if (useNewApi) {
route = fakeServer.get()[method]();
} else {
route = fakeServer.get().to(path)[method]();
}
const queryParams = '?k1=v1&k3=v3';
const res = await fetch(`http://localhost:${port}${path}${queryParams}`, {method: 'GET'});
expect(res.status).toEqual(defaultStatus);
expect(fakeServer.didReceive(route.call)).toEqual(true);
});
test('DELETE route defined and called - match', async () => {
const path = '/somePath';
let route;
if (useNewApi) {
route = fakeServer.delete(path)[method]();
} else {
route = fakeServer.delete().to(path)[method]();
}
const res = await fetch(`http://localhost:${port}${path}`, {method: 'DELETE'});
expect(res.status).toEqual(defaultStatus);
expect(fakeServer.didReceive(route.call)).toEqual(true);
});
test('PUT route defined and called - match', async () => {
const path = '/somePath';
let route;
if (useNewApi) {
route = fakeServer.put(path)[method]();
} else {
route = fakeServer.put().to(path)[method]();
}
const res = await fetch(`http://localhost:${port}${path}`, {method: 'PUT'});
expect(res.status).toEqual(defaultStatus);
expect(fakeServer.didReceive(route.call)).toEqual(true);
});
test('PATCH route defined and called - match', async () => {
const path = '/somePath';
let route;
if (useNewApi) {
route = fakeServer.patch(path)[method]();
} else {
route = fakeServer.patch().to(path)[method]();
}
const res = await fetch(`http://localhost:${port}${path}`, {method: 'PATCH'});
expect(res.status).toEqual(defaultStatus);
expect(fakeServer.didReceive(route.call)).toEqual(true);
});
test('route defined and not called - no match', () => {
const path = '/somePath';
let route;
if (useNewApi) {
route = fakeServer.get(path)[method]();
} else {
route = fakeServer.get().to(path)[method]();
}
expect(fakeServer.didReceive(route.call)).toEqual(false);
});
test('route defined with path regex - asserting on specific path that matches the regex - assertion success', async () => {
const pathRegex = '/[a-zA-Z]+$';
const actualPath = '/somePathThatMatchesTheRegex';
let route;
if (useNewApi) {
route = fakeServer.get(pathRegex)[method]();
} else {
route = fakeServer.get().to(pathRegex)[method]();
}
const res = await fetch(`http://localhost:${port}${actualPath}`, {method: 'GET'});
expect(res.status).toEqual(defaultStatus);
expect(fakeServer.didReceive(route.call.withPath(actualPath))).toEqual(true);
});
test('route defined with path regex - asserting on specific path that does not match the path regex - throws', () => {
const pathRegex = '/[0-9]+$';
const pathThatDoesNotMatchTheRegex = '/pathThatDoesNotMatchTheRegex';
let route;
if (useNewApi) {
route = fakeServer.get(pathRegex)[method]();
} else {
route = fakeServer.get().to(pathRegex)[method]();
}
expect(() => route.call.withPath(pathThatDoesNotMatchTheRegex)).toThrow();
});
test('route defined with path and body regex - chaining assertions, specific path and body match path and body regex - assertion success', async () => {
const pathRegex = '/[a-zA-Z]+$';
const actualPath = '/somePath';
const bodyRegex = '[0-9]+$';
const actualBody = '123';
let route;
if (useNewApi) {
route = fakeServer.post(pathRegex).withBodyThatMatches(bodyRegex)[method]();
} else {
route = fakeServer.post().to(pathRegex).withBodyThatMatches(bodyRegex)[method]();
}
const res = await fetch(`http://localhost:${port}${actualPath}`, {
method: 'POST',
headers: {'Content-Type': 'text/plain'},
body: actualBody,
});
expect(res.status).toEqual(defaultStatus);
expect(fakeServer.didReceive(route.call.withPath(actualPath).withBodyText(actualBody))).toEqual(true);
expect(fakeServer.didReceive(route.call.withBodyText(actualBody).withPath(actualPath))).toEqual(true);
});
});
});
[{useNewApi: true}, {useNewApi: false}].forEach(({useNewApi}) => {
describe('Route Matching - willReturn', () => {
test('GET route defined with default status code and called - match', async () => {
const path = '/somePath';
let route;
if (useNewApi) {
route = fakeServer.get(path).willReturn({name: 'Morty'});
} else {
route = fakeServer.get().to(path).willReturn({name: 'Morty'});
}
const res = await fetch(`http://localhost:${port}${path}`, {method: 'GET'});
const body = await res.json();
expect(body.name).toEqual('Morty');
expect(res.status).toEqual(200);
expect(fakeServer.didReceive(route.call)).toEqual(true);
});
test('GET route defined with custom status code and called - match', async () => {
const path = '/somePath';
let route;
if (useNewApi) {
route = fakeServer.get(path).willReturn({name: 'Teapot'}, 418);
} else {
route = fakeServer.get().to(path).willReturn({name: 'Teapot'}, 418);
}
const res = await fetch(`http://localhost:${port}${path}`, {method: 'GET'});
const body = await res.json();
expect(body.name).toEqual('Teapot');
expect(res.status).toEqual(418);
expect(fakeServer.didReceive(route.call)).toEqual(true);
});
test('route defined with path and body regex - chaining assertions, specific path and body match path and body regex - assertion success', async () => {
const pathRegex = '/[a-zA-Z]+$';
const actualPath = '/somePath';
const bodyRegex = '[0-9]+$';
const actualBody = '123';
const requestBody = 'body as string';
let route = fakeServer.post().to(pathRegex).withBodyThatMatches(bodyRegex).willReturn(requestBody, 300);
if (useNewApi) {
route = fakeServer.post(pathRegex).withBodyThatMatches(bodyRegex).willReturn(requestBody, 300);
} else {
route = fakeServer.post().to(pathRegex).withBodyThatMatches(bodyRegex).willReturn(requestBody, 300);
}
const res = await fetch(`http://localhost:${port}${actualPath}`, {
method: 'POST',
headers: {'Content-Type': 'text/plain'},
body: actualBody,
});
const body = await res.text();
expect(res.status).toEqual(300);
expect(body).toEqual(requestBody);
expect(fakeServer.didReceive(route.call.withPath(actualPath).withBodyText(actualBody))).toEqual(true);
expect(fakeServer.didReceive(route.call.withBodyText(actualBody).withPath(actualPath))).toEqual(true);
});
});
});