From 3cdc4d6e932f144c565a0c941041a934e105bb59 Mon Sep 17 00:00:00 2001 From: Luis Deschamps Rudge Date: Wed, 9 Aug 2017 20:14:11 -0300 Subject: [PATCH] Add flag to retry requests --- src/helper/request-builder.js | 4 ++++ src/web-auth/index.js | 10 ++++++++++ test/helper/request-builder.test.js | 10 ++++++++++ test/mock/request-mock.js | 5 +++++ test/web-auth/web-auth.test.js | 1 + 5 files changed, 30 insertions(+) diff --git a/src/helper/request-builder.js b/src/helper/request-builder.js index 9a228b15..807bbe5e 100644 --- a/src/helper/request-builder.js +++ b/src/helper/request-builder.js @@ -64,6 +64,7 @@ RequestObj.prototype.end = function(cb) { function RequestBuilder(options) { this._sendTelemetry = options._sendTelemetry === false ? options._sendTelemetry : true; this._telemetryInfo = options._telemetryInfo || null; + this._timesToRetryFailedRequests = options._timesToRetryFailedRequests; this.headers = options.headers || {}; } @@ -86,6 +87,9 @@ RequestBuilder.prototype.setCommonConfiguration = function(ongoingRequest, optio if (this._sendTelemetry) { ongoingRequest = ongoingRequest.set('Auth0-Client', this.getTelemetryData()); } + if (this._timesToRetryFailedRequests > 0) { + ongoingRequest = ongoingRequest.retry(this._timesToRetryFailedRequests); + } return ongoingRequest; }; diff --git a/src/web-auth/index.js b/src/web-auth/index.js index af9e3320..cfce3454 100644 --- a/src/web-auth/index.js +++ b/src/web-auth/index.js @@ -24,6 +24,7 @@ var CrossOriginAuthentication = require('./cross-origin-authentication'); * @param {String} [options.scope] scopes to be requested during Auth. e.g. `openid email` * @param {String} [options.audience] identifier of the resource server who will consume the access token issued after Auth * @param {Array} [options.plugins] + * @param {Number} [options._timesToRetryFailedRequests] Number of times to retry a failed request, according to {@link https://github.com/visionmedia/superagent/blob/master/lib/should-retry.js} * @see {@link https://auth0.com/docs/api/authentication} */ function WebAuth(options) { @@ -55,6 +56,11 @@ function WebAuth(options) { optional: true, type: 'object', message: '_telemetryInfo option is not valid' + }, + _timesToRetryFailedRequests: { + optional: true, + type: 'number', + message: '_timesToRetryFailedRequests option is not valid' } } ); @@ -78,6 +84,10 @@ function WebAuth(options) { ? this.baseOptions._sendTelemetry : true; + this.baseOptions._timesToRetryFailedRequests = options._timesToRetryFailedRequests + ? parseInt(options._timesToRetryFailedRequests, 0) + : 0; + this.baseOptions.tenant = (this.baseOptions.overrides && this.baseOptions.overrides.__tenant) || this.baseOptions.domain.split('.')[0]; diff --git a/test/helper/request-builder.test.js b/test/helper/request-builder.test.js index 7c2ef3b1..14ae2fa2 100644 --- a/test/helper/request-builder.test.js +++ b/test/helper/request-builder.test.js @@ -103,6 +103,16 @@ describe('helpers requestBuilder', function() { }); }); + it('should retry request', function() { + var retryTimes = 2; + var req = new RequestBuilder({ + _timesToRetryFailedRequests: retryTimes + }); + var handler = req.get('https://test.com').withCredentials().end(function(err, data) {}); + + expect(handler.request.willRetry).to.eql(retryTimes); + }); + it('should post stuff', function() { var req = new RequestBuilder({}); var handler = req diff --git a/test/mock/request-mock.js b/test/mock/request-mock.js index d44e68c2..1b68cd20 100644 --- a/test/mock/request-mock.js +++ b/test/mock/request-mock.js @@ -33,4 +33,9 @@ RequestMock.prototype.end = function(cb) { return this; }; +RequestMock.prototype.retry = function(times) { + this.willRetry = times; + return this; +}; + module.exports = RequestMock; diff --git a/test/web-auth/web-auth.test.js b/test/web-auth/web-auth.test.js index 326ebc77..08579c24 100644 --- a/test/web-auth/web-auth.test.js +++ b/test/web-auth/web-auth.test.js @@ -33,6 +33,7 @@ describe('auth0.WebAuth', function() { scope: 'openid name read:blog', audience: 'urn:site:demo:blog', _sendTelemetry: false, + _timesToRetryFailedRequests: 2, overrides: { __tenant: 'tenant1', __token_issuer: 'issuer1'