-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathstrategies.test.js
191 lines (162 loc) · 5.44 KB
/
strategies.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
'use strict';
var request = require('../');
var t = require('chai').assert;
describe('RetryStrategies', function () {
describe('Default strategies', function () {
it('should have a strategy `HTTPError` to only retry on HTTP errors', function () {
checkHTTPErrors(request.RetryStrategies.HTTPError);
});
it('should have a strategy `NetworkError` to only retry on nodejs network errors', function () {
checkNetworkErrors(request.RetryStrategies.NetworkError, request.RetryStrategies.NetworkError.RETRIABLE_ERRORS);
});
it('should have a strategy `HTTPOrNetworkError` to only retry on nodejs network and HTTP errors', function () {
checkHTTPErrors(request.RetryStrategies.HTTPOrNetworkError);
checkNetworkErrors(request.RetryStrategies.HTTPOrNetworkError, request.RetryStrategies.NetworkError.RETRIABLE_ERRORS);
});
});
describe('Custom strategies', function () {
it('should overwrite `options` object if strategy returned it', function (done) {
var strategy = function (err, response, body, options) {
options.url = 'http://www.filltext.com/?rows=1&err=200'; //overwrite url to return 200
return {
mustRetry: true,
options: options,
};
};
request({
url: 'http://www.filltext.com/?rows=1&err=500', // returns a 500 status
maxAttempts: 3,
retryDelay: 100,
retryStrategy: strategy
}, function(err, response, body) {
if(err) done(err);
t.strictEqual(200, response.statusCode);
done();
});
});
it('should not overwrite `options` object if strategy did not returned it', function (done) {
var strategy = function (err, response, body, options) {
options.url = 'http://www.filltext.com/?rows=1&err=200'; //overwrite url to return 200
//do not return `options`
return {
mustRetry: true,
};
};
request({
url: 'http://www.filltext.com/?rows=1&err=500', // returns a 500 status
maxAttempts: 3,
retryDelay: 100,
retryStrategy: strategy
}, function(err, response, body) {
if(err) done(err);
t.strictEqual(500, response.statusCode);
done();
});
});
it('should allow boolean true return value', function (done) {
var strategy = function (err, response, body) {
return true;
};
request({
url: 'http://www.filltext.com/?rows=1',
maxAttempts: 2,
retryDelay: 100,
retryStrategy: strategy
}, function(err, response, body) {
if(err) done(err);
t.strictEqual(2, this.attempts); // maxAttempts reached
done();
});
});
it('should allow boolean false return value', function (done) {
var strategy = function (err, response, body) {
return false;
};
request({
url: 'http://www.filltext.com/?rows=1',
maxAttempts: 2,
retryDelay: 100,
retryStrategy: strategy
}, function(err, response, body) {
if(err) done(err);
t.strictEqual(1, this.attempts); // maxAttempts NOT reached
done();
});
});
it('should allow mustRetry object with true return value', function (done) {
var strategy = function (err, response, body) {
return {
mustRetry: true,
};
};
request({
url: 'http://www.filltext.com/?rows=1',
maxAttempts: 2,
retryDelay: 100,
retryStrategy: strategy
}, function(err, response, body) {
if(err) done(err);
t.strictEqual(2, this.attempts); // maxAttempts reached
done();
});
});
it('should allow mustRetry object with false return value', function (done) {
var strategy = function (err, response, body) {
return {
mustRetry: false,
};
};
request({
url: 'http://www.filltext.com/?rows=1',
maxAttempts: 2,
retryDelay: 100,
retryStrategy: strategy
}, function(err, response, body) {
if(err) done(err);
t.strictEqual(1, this.attempts); // maxAttempts NOT reached
done();
});
})
it('should work with an async strategy', function (done) {
var strategy = function (err, response, body) {
return new Promise(function(resolve, reject) {
setTimeout(function () {resolve({mustRetry: false})}, 500);
});
};
request({
url: 'http://www.filltext.com/?rows=1',
maxAttempts: 2,
retryDelay: 100,
retryStrategy: strategy
}, function(err, response, body) {
if(err) done(err);
t.strictEqual(1, this.attempts); // maxAttempts NOT reached
done();
});
});
});
});
function checkNetworkErrors(strategy, errorCodes) {
errorCodes.forEach(function (errorCode) {
var err = new Error();
err.code = errorCode;
t.ok(strategy(err), 'error code ' + errorCode + ' is recoverable');
});
['hello', 'plop'].forEach(function (errorCode) {
var err = new Error();
err.code = errorCode;
t.ok(!strategy(err), 'error code ' + errorCode + ' is not recoverable');
});
}
function checkHTTPErrors(strategy) {
[400, 301, 600].forEach(function (code) {
t.ok(!strategy(null, {
statusCode: code
}), code + ' error is not recoverable');
});
[429, 500, 599].forEach(function (code) {
t.ok(strategy(null, {
statusCode: code
}), code + ' error is recoverable');
});
}