|
3 | 3 | decodeJWT, |
4 | 4 | generateCallbackId, |
5 | 5 | getAlgorithm, |
| 6 | + getItemAsync, |
6 | 7 | parseParametersFromURL, |
7 | 8 | parseResponseAPIVersion, |
8 | 9 | getCodeChallengeAndMethod, |
@@ -340,3 +341,49 @@ describe('validateUUID', () => { |
340 | 341 | } |
341 | 342 | }) |
342 | 343 | }) |
| 344 | + |
| 345 | +describe('getItemAsync', () => { |
| 346 | + const makeStorage = (initial: { [key: string]: string | null }) => { |
| 347 | + const data: { [key: string]: string | null } = { ...initial } |
| 348 | + return { |
| 349 | + getItem: jest.fn(async (key: string) => data[key] ?? null), |
| 350 | + setItem: jest.fn(async (key: string, value: string) => { |
| 351 | + data[key] = value |
| 352 | + }), |
| 353 | + removeItem: jest.fn(async (key: string) => { |
| 354 | + delete data[key] |
| 355 | + }), |
| 356 | + } |
| 357 | + } |
| 358 | + |
| 359 | + it('returns null when the storage value is missing', async () => { |
| 360 | + const storage = makeStorage({}) |
| 361 | + expect(await getItemAsync(storage, 'session')).toBeNull() |
| 362 | + }) |
| 363 | + |
| 364 | + it('returns null when the storage value is empty string', async () => { |
| 365 | + const storage = makeStorage({ session: '' }) |
| 366 | + expect(await getItemAsync(storage, 'session')).toBeNull() |
| 367 | + }) |
| 368 | + |
| 369 | + it('returns the parsed object for valid JSON', async () => { |
| 370 | + const session = { access_token: 'a', refresh_token: 'b', expires_at: 1 } |
| 371 | + const storage = makeStorage({ session: JSON.stringify(session) }) |
| 372 | + expect(await getItemAsync(storage, 'session')).toEqual(session) |
| 373 | + }) |
| 374 | + |
| 375 | + it('returns null when the storage value is not valid JSON', async () => { |
| 376 | + // Simulates corrupted chunked cookies: combined+decoded payload that is |
| 377 | + // not parseable. Returning the raw string would cause _recoverAndRefresh |
| 378 | + // to throw `TypeError: Cannot create property 'user' on string ...`. |
| 379 | + const storage = makeStorage({ session: '{"access_token":"abc' }) |
| 380 | + expect(await getItemAsync(storage, 'session')).toBeNull() |
| 381 | + }) |
| 382 | + |
| 383 | + it('returns null for a JSON-encoded primitive that auth callers do not expect', async () => { |
| 384 | + // JSON.parse('"hello"') succeeds and returns the string "hello", which is |
| 385 | + // valid behavior. We are only guarding against parse failures here. |
| 386 | + const storage = makeStorage({ session: '"hello"' }) |
| 387 | + expect(await getItemAsync(storage, 'session')).toEqual('hello') |
| 388 | + }) |
| 389 | +}) |
0 commit comments