-
Notifications
You must be signed in to change notification settings - Fork 1
/
requests.spec.js
429 lines (395 loc) · 11.7 KB
/
requests.spec.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
describe('Requests', function () {
this.timeout(8000);
it('makes a GET request', function () {
return yea
.method('get')
.url('/simple-get')
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.equal('hello');
});
});
it('defaults to GET', function () {
return yea
.url('/simple-get')
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.equal('hello');
});
});
it('makes a PUT request', function () {
return yea
.method('put')
.url('/simple-put')
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.equal('golf');
});
});
it('makes a DELETE request', function () {
return yea
.method('delete')
.url('/simple-delete')
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.equal('begone');
});
});
it('sends request headers', function () {
return yea
.url('/dump-headers')
.headers({ 'x-random': 'yes' })
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.contain('x-random: yes');
});
});
it('returns response headers', function () {
return yea
.method('get')
.url('/dummy-headers')
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.headers).to.be.a('object');
expect(response.headers['x-dummy']).to.equal('definitely');
});
});
// it is almost impossible to send any other than lowercase header names from our
// node-based test server, so we can't really test this property at this time
// see: https://github.com/nodejs/node/issues/3591
it('lowercases incoming response header names');
it('uses baseUrl if set', function () {
return yea
.method('get')
.baseUrl('http://localhost:9876/nested')
.url('simple-get')
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.equal('nested hello');
});
});
it('includes query string if set', function () {
return yea
.url('/dump-query')
.query({ hello: 'yes', looking: 'me' })
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.contain('hello: yes\nlooking: me');
});
});
it('overrides query string via .query', function () {
return yea
.url('/dump-query?first=here')
.query({ hello: 'yes' })
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.contain('hello: yes');
});
});
it('overrides query string via .url', function () {
return yea
.query({ hello: 'yes' })
.url('/dump-query?first=here')
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.contain('first: here');
});
});
it('replaces urlParams if present', function () {
return yea
.url('/dump-url/:accountId')
.urlParams({ accountId: 123 })
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.equal('/dump-url/123');
});
});
it('sends body', function () {
return yea
.method('post')
.url('/dump-request-body')
.body('here i am')
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.equal('here i am');
});
});
it('sends body via .send', function () {
return yea
.method('post')
.url('/dump-request-body')
.send('here i am')
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.equal('here i am');
});
});
it('sends urlencoded', function () {
return yea
.method('post')
.url('/validate-urlencoded-request')
.urlencoded({ hello: 'there', whats: 'up' })
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.equal('PASS');
});
});
it('sends urlencoded via .sendUrlencoded', function () {
return yea
.method('post')
.url('/validate-urlencoded-request')
.sendUrlencoded({ hello: 'there', whats: 'up' })
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.equal('PASS');
});
});
it('sends json', function () {
return yea
.method('post')
.url('/validate-json-request')
.json({ lover: 'leaver' })
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.equal('{"lover":"leaver"}');
});
});
it('sends json via .sendJson', function () {
return yea
.method('post')
.url('/validate-json-request')
.sendJson({ lover: 'leaver' })
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.equal('{"lover":"leaver"}');
});
});
it('decodes incoming json', function () {
return yea
.method('get')
.url('/json-payload')
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.data).to.be.a('object');
expect(response.data).to.deep.equal({
taker: 'believer'
});
});
});
it('decodes incoming json on rejected responses', function () {
var error;
return yea
.method('get')
.url('/json-payload-fail')
.send()
.catch(function (_error) {
error = _error;
})
.then(function () {
expect(error).to.be.instanceOf(Error);
expect(error.message).to.equal('Request failed with status 400');
expect(error.response).to.be.a('object');
expect(error.response.data).to.deep.equal({
taker: 'believer'
});
expect(error.response.status).to.equal(400);
});
});
it('does not decode incoming json without transformer', function () {
return yea
.method('get')
.url('/json-payload')
.setResponseTransformers([])
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.equal('{"taker":"believer"}');
});
});
it('returns property via .prop()', function () {
return yea
.method('get')
.url('/json-payload')
.prop('data')
.send()
.then(function (body) {
expect(body).to.deep.equal({
taker: 'believer'
});
});
});
it('returns nested property via .prop()', function () {
return yea
.method('get')
.url('/json-payload')
.prop('data.taker')
.send()
.then(function (value) {
expect(value).to.equal('believer');
});
});
it('throws on 404', function () {
var error;
return yea
.method('get')
.url('/this-shall-not-exist')
.send()
.catch(function (_error) {
error = _error;
})
.then(function () {
expect(error).to.be.instanceOf(Error);
expect(error.message).to.equal('Request failed with status 404');
expect(error.response).to.be.a('object');
expect(error.response.status).to.equal(404);
});
});
it('validates status using integer', function () {
var error;
var req = yea.setAllowedStatusCode(202);
return req
.url('/specific-status?give=200')
.send()
.catch(function (_error) {
error = _error;
})
.then(function () {
expect(error).to.be.instanceOf(Error);
expect(error.message).to.equal('Request failed with status 200');
return req.url('/specific-status?give=202').send();
});
});
it('validates status using regex', function () {
var error;
var req = yea.setAllowedStatusCode(/^300$/);
return req
.url('/specific-status?give=200')
.send()
.catch(function (_error) {
error = _error;
})
.then(function () {
expect(error).to.be.instanceOf(Error);
expect(error.message).to.equal('Request failed with status 200');
return req.url('/specific-status?give=300').send();
});
});
it('validates status using function', function () {
var error;
var req = yea.setAllowedStatusCode(function (status) {
return status === 201;
});
return req
.url('/specific-status?give=200')
.send()
.catch(function (_error) {
error = _error;
})
.then(function () {
expect(error).to.be.instanceOf(Error);
expect(error.message).to.equal('Request failed with status 200');
return req.url('/specific-status?give=201').send();
});
});
it('uses custom response transformers', function () {
var transformers = [
// transformer 1
function (response) {
return yea.utils.assign({}, response, {
body: response.body + '-1'
});
},
// transformer 2
function (response) {
return yea.utils.assign({}, response, {
body: response.body + '-2'
});
}
];
return yea
.method('get')
.url('/simple-get')
.setResponseTransformers(transformers)
.send()
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.equal('hello-1-2');
});
});
it('supports specifying a timeout', function () {
var error;
var req = yea.timeout(1000);
return req
.url('/specific-timeout?wait=2000')
.send()
.catch(function (_error) {
error = _error;
})
.then(function () {
expect(error).to.be.instanceOf(Error);
expect(error.message).to.equal('Request failed due to timeout (1000ms)');
return req.url('/specific-timeout?wait=0').send();
});
}).slow(3000);
it('supports clearing timeout', function () {
return yea
.timeout(100)
.unsetTimeout()
.url('/specific-timeout?wait=200')
.send();
}).slow(500);
it('does not require .send()', function () {
return yea
.method('get')
.url('/simple-get')
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.body).to.be.a('string');
expect(response.body).to.equal('hello');
});
});
it('allows setting Promise-implementation', function () {
function MyPromise () {}
var promise = yea
.method('get')
.url('/simple-get')
.polyfills({
Promise: MyPromise
})
.send();
expect(promise).to.be.instanceOf(MyPromise);
});
});