Added test for OAuth2Client#request retry, fixed implementation

This commit is contained in:
murgatroid99
2016-03-25 11:00:42 -07:00
parent 2b5ebfc85c
commit 3ce01ec28a
2 changed files with 40 additions and 2 deletions

View File

@@ -344,12 +344,17 @@ OAuth2Client.prototype.request = function(opts, callback) {
// Hook the callback routine to call the _postRequest method.
var postRequestCb = function(err, body, resp) {
// Automatically retry 401 and 403 responses
if (retry && err && (err.code == 401 || err.code == 403)) {
// if err is set, then getting credentials failed, and retrying won't help
if (retry && !err && resp &&
(resp.statusCode == 401 || resp.statusCode == 403)) {
/* It only makes sense to retry once, because the retry is intended to
* handle expiration-related failures. If refreshing the token does not
* fix the failure, then refreshing again probably won't help */
retry = false;
that.getRequestMetadata(unusedUri, authCb);
// Force token refresh
that.refreshAccessToken(function() {
that.getRequestMetadata(unusedUri, authCb);
});
} else {
that._postRequest(err, body, resp, callback);
}