|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | + |
| 3 | +import type { OAuth } from '../oauth/OAuth' |
| 4 | + |
| 5 | +import { addTokenRefreshMiddleware } from './addTokenRefreshMiddleware' |
| 6 | + |
| 7 | +// Minimal fetch mock helper |
| 8 | +function mockResponse(status: number, body?: object): Response { |
| 9 | + return new Response(body ? JSON.stringify(body) : null, { |
| 10 | + status, |
| 11 | + headers: { 'Content-Type': 'application/json' } |
| 12 | + }) |
| 13 | +} |
| 14 | + |
| 15 | +function createMockOAuth( |
| 16 | + refreshBehaviour: () => Promise<string | null>, |
| 17 | + hasRefreshToken = true |
| 18 | +): OAuth { |
| 19 | + return { |
| 20 | + hasRefreshToken, |
| 21 | + refreshAccessToken: refreshBehaviour |
| 22 | + } as unknown as OAuth |
| 23 | +} |
| 24 | + |
| 25 | +describe('addTokenRefreshMiddleware', () => { |
| 26 | + beforeEach(() => { |
| 27 | + vi.restoreAllMocks() |
| 28 | + }) |
| 29 | + |
| 30 | + it('passes through non-401 responses unchanged', async () => { |
| 31 | + const oauth = createMockOAuth(async () => 'token') |
| 32 | + const mw = addTokenRefreshMiddleware({ oauth }) |
| 33 | + const response = mockResponse(200, { data: 'ok' }) |
| 34 | + |
| 35 | + const result = await mw.post!({ |
| 36 | + fetch, |
| 37 | + url: 'https://api.example.com/v1/users/me', |
| 38 | + init: {}, |
| 39 | + response |
| 40 | + }) |
| 41 | + |
| 42 | + expect(result).toBe(response) |
| 43 | + }) |
| 44 | + |
| 45 | + it('passes through 401 without calling refreshAccessToken when unauthenticated', async () => { |
| 46 | + const refreshFn = vi.fn() |
| 47 | + const oauth = createMockOAuth(refreshFn, false) |
| 48 | + const mw = addTokenRefreshMiddleware({ oauth }) |
| 49 | + const response = mockResponse(401) |
| 50 | + |
| 51 | + const result = await mw.post!({ |
| 52 | + fetch, |
| 53 | + url: 'https://api.example.com/v1/users/me', |
| 54 | + init: {}, |
| 55 | + response |
| 56 | + }) |
| 57 | + |
| 58 | + expect(result).toBe(response) |
| 59 | + expect(refreshFn).not.toHaveBeenCalled() |
| 60 | + }) |
| 61 | + |
| 62 | + it('passes through 401 when refresh returns null (no refresh token)', async () => { |
| 63 | + const oauth = createMockOAuth(async () => null) |
| 64 | + const mw = addTokenRefreshMiddleware({ oauth }) |
| 65 | + const response = mockResponse(401) |
| 66 | + |
| 67 | + const result = await mw.post!({ |
| 68 | + fetch, |
| 69 | + url: 'https://api.example.com/v1/users/me', |
| 70 | + init: {}, |
| 71 | + response |
| 72 | + }) |
| 73 | + |
| 74 | + expect(result).toBe(response) |
| 75 | + }) |
| 76 | + |
| 77 | + it('refreshes and retries on 401 when refresh succeeds', async () => { |
| 78 | + const oauth = createMockOAuth(async () => 'new-access') |
| 79 | + const retryResponse = mockResponse(200, { data: 'success' }) |
| 80 | + const contextFetch = vi.fn().mockResolvedValueOnce(retryResponse) |
| 81 | + |
| 82 | + const mw = addTokenRefreshMiddleware({ oauth }) |
| 83 | + |
| 84 | + const result = await mw.post!({ |
| 85 | + fetch: contextFetch, |
| 86 | + url: 'https://api.example.com/v1/tracks/123', |
| 87 | + init: { |
| 88 | + method: 'GET', |
| 89 | + headers: { Authorization: 'Bearer expired-access' } |
| 90 | + }, |
| 91 | + response: mockResponse(401) |
| 92 | + }) |
| 93 | + |
| 94 | + // Original request was retried with new token |
| 95 | + expect(contextFetch).toHaveBeenCalledWith( |
| 96 | + 'https://api.example.com/v1/tracks/123', |
| 97 | + expect.objectContaining({ |
| 98 | + headers: expect.objectContaining({ |
| 99 | + Authorization: 'Bearer new-access' |
| 100 | + }) |
| 101 | + }) |
| 102 | + ) |
| 103 | + |
| 104 | + expect(result).toBe(retryResponse) |
| 105 | + }) |
| 106 | + |
| 107 | + it('surfaces 401 when refresh fails', async () => { |
| 108 | + const oauth = createMockOAuth(async () => null) |
| 109 | + const mw = addTokenRefreshMiddleware({ oauth }) |
| 110 | + const original401 = mockResponse(401) |
| 111 | + |
| 112 | + const result = await mw.post!({ |
| 113 | + fetch, |
| 114 | + url: 'https://api.example.com/v1/tracks/123', |
| 115 | + init: {}, |
| 116 | + response: original401 |
| 117 | + }) |
| 118 | + |
| 119 | + expect(result).toBe(original401) |
| 120 | + }) |
| 121 | + |
| 122 | + it('surfaces 401 when refreshAccessToken throws', async () => { |
| 123 | + const oauth = createMockOAuth(async () => { |
| 124 | + throw new Error('network failure') |
| 125 | + }) |
| 126 | + const mw = addTokenRefreshMiddleware({ oauth }) |
| 127 | + const original401 = mockResponse(401) |
| 128 | + |
| 129 | + const result = await mw.post!({ |
| 130 | + fetch, |
| 131 | + url: 'https://api.example.com/v1/tracks/123', |
| 132 | + init: {}, |
| 133 | + response: original401 |
| 134 | + }) |
| 135 | + |
| 136 | + expect(result).toBe(original401) |
| 137 | + }) |
| 138 | +}) |
0 commit comments