|
2 | 2 |
|
3 | 3 | var assert = require('assert');
|
4 | 4 | var retryRequest = require('./index.js');
|
| 5 | +var through = require('through2'); |
5 | 6 |
|
6 | 7 | describe('retry-request', function () {
|
7 | 8 | var URI_404 = 'http://yahoo.com/theblahstore';
|
@@ -29,6 +30,89 @@ describe('retry-request', function () {
|
29 | 30 | done();
|
30 | 31 | });
|
31 | 32 | });
|
| 33 | + |
| 34 | + it('exposes an `abort` fuction to match request', function (done) { |
| 35 | + var retryStream = retryRequest(URI_NON_EXISTENT); |
| 36 | + |
| 37 | + retryStream.on('error', function () { |
| 38 | + assert.equal(typeof retryStream.abort, 'function'); |
| 39 | + done(); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('works on the last attempt', function (done) { |
| 44 | + var numAborts = 0; |
| 45 | + var numAttempts = 0; |
| 46 | + |
| 47 | + var opts = { |
| 48 | + request: function () { |
| 49 | + numAttempts++; |
| 50 | + |
| 51 | + var fakeRequestStream = through(); |
| 52 | + fakeRequestStream.abort = function () { |
| 53 | + numAborts++; |
| 54 | + }; |
| 55 | + |
| 56 | + var response = numAttempts < 3 ? { statusCode: 503 } : { statusCode: 200 }; |
| 57 | + setImmediate(function () { |
| 58 | + fakeRequestStream.emit('response', response); |
| 59 | + |
| 60 | + setImmediate(function () { |
| 61 | + // They all emit 'complete', but the user's stream should only get |
| 62 | + // the last one. |
| 63 | + fakeRequestStream.emit('complete', numAttempts); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + return fakeRequestStream; |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + retryRequest(URI_404, opts) |
| 72 | + .on('error', done) |
| 73 | + .on('complete', function (numAttempts) { |
| 74 | + assert.strictEqual(numAborts, 2); |
| 75 | + assert.deepEqual(numAttempts, 3); |
| 76 | + done(); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('never succeeds', function (done) { |
| 81 | + var numAborts = 0; |
| 82 | + var numAttempts = 0; |
| 83 | + |
| 84 | + var opts = { |
| 85 | + request: function () { |
| 86 | + numAttempts++; |
| 87 | + |
| 88 | + var fakeRequestStream = through(); |
| 89 | + fakeRequestStream.abort = function () { |
| 90 | + numAborts++; |
| 91 | + }; |
| 92 | + |
| 93 | + var response = { statusCode: 503 }; |
| 94 | + setImmediate(function () { |
| 95 | + fakeRequestStream.emit('response', response); |
| 96 | + |
| 97 | + setImmediate(function () { |
| 98 | + // They all emit 'complete', but the user's stream should only get |
| 99 | + // the last one. |
| 100 | + fakeRequestStream.emit('complete', numAttempts); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + return fakeRequestStream; |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + retryRequest(URI_404, opts) |
| 109 | + .on('error', done) |
| 110 | + .on('complete', function (numAttempts) { |
| 111 | + assert.strictEqual(numAborts, 2); |
| 112 | + assert.strictEqual(numAttempts, 3); |
| 113 | + done(); |
| 114 | + }); |
| 115 | + }); |
32 | 116 | });
|
33 | 117 |
|
34 | 118 | describe('callbacks', function () {
|
@@ -59,7 +143,7 @@ describe('retry-request', function () {
|
59 | 143 | var shouldRetryFnCalled = false;
|
60 | 144 |
|
61 | 145 | var opts = {
|
62 |
| - retries: 1, // so that our retry function is only called once and |
| 146 | + retries: 1, // so that our retry function is only called once |
63 | 147 |
|
64 | 148 | shouldRetryFn: function () {
|
65 | 149 | shouldRetryFnCalled = true;
|
|
0 commit comments