Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enable Token Retries #481

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ export class GoogleToken {
throw new Error('No token to revoke.');
}
const url = GOOGLE_REVOKE_TOKEN_URL + this.accessToken;
await this.transporter.request({url});
await this.transporter.request({
url,
retry: true,
});
this.configure({
email: this.iss,
sub: this.sub,
Expand Down Expand Up @@ -337,6 +340,9 @@ export class GoogleToken {
},
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
responseType: 'json',
retryConfig: {
httpMethodsToRetry: ['POST'],
},
});
this.rawToken = r.data;
this.expiresAt =
Expand Down
28 changes: 24 additions & 4 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
gtoken.rawToken = {
access_token: token,
};
gtoken.revokeToken(err => {

Check warning on line 183 in test/index.ts

View workflow job for this annotation

GitHub Actions / lint

'err' is defined but never used
assert.strictEqual(gtoken.accessToken, undefined);
scope.done();
done();
Expand Down Expand Up @@ -248,7 +248,7 @@
it('should read .pem keyFile from file', done => {
const gtoken = new GoogleToken(TESTDATA_KEYFILE);
const scope = createGetTokenMock();
gtoken.getToken((err, token) => {

Check warning on line 251 in test/index.ts

View workflow job for this annotation

GitHub Actions / lint

'err' is defined but never used

Check warning on line 251 in test/index.ts

View workflow job for this annotation

GitHub Actions / lint

'token' is defined but never used
assert.deepStrictEqual(gtoken.key, KEYCONTENTS);
scope.done();
done();
Expand All @@ -258,7 +258,7 @@
it('should read .pem keyFile from file async', async () => {
const gtoken = new GoogleToken(TESTDATA_KEYFILE);
const scope = createGetTokenMock();
const token = await gtoken.getToken();

Check warning on line 261 in test/index.ts

View workflow job for this annotation

GitHub Actions / lint

'token' is assigned a value but never used
scope.done();
assert.deepStrictEqual(gtoken.key, KEYCONTENTS);
});
Expand All @@ -279,7 +279,7 @@

it('should return err if neither key nor keyfile are set', done => {
const gtoken = new GoogleToken();
gtoken.getToken((err, token) => {

Check warning on line 282 in test/index.ts

View workflow job for this annotation

GitHub Actions / lint

'token' is defined but never used
assert(err);
done();
});
Expand All @@ -288,7 +288,7 @@
it('should read .json key from file', done => {
const gtoken = new GoogleToken(TESTDATA_KEYFILEJSON);
const scope = createGetTokenMock();
gtoken.getToken((err, token) => {

Check warning on line 291 in test/index.ts

View workflow job for this annotation

GitHub Actions / lint

'token' is defined but never used
scope.done();
assert.strictEqual(err, null);
const parsed = JSON.parse(KEYJSONCONTENTS);
Expand All @@ -305,7 +305,7 @@
});
const gtoken = new GoogleToken(opts);
const scope = createGetTokenMock();
const token = await gtoken.getToken();

Check warning on line 308 in test/index.ts

View workflow job for this annotation

GitHub Actions / lint

'token' is assigned a value but never used
scope.done();
assert.deepStrictEqual(gtoken.key, KEYCONTENTS);
});
Expand Down Expand Up @@ -479,6 +479,26 @@
});
});

it('should retry on error', async () => {
const gtoken = new GoogleToken(TESTDATA);
const fakeToken = 'token';

const scopes = [
nock(GOOGLE_TOKEN_URLS[0])
.post(GOOGLE_TOKEN_URLS[1])
.replyWithError({code: 'ECONNRESET'}),
createGetTokenMock(200, {access_token: fakeToken}),
];

const token = await gtoken.getToken();

assert.strictEqual(token.access_token, fakeToken);

for (const scope of scopes) {
scope.done();
}
});

it('should use a custom transporter if one is provided', done => {
let customTransporterWasUsed = false;
const gtoken = new GoogleToken({
Expand All @@ -492,7 +512,7 @@
});
const fakeToken = 'nodeftw';
const scope = createGetTokenMock(200, {access_token: fakeToken});
gtoken.getToken((err, token) => {

Check warning on line 515 in test/index.ts

View workflow job for this annotation

GitHub Actions / lint

'token' is defined but never used
scope.done();
assert.strictEqual(err, null);
assert(customTransporterWasUsed);
Expand Down Expand Up @@ -526,8 +546,8 @@
it('should set and return correct properties on error', done => {
const ERROR = 'An error occurred.';
const gtoken = new GoogleToken(TESTDATA);
const scope = createGetTokenMock(500, {error: ERROR});
gtoken.getToken((err, token) => {
const scope = createGetTokenMock(400, {error: ERROR});
gtoken.getToken(err => {
scope.done();
assert(err);
assert.strictEqual(gtoken.rawToken, undefined);
Expand All @@ -543,8 +563,8 @@
const ERROR = 'error_name';
const DESCRIPTION = 'more detailed message';
const RESPBODY = {error: ERROR, error_description: DESCRIPTION};
const scope = createGetTokenMock(500, RESPBODY);
gtoken.getToken((err, token) => {
const scope = createGetTokenMock(400, RESPBODY);
gtoken.getToken(err => {
scope.done();
assert(err instanceof Error);
if (err) {
Expand All @@ -558,7 +578,7 @@
const gtoken = new GoogleToken(TESTDATA);
const message = 'Request failed with status code 404';
const scope = createGetTokenMock(404);
gtoken.getToken((err, token) => {

Check warning on line 581 in test/index.ts

View workflow job for this annotation

GitHub Actions / lint

'token' is defined but never used
scope.done();
assert(err instanceof Error);
if (err) assert.strictEqual(err.message, message);
Expand Down
Loading