Skip to content

Commit 5746587

Browse files
committed
Add retry code test
1 parent dd7ccdd commit 5746587

File tree

6 files changed

+63
-360
lines changed

6 files changed

+63
-360
lines changed

packages/app-check/src/api.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ function _activate(
126126
newState.cachedTokenPromise = readTokenFromStorage(app).then(cachedToken => {
127127
if (cachedToken && isValid(cachedToken)) {
128128
setState(app, { ...getState(app), token: cachedToken });
129-
console.log('notifying token listeners');
130129
// notify all listeners with the cached token
131130
notifyTokenListeners(app, { token: cachedToken.token });
132131
}

packages/app-check/src/internal-api.test.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ import * as reCAPTCHA from './recaptcha';
3838
import * as client from './client';
3939
import * as storage from './storage';
4040
import * as util from './util';
41+
import { logger } from './logger';
4142
import { getState, clearState, setState, getDebugState } from './state';
4243
import { AppCheckTokenListener } from './public-types';
4344
import { Deferred, FirebaseError } from '@firebase/util';
4445
import { ReCaptchaV3Provider } from './providers';
4546
import { AppCheckService } from './factory';
4647
import { ListenerType } from './types';
48+
import { AppCheckError } from './errors';
4749

4850
const fakeRecaptchaToken = 'fake-recaptcha-token';
4951
const fakeRecaptchaAppCheckToken = {
@@ -362,24 +364,47 @@ describe('internal api', () => {
362364
);
363365
expect(token).to.deep.equal({ token: fakeRecaptchaAppCheckToken.token });
364366
});
365-
it('throttle', async () => {
367+
it('throttles exponentially on 503', async () => {
366368
const appCheck = initializeAppCheck(app, {
367369
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
368370
});
369-
stub(
370-
client,
371-
'exchangeToken'
372-
).returns(
371+
const warnStub = stub(logger, 'warn');
372+
stub(client, 'exchangeToken').returns(
373373
Promise.reject(
374-
new FirebaseError('test-error', 'test error msg', { httpStatus: 503 })
374+
new FirebaseError(
375+
AppCheckError.FETCH_STATUS_ERROR,
376+
'test error msg',
377+
{ httpStatus: 503 }
378+
)
375379
)
376380
);
377381

378382
const token = await getToken(appCheck as AppCheckService);
379383

380-
expect(token.error?.message).to.equal(
381-
'sdfa'
384+
expect(token.error?.message).to.include('503');
385+
expect(token.error?.message).to.include('00m');
386+
expect(warnStub.args[0][0]).to.include('503');
387+
});
388+
it('throttles 1d on 403', async () => {
389+
const appCheck = initializeAppCheck(app, {
390+
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
391+
});
392+
const warnStub = stub(logger, 'warn');
393+
stub(client, 'exchangeToken').returns(
394+
Promise.reject(
395+
new FirebaseError(
396+
AppCheckError.FETCH_STATUS_ERROR,
397+
'test error msg',
398+
{ httpStatus: 403 }
399+
)
400+
)
382401
);
402+
403+
const token = await getToken(appCheck as AppCheckService);
404+
405+
expect(token.error?.message).to.include('403');
406+
expect(token.error?.message).to.include('1d');
407+
expect(warnStub.args[0][0]).to.include('403');
383408
});
384409
});
385410

packages/app-check/src/internal-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export async function getToken(
139139
}
140140
token = await state.exchangeTokenPromise;
141141
} catch (e) {
142-
if ((e as FirebaseError).code === AppCheckError.THROTTLED) {
142+
if ((e as FirebaseError).code === `appCheck/${AppCheckError.THROTTLED}`) {
143143
// Warn if throttled, but do not treat it as an error.
144144
logger.warn((e as FirebaseError).message);
145145
} else {

0 commit comments

Comments
 (0)