Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #64 from nathan-boyd/master
Browse files Browse the repository at this point in the history
Updates to 401 Handling
  • Loading branch information
dougwilson committed Nov 5, 2015
2 parents 37fb478 + 23ad3c8 commit bc99431
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/fuel-rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ FuelRest.prototype._processRequest = function(options, callback) {
this.AuthClient.getAccessToken(clone(options.auth), function(err, authResponse) {
var authOptions;
var localError;
var retry;
var retry = false;
var consolidatedOpts = {};

if(err) {
Expand All @@ -97,18 +97,22 @@ FuelRest.prototype._processRequest = function(options, callback) {
return;
}

retry = options.retry || false;
authOptions = clone(options.auth);

delete options.retry;
delete options.auth;

options.uri = helpers.resolveUri(this.origin, options.uri);
options.headers = merge({}, this.defaultHeaders, options.headers);
options.headers.Authorization = options.headers.Authorization || 'Bearer ' + authResponse.accessToken;

if(!options.headers.Authorization) {
options.headers.Authorization = 'Bearer ' + authResponse.accessToken;
retry = options.retry || false;
}

delete options.retry;
delete options.auth;

consolidatedOpts.req = options;
consolidatedOpts.auth = authOptions;
consolidatedOpts.accessToken = authResponse.accessToken;
consolidatedOpts.retry = retry;

this._makeRequest(consolidatedOpts, callback);
Expand All @@ -129,6 +133,7 @@ FuelRest.prototype._makeRequest = function(consolidatedOpts, callback) {

// check if we should retry req
if(helpers.isValid401(res) && consolidatedOpts.retry) {
this.AuthClient.invalidateToken(consolidatedOpts.accessToken);
requestOptions.auth = consolidatedOpts.auth;
this.apiRequest(requestOptions, callback);
return;
Expand Down
61 changes: 61 additions & 0 deletions test/specs/fn-apiRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,38 @@ describe('apiRequest method', function() {
}, true);
});

it('should skip retry when Authorization header is provided and request 401s', function(done) {
var requestSpy;
var RestClient;

requestSpy = sinon.spy(FuelRest.prototype, 'apiRequest');

sinon.stub(FuelAuth.prototype, 'getAccessToken', function(options, callback) {
callback(null, { accessToken: 'testing', expiresIn: 3600 });
});

RestClient = new FuelRest(initOptions);

requestOptions.uri = routes.invalidToken;
requestOptions.retry = true;
requestOptions.auth = {
force: true
};
requestOptions.headers = {
Authorization: 'Bearer SomeToken'
};

RestClient.apiRequest(requestOptions, function() {
// error should be passed, and data should be null
expect(requestSpy.calledTwice).to.be.false;

FuelRest.prototype.apiRequest.restore();
FuelAuth.prototype.getAccessToken.restore();
// finish async test
done();
}, true);
});

it('should use a full URI if provided', function(done) {
requestOptions.uri = localhost + routes.get;

Expand All @@ -256,4 +288,33 @@ describe('apiRequest method', function() {
done();
});
});

describe('invalidating token', function() {
it('should tell auth client to invalide it\'s token', function(done) {
var invalidateSpy = sinon.stub(FuelAuth.prototype, 'invalidateToken');
var RestClient;

sinon.stub(FuelAuth.prototype, 'getAccessToken', function(options, callback) {
callback(null, { accessToken: 'testing', expiresIn: 3600 });
});

RestClient = new FuelRest(initOptions);

requestOptions.uri = routes.invalidToken;
requestOptions.retry = true;
requestOptions.auth = {
force: true
};

RestClient.apiRequest(requestOptions, function() {
expect(invalidateSpy.callCount).to.equal(1);

FuelAuth.prototype.getAccessToken.restore();
FuelAuth.prototype.invalidateToken.restore();

// finish async test
done();
}, true);
});
});
});

0 comments on commit bc99431

Please sign in to comment.