-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathrequest-test.js
983 lines (723 loc) · 30 KB
/
request-test.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
'use strict';
var rp = require('../../lib/rp.js');
var http = require('http');
var url = require('url');
var Bluebird = require('bluebird');
var childProcess = require('child_process');
var path = require('path');
var es = require('event-stream');
describe('Request-Promise', function () {
var server, lastResponseBody;
before(function (done) {
// This creates a local server to test for various status codes. A request to /404 returns a 404, etc
server = http.createServer(function(request, response){
var path = url.parse(request.url).pathname;
var status = parseInt(path.split('/')[1]);
if(isNaN(status)) { status = 555; }
if (status === 302) {
response.writeHead(status, { location: '/200' });
lastResponseBody = '';
response.end();
} else {
response.writeHead(status, { 'Content-Type': 'text/plain' });
lastResponseBody = request.method + ' ' + request.url;
response.end(lastResponseBody);
}
});
server.listen(4000, function () {
done();
});
});
after(function () {
// Wait for all requests to finish since they may produce unhandled errors for tests at the end that don't wait themselves.
setTimeout(function () {
server.close();
}, 20);
});
describe('should reject HTTP errors', function () {
it('like an unreachable host', function () {
return expect(rp('http://localhost:1/200')).to.be.rejected;
});
});
describe('should handle status codes', function () {
it('by resolving a 200', function () {
return expect(rp('http://localhost:4000/200')).to.be.fulfilled;
});
it('by resolving a 201', function () {
return expect(rp('http://localhost:4000/201')).to.be.fulfilled;
});
it('by resolving a 204', function () {
return expect(rp('http://localhost:4000/204')).to.be.fulfilled;
});
describe('with options.simple = true', function(){
it('by rejecting a 404', function () {
return expect(rp('http://localhost:4000/404')).to.be.rejected;
});
it('by rejecting a 500', function () {
return expect(rp('http://localhost:4000/500')).to.be.rejected;
});
});
describe('with options.simple = false', function(){
it('by resolving a 404', function (){
var options = {
url: 'http://localhost:4000/404',
simple: false
};
return expect(rp(options)).to.be.fulfilled;
});
it('by resolving a 500', function (){
var options = {
url: 'http://localhost:4000/500',
simple: false
};
return expect(rp(options)).to.be.fulfilled;
});
});
});
describe('should provide a detailed reject reason', function () {
it('when erroring out', function (done) {
var expectedOptions = {
uri: 'http://localhost:1/200',
simple: true,
resolveWithFullResponse: false,
tunnel: false,
callback: undefined
};
rp('http://localhost:1/200')
.then(function () {
done(new Error('Request should have errored out!'));
})
.catch(function (reason) {
expect(reason).to.be.an('object');
expect(reason.error.message).to.eql('connect ECONNREFUSED');
expect(reason.options).to.eql(expectedOptions);
expect(reason.response).to.eql(undefined);
expect(reason.statusCode).to.eql(undefined);
done();
})
.catch(done);
});
it('when getting a non-success status code', function (done) {
var expectedOptions = {
uri: 'http://localhost:4000/404',
simple: true,
resolveWithFullResponse: false,
tunnel: false,
callback: undefined
};
rp('http://localhost:4000/404')
.then(function () {
done(new Error('Request should have errored out!'));
})
.catch(function (reason) {
expect(reason).to.be.an('object');
expect(reason.error).to.eql('GET /404');
expect(reason.options).to.eql(expectedOptions);
expect(reason.response).to.be.an('object');
expect(reason.response.body).to.eql('GET /404');
expect(reason.statusCode).to.eql(404);
done();
})
.catch(done);
});
});
describe('should process the options', function () {
it('by correcting options.method with wrong case', function (done) {
rp({ uri: 'http://localhost:4000/500', method: 'Get' })
.then(function () {
done(new Error('A 500 response code should reject, not resolve'));
}).catch(function (reason) {
expect(reason.options.method).to.eql('GET');
expect(reason.error).to.eql('GET /500');
done();
})
.catch(done);
});
it('falling back to the default for a non-boolean options.simple', function () {
return expect(rp({ url: 'http://localhost:4000/404', simple: 0 })).to.be.rejected;
});
it('falling back to the default for a non-boolean options.resolveWithFullResponse', function () {
return rp({ url: 'http://localhost:4000/200', resolveWithFullResponse: 1 })
.then(function (body) {
expect(body).to.eql('GET /200');
});
});
it('by not cross-polluting the options of later requests', function () {
return Bluebird.resolve()
.then(function () {
var options = {
uri : 'http://localhost:4000/500', // UR - I -
method : 'GET',
simple : true
};
return expect(rp(options), 'First request').to.be.rejected;
})
.then(function () {
var options = {
url : 'http://localhost:4000/200', // UR - L -
method : 'GET',
simple : true
};
return expect(rp(options), 'Second request').to.be.fulfilled;
});
});
it('by not cross-polluting the options of parallel requests', function () {
return Bluebird.all([
rp({ uri: 'http://localhost:4000/200', simple: true }),
rp({ url: 'http://localhost:4000/500', simple: false }),
rp({ url: 'http://localhost:4000/201', resolveWithFullResponse: true })
])
.then(function (results) {
expect(results[0]).to.eql('GET /200');
expect(results[1]).to.eql('GET /500');
expect(results[2].body).to.eql('GET /201');
});
});
it('resolveWithFullResponse = true', function () {
var options = {
url: 'http://localhost:4000/200',
method: 'GET',
resolveWithFullResponse: true
};
return rp(options)
.then(function(response){
expect(response.statusCode).to.eql(200);
expect(response.request.method).to.eql('GET');
expect(response.body).to.eql('GET /200');
});
});
});
describe('should apply a transform function', function () {
it('that processes the body', function () {
var options = {
url: 'http://localhost:4000/200',
transform: function (body) {
return body.split('').reverse().join('');
}
};
return rp(options)
.then(function (transformedResponse) {
expect(transformedResponse).to.eql('002/ TEG');
});
});
it('that processes the full response', function () {
var options = {
url: 'http://localhost:4000/200',
transform: function (body, httpResponse) {
return httpResponse.body.split('').reverse().join('');
}
};
return rp(options)
.then(function (transformedResponse) {
expect(transformedResponse).to.eql('002/ TEG');
});
});
it('that returns a promise', function () {
var options = {
url: 'http://localhost:4000/200',
transform: function (body) {
return new Bluebird(function (resolve) {
setTimeout(function () {
resolve(body.split('').reverse().join(''));
});
});
}
};
return rp(options)
.then(function (transformedResponse) {
expect(transformedResponse).to.eql('002/ TEG');
});
});
it('that returns a rejected promise', function () {
var options = {
url: 'http://localhost:4000/200',
transform: function (body) {
return new Bluebird(function (resolve, reject) {
setTimeout(function () {
reject(new Error('Transform rejected!'));
});
});
}
};
return expect(rp(options)).to.be.rejected;
});
it('that throws an exception', function () {
var options = {
url: 'http://localhost:4000/200',
transform: function (body) {
throw new Error('Transform failed!');
}
};
return rp(options)
.then(function (transformedResponse) {
throw new Error('Request should not have been fulfilled!');
})
.catch(function (err) {
expect(err.message).to.eql('Transform failed!');
});
});
it('not if options.transform is not a function', function () {
var options = {
url: 'http://localhost:4000/200',
transform: {}
};
return rp(options)
.then(function (transformedResponse) {
expect(transformedResponse).to.eql('GET /200');
});
});
});
describe('should cover the HTTP method shortcuts', function () {
it('rp.get', function () {
return expect(rp.get('http://localhost:4000/200')).to.eventually.eql('GET /200');
});
it('rp.head', function () {
var options = {
url: 'http://localhost:4000/200',
resolveWithFullResponse: true
};
return rp.head(options)
.then(function (response) {
expect(response.statusCode).to.eql(200);
expect(response.request.method).to.eql('HEAD');
expect(response.body).to.eql('');
});
});
it('rp.post', function () {
return expect(rp.post('http://localhost:4000/200')).to.eventually.eql('POST /200');
});
it('rp.put', function () {
return expect(rp.put('http://localhost:4000/200')).to.eventually.eql('PUT /200');
});
it('rp.patch', function () {
return expect(rp.patch('http://localhost:4000/200')).to.eventually.eql('PATCH /200');
});
it('rp.del', function () {
return expect(rp.del('http://localhost:4000/200')).to.eventually.eql('DELETE /200');
});
});
describe('should cover the defaults mechanism', function () {
it("for overwriting the simple property's default", function () {
var nonSimpleRP = rp.defaults({ simple: false });
return expect(nonSimpleRP('http://localhost:4000/404')).to.be.fulfilled;
});
it("for overwriting the resolveWithFullResponse property's default", function () {
var rpWithFullResp = rp.defaults({ resolveWithFullResponse: true });
return rpWithFullResp('http://localhost:4000/200')
.then(function (response) {
expect(response.statusCode).to.eql(200);
});
});
it('for cascading overwrites', function () {
var nonSimpleRP = rp.defaults({ simple: false });
var nonSimpleRPWithFullResp = nonSimpleRP.defaults({ resolveWithFullResponse: true });
return nonSimpleRPWithFullResp('http://localhost:4000/404')
.then(function (response) {
expect(response.statusCode).to.eql(404);
});
});
it('and not interfere with the original Request-Promise instance', function () {
var rpWithFullResp = rp.defaults({ resolveWithFullResponse: true });
return Bluebird.all([
rpWithFullResp('http://localhost:4000/200'),
rp('http://localhost:4000/200')
])
.then(function (results) {
expect(results[0].statusCode).to.eql(200);
expect(results[1]).to.eql('GET /200');
});
});
it('for setting the transform property', function () {
var rpWithTransform = rp.defaults({ transform: function (body) { return body.split('/'); }});
return rpWithTransform('http://localhost:4000/200')
.then(function (body) {
expect(body).to.eql(['GET ', '200']);
});
});
});
describe('should still allow a callback', function () {
it('without using the then method', function (done) {
rp('http://localhost:4000/200', function (err, httpResponse, body) {
try {
if (err) { throw err; }
expect(body).to.eql('GET /200');
done();
} catch (e) {
done(e);
}
});
});
it('with using the then method at the same time', function () {
var callOrder = 0;
return new Bluebird(function (resolve, reject) {
var callback = function (err, httpResponse, body) {
try {
if (err) { throw err; }
expect(body).to.eql('GET /200');
expect(callOrder).to.eql(0);
callOrder += 1;
} catch (e) {
reject(e);
}
};
rp('http://localhost:4000/200', callback)
.then(function (body) {
expect(body).to.eql('GET /200');
expect(callOrder).to.eql(1);
callOrder += 1;
resolve();
});
});
});
it('and still work if the callback throws an exception', function (done) {
var callback = function (err, httpResponse, body) {
throw new Error();
};
var originalErrorListeners = process.domain.listeners('error');
process.domain.removeAllListeners('error');
var errorCount = 0;
process.domain.on('error', function () {
errorCount += 1;
});
rp('http://localhost:4000/200', callback)
.then(function (body) {
process.domain.removeAllListeners('error');
for ( var i = 0; i < originalErrorListeners.length; i+=1 ) {
process.domain.on('error', originalErrorListeners[i]);
}
expect(errorCount).to.eql(1);
expect(body).to.eql('GET /200');
done();
})
.catch(done);
});
it('without using the then method and no callback provided', function (done) {
rp('http://localhost:4000/234'); // 234 is only used here.
setTimeout(function () {
if (lastResponseBody !== 'GET /234') {
done (new Error('The server did not receive the request!'));
} else {
done();
}
}, 20);
});
});
describe('should be Promises/A+ compliant', function () {
it('and allow calling then after the request already finished', function (done) {
var req = rp('http://localhost:4000/200');
setTimeout(function () {
req.then(function () {
done();
});
}, 20);
});
it('and take only a fulfilled handler', function (done) {
rp('http://localhost:4000/200')
.then(function () {
done();
});
});
it('and take only a rejected handler', function (done) {
rp('http://localhost:4000/404')
.then(null, function () {
done();
});
});
it('by allowing chaining another then', function (done) {
rp('http://localhost:4000/200')
.then(function () {
})
.then(function () {
done();
});
});
it('by allowing chaining another then for rejection', function (done) {
rp('http://localhost:4000/404')
.then(function () {
})
.then(null, function () {
done();
});
});
it('by allowing chaining a catch', function (done) {
rp('http://localhost:4000/404')
.then(function () {
})
.catch(function () {
done();
});
});
it('and allow the then method to be invoked more than once', function (done) {
var countFulfillCalls = 0;
function onFulfilled() {
countFulfillCalls += 1;
if (countFulfillCalls === 3) {
done();
}
}
var req = rp('http://localhost:4000/200');
req.then(onFulfilled);
req.then(onFulfilled);
req.then(onFulfilled);
});
});
describe('should expose additional Bluebird methods', function () {
it('.catch(Function handler)', function (done) {
rp('http://localhost:4000/404')
.catch(function (reason) {
done();
});
});
it('.catch([Function ErrorClass|Function predicate...], Function handler)', function (done) {
rp({ uri: 'http://localhost:4000/200', transform: function () { throw new Error('Transform failed.'); } })
.catch(Error, function (err) {
done();
})
.catch(function (reason) {
done(new Error('Expected rejection reason to be an Error object.'));
});
});
it('.finally(Function handler)', function (done) {
rp('http://localhost:4000/200')
.finally(function () {
done();
});
});
it('.promise() to return the Bluebird promise itself', function () {
var context = { message: 'Hello World!'};
return rp('http://localhost:4000/200').promise().bind(context)
.then(function (body) {
expect(body).to.eql('GET /200');
expect(this).to.eql(context);
});
});
});
describe('should handle possibly unhandled rejections', function () {
var origStderrWrite, stderr;
beforeEach(function () {
origStderrWrite = process.stderr.write;
stderr = [];
process.stderr.write = function(string, encoding, fd) {
stderr.push(string);
};
});
afterEach(function () {
process.stderr.write = origStderrWrite;
});
it('by muting them if .then(...) was not called yet', function (done) {
rp('http://localhost:1/200', function (err) {
if (!err) {
done(new Error('The request should fail.'));
return;
}
setTimeout(function () {
if (stderr.length > 0) {
done(new Error('Observed unexpected output to stderr.'));
} else {
done();
}
});
});
});
it('by printing them if .then(...) was called without a rejection handler', function (done) {
rp('http://localhost:1/200', function (err) {
if (!err) {
done(new Error('The request should fail.'));
return;
}
setTimeout(function () {
if (stderr.length === 0) {
done(new Error('Observed no output to stderr.'));
} else {
done();
}
});
}).then(function () {}, null); // No rejection handler
});
it('and also printing them if .promise() was called without any further chaining', function (done) {
rp('http://localhost:1/200', function (err) {
if (!err) {
done(new Error('The request should fail.'));
return;
}
setTimeout(function () {
if (stderr.length === 0) {
done(new Error('Observed no output to stderr.'));
} else {
done();
}
});
}).promise(); // No further chaining
});
it('but not printing them if .then(...) was called with a rejection handler', function (done) {
rp('http://localhost:1/200', function (err) {
if (!err) {
done(new Error('The request should fail.'));
return;
}
setTimeout(function () {
if (stderr.length > 0) {
done(new Error('Observed unexpected output to stderr.'));
} else {
done();
}
});
}).then(function () {}, function () {});
});
it('and also not printing them if just .catch(...) was called with a rejection handler', function (done) {
rp('http://localhost:1/200', function (err) {
if (!err) {
done(new Error('The request should fail.'));
return;
}
setTimeout(function () {
if (stderr.length > 0) {
done(new Error('Observed unexpected output to stderr.'));
} else {
done();
}
});
}).catch(function () {});
});
it('and also not printing them if .promise() was called with a rejection handler given the chain further down', function (done) {
rp('http://localhost:1/200', function (err) {
if (!err) {
done(new Error('The request should fail.'));
return;
}
setTimeout(function () {
if (stderr.length > 0) {
done(new Error('Observed unexpected output to stderr.'));
} else {
done();
}
});
}).promise().catch(function () {});
});
it('and not interfere with Bluebird required by the user', function (done) {
Bluebird.reject(new Error());
setTimeout(function () {
if (stderr.length === 0) {
done(new Error('Observed no output to stderr.'));
} else {
done();
}
});
});
});
describe("should not alter Request's original behavior", function () {
it('for emitting errors with no listener', function () {
expect(function () {
rp({});
}).to.throw();
});
it('for emitting errors to the callback', function (done) {
rp({}, function (err) {
if (err) { done(); }
});
});
it('for registering a handler to the emitted complete event', function (done) {
rp('http://localhost:4000/200')
.on('complete', function (httpResponse, body) {
expect(httpResponse.statusCode).to.eql(200);
expect(body).to.eql('GET /200');
done();
});
});
it('for piping data while also the promise is used', function (done) {
var req = rp('http://localhost:4000/200');
var _data;
req.pipe(es.wait(function (err, data) {
_data = data.toString();
}));
req.then(function (body) {
expect(body).to.eql('GET /200');
expect(_data).to.eql('GET /200');
done();
});
});
it('for requests with a redirect', function () {
return rp('http://localhost:4000/302')
.then(function (body) {
expect(body).to.eql('GET /200');
});
});
});
describe('should still allow to require Request independently', function (done) {
it('by not interfering with Request required afterwards', function (done) {
childProcess.exec('node ' + path.join(__dirname, '../fixtures/require/afterwards.js'), function (err, stdout, stderr) {
expect(stdout).to.contain('rp: true, request: true');
done();
});
});
it('by not interfering with Request required beforehand', function (done) {
childProcess.exec('node ' + path.join(__dirname, '../fixtures/require/beforehand.js'), function (err, stdout, stderr) {
expect(stdout).to.contain('request: true, rp: true');
done();
});
});
it('by not interfering with Request required beforehand and afterwards being identical', function (done) {
childProcess.exec('node ' + path.join(__dirname, '../fixtures/require/beforehandAndAfterwards.js'), function (err, stdout, stderr) {
expect(stdout).to.contain('request1: true, rp: true, request2: true');
done();
});
});
});
describe('should run the examples', function () {
it('in the README', function () {
var options;
return Bluebird.resolve()
.then(function () {
return rp('http://www.google.com')
.then(function (body) {
expect(body).to.contain('</html>');
})
.catch(function (err) {
throw err;
});
// --> 'GET's and displays google.com
})
.then(function () {
options = {
uri : 'http://localhost:4000/200',
method : 'POST'
};
return rp(options)
.then(function (body) {
expect(body).to.eql('POST /200');
})
.catch(function (err) {
throw err;
});
// --> Displays response from server after post
})
.then(function () {
options.transform = function (data) { return data.length; };
return rp(options)
.then(function (body) {
expect(body).to.eql('POST /200'.length);
})
.catch(function (err) {
throw err;
});
// transform is called just before promise is fulfilled
// --> Displays length of response from server after post
})
.then(function () {
// Get full response after DELETE
options = {
method: 'DELETE',
uri: 'http://localhost:4000/200',
resolveWithFullResponse: true
};
return rp(options)
.then(function (response) {
expect(response.statusCode).to.eql(200);
})
.catch(function (err) {
throw err;
});
});
});
});
});