-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbody-restrictions-on-assertion-tests.ts
279 lines (225 loc) · 10.9 KB
/
body-restrictions-on-assertion-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
import fetch from 'node-fetch';
import {FakeServer} from '../src';
const port = 6666;
const path = '/somePath';
const lettersRegex = '[a-zA-Z]+$';
let fakeServer: FakeServer;
beforeEach(() => {
fakeServer = new FakeServer(port);
fakeServer.start();
});
afterEach(() => {
fakeServer.stop();
});
// route defined with regex body restriction
[{useNewApi: true}, {useNewApi: false}].forEach(({useNewApi}) => {
describe('Body restriction on assertion', () => {
test('regex restriction, assert on a specific string that matches the regex, request body equals test string - assertion success', async () => {
let route;
if (useNewApi) {
route = fakeServer.post(path).withBodyThatMatches(lettersRegex).willSucceed();
} else {
route = fakeServer.post().to(path).withBodyThatMatches(lettersRegex).willSucceed();
}
const actualBody = 'abc';
const testString = 'abc';
await fetch(`http://localhost:${port}${path}`, {
method: 'POST',
headers: {'Content-Type': 'text/plain'},
body: actualBody,
});
expect(fakeServer.didReceive(route.call.withBodyText(testString))).toEqual(true);
});
test('regex restriction, assert on a specific string that matches the regex, request body does not equal test string (but matches regex) - assertion success', async () => {
let route;
if (useNewApi) {
route = fakeServer.post(path).withBodyThatMatches(lettersRegex).willSucceed();
} else {
route = fakeServer.post().to(path).withBodyThatMatches(lettersRegex).willSucceed();
}
const actualBody = 'abc';
const testString = 'def';
await fetch(`http://localhost:${port}${path}`, {
method: 'POST',
headers: {'Content-Type': 'text/plain'},
body: actualBody,
});
expect(fakeServer.didReceive(route.call.withBodyText(testString))).toEqual(false);
});
test('regex restriction, assert on a specific string that does not match the regex - exception', () => {
let route;
if (useNewApi) {
route = fakeServer.post(path).withBodyThatMatches(lettersRegex).willSucceed();
} else {
route = fakeServer.post().to(path).withBodyThatMatches(lettersRegex).willSucceed();
}
expect(() => route.call.withBodyText('123')).toThrow();
});
test('regex restriction, passing something other than string to specific string method - exception', () => {
let route;
if (useNewApi) {
route = fakeServer.post(path).withBodyThatMatches(lettersRegex).willSucceed();
} else {
route = fakeServer.post().to(path).withBodyThatMatches(lettersRegex).willSucceed();
}
expect(() => route.call.withBodyText({} as string)).toThrow();
});
test('regex restriction, assert on a specific object - exception', () => {
let route;
if (useNewApi) {
route = fakeServer.post(path).withBodyThatMatches(lettersRegex).willSucceed();
} else {
route = fakeServer.post().to(path).withBodyThatMatches(lettersRegex).willSucceed();
}
expect(() => route.call.withSpecificBody({})).toThrow();
});
// route defined with partial object body restriction
test('partial object restriction, assert on a specific object that matches the partial object, request body equals test object - assertion success', async () => {
const expectedPartialBody = {a: 1};
const actualBody = {a: 1, b: 2};
const testBody = {a: 1, b: 2};
let route;
if (useNewApi) {
route = fakeServer.post(path).withBodyThatContains(expectedPartialBody).willSucceed();
} else {
route = fakeServer.post().to(path).withBodyThatContains(expectedPartialBody).willSucceed();
}
await fetch(`http://localhost:${port}${path}`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(actualBody),
});
expect(fakeServer.didReceive(route.call.withSpecificBody(testBody))).toEqual(true);
});
test('partial object restriction, assert on a specific object that matches the partial object, request body does not equal test object (but matches partial object) - assertion fails', async () => {
const expectedPartialBody = {a: 1};
const actualBody = {a: 1, b: 2};
const testBody = {a: 1};
let route;
if (useNewApi) {
route = fakeServer.post(path).withBodyThatContains(expectedPartialBody).willSucceed();
} else {
route = fakeServer.post().to(path).withBodyThatContains(expectedPartialBody).willSucceed();
}
await fetch(`http://localhost:${port}${path}`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(actualBody),
});
expect(fakeServer.didReceive(route.call.withSpecificBody(testBody))).toEqual(false);
});
test('partial object restriction, assert on a specific object that does not match the partial object - exception', () => {
const expectedPartialBody = {a: 1};
const testBody = {b: 2};
let route;
if (useNewApi) {
route = fakeServer.post(path).withBodyThatContains(expectedPartialBody).willSucceed();
} else {
route = fakeServer.post().to(path).withBodyThatContains(expectedPartialBody).willSucceed();
}
expect(() => route.call.withSpecificBody(testBody)).toThrow();
});
test('partial object restriction, passing something other than object to specific object method - exception', () => {
let route;
if (useNewApi) {
route = fakeServer.post(path).withBodyThatContains({a: 1}).willSucceed();
} else {
route = fakeServer.post().to(path).withBodyThatContains({a: 1}).willSucceed();
}
expect(() => route.call.withSpecificBody('')).toThrow();
});
test('partial object restriction, assert on a specific string - exception', () => {
let route;
if (useNewApi) {
route = fakeServer.post(path).withBodyThatContains({}).willSucceed();
} else {
route = fakeServer.post().to(path).withBodyThatContains({}).willSucceed();
}
expect(() => route.call.withBodyText('')).toThrow();
});
// route defined with object body restriction
test('object restriction, assert on a specific string - exception', () => {
let route;
if (useNewApi) {
route = fakeServer.post(path).withBody({}).willSucceed();
} else {
route = fakeServer.post().to(path).withBody({}).willSucceed();
}
expect(() => route.call.withBodyText('')).toThrow();
});
test('object restriction, assert on a specific object (again) - exception', () => {
let route;
if (useNewApi) {
route = fakeServer.post(path).withBody({}).willSucceed();
} else {
route = fakeServer.post().to(path).withBody({}).willSucceed();
}
expect(() => route.call.withSpecificBody({})).toThrow();
});
// route defined with no body restriction
test('no body restriction, assert on a specific string, application/json header, request body equals test string - assertion success', async () => {
let route;
if (useNewApi) {
route = fakeServer.post(path).willSucceed();
} else {
route = fakeServer.post().to(path).willSucceed();
}
const actualBody = {a: 1};
const testString = JSON.stringify({a: 1});
await fetch(`http://localhost:${port}${path}`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(actualBody),
});
expect(fakeServer.didReceive(route.call.withBodyText(testString))).toEqual(true);
});
test('no body restriction, assert on a specific string, no application/json header, request body equals test string - assertion success', async () => {
let route;
if (useNewApi) {
route = fakeServer.post(path).willSucceed();
} else {
route = fakeServer.post().to(path).willSucceed();
}
const actualBody = 'abc';
const testString = 'abc';
await fetch(`http://localhost:${port}${path}`, {
method: 'POST',
headers: {'Content-Type': 'text/plain'},
body: actualBody,
});
expect(fakeServer.didReceive(route.call.withBodyText(testString))).toEqual(true);
});
test('no body restriction, assert on a specific object, application/json header request body equals test object - assertion success', async () => {
const actualBody = {a: 1, b: 2};
const testBody = {a: 1, b: 2};
let route;
if (useNewApi) {
route = fakeServer.post(path).willSucceed();
} else {
route = fakeServer.post().to(path).willSucceed();
}
await fetch(`http://localhost:${port}${path}`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(actualBody),
});
expect(fakeServer.didReceive(route.call.withSpecificBody(testBody))).toEqual(true);
});
test('no body restriction, assert on a specific object, no application/json header, request body equals test object - assertion fails', async () => {
const actualBody = {a: 1, b: 2};
const testBody = {a: 1, b: 2};
let route;
if (useNewApi) {
route = fakeServer.post(path).willSucceed();
} else {
route = fakeServer.post().to(path).willSucceed();
}
await fetch(`http://localhost:${port}${path}`, {
method: 'POST',
headers: {'Content-Type': 'text/plain'},
body: JSON.stringify(actualBody),
});
expect(fakeServer.didReceive(route.call.withSpecificBody(testBody))).toEqual(false);
});
});
});