|
8 | 8 | refreshAuthorization, |
9 | 9 | registerClient, |
10 | 10 | discoverOAuthProtectedResourceMetadata, |
11 | | - extractFieldFromWwwAuth, |
12 | 11 | extractWWWAuthenticateParams, |
13 | 12 | auth, |
14 | 13 | type OAuthClientProvider, |
@@ -36,50 +35,6 @@ describe('OAuth Authorization', () => { |
36 | 35 | mockFetch.mockReset(); |
37 | 36 | }); |
38 | 37 |
|
39 | | - describe('extractFieldFromWwwAuth', () => { |
40 | | - function mockResponseWithWWWAuthenticate(headerValue: string): Response { |
41 | | - return { |
42 | | - headers: { |
43 | | - get: vi.fn(name => (name === 'WWW-Authenticate' ? headerValue : null)) |
44 | | - } |
45 | | - } as unknown as Response; |
46 | | - } |
47 | | - |
48 | | - it('returns the value of a quoted field', () => { |
49 | | - const mockResponse = mockResponseWithWWWAuthenticate(`Bearer realm="example", field="value"`); |
50 | | - expect(extractFieldFromWwwAuth(mockResponse, 'field')).toBe('value'); |
51 | | - }); |
52 | | - |
53 | | - it('returns the value of an unquoted field', () => { |
54 | | - const mockResponse = mockResponseWithWWWAuthenticate(`Bearer realm=example, field=value`); |
55 | | - expect(extractFieldFromWwwAuth(mockResponse, 'field')).toBe('value'); |
56 | | - }); |
57 | | - |
58 | | - it('returns the correct value when multiple parameters are present', () => { |
59 | | - const mockResponse = mockResponseWithWWWAuthenticate( |
60 | | - `Bearer realm="api", error="invalid_token", field="test_value", scope="admin"` |
61 | | - ); |
62 | | - expect(extractFieldFromWwwAuth(mockResponse, 'field')).toBe('test_value'); |
63 | | - }); |
64 | | - |
65 | | - it('returns null if the field is not present', () => { |
66 | | - const mockResponse = mockResponseWithWWWAuthenticate(`Bearer realm="api", scope="admin"`); |
67 | | - expect(extractFieldFromWwwAuth(mockResponse, 'missing_field')).toBeNull(); |
68 | | - }); |
69 | | - |
70 | | - it('returns null if the WWW-Authenticate header is missing', () => { |
71 | | - const mockResponse = { headers: new Headers() } as unknown as Response; |
72 | | - expect(extractFieldFromWwwAuth(mockResponse, 'field')).toBeNull(); |
73 | | - }); |
74 | | - |
75 | | - it('handles fields with special characters in quotes', () => { |
76 | | - const mockResponse = mockResponseWithWWWAuthenticate( |
77 | | - `Bearer error="invalid_token", error_description="The token has expired, please re-authenticate."` |
78 | | - ); |
79 | | - expect(extractFieldFromWwwAuth(mockResponse, 'error_description')).toBe('The token has expired, please re-authenticate.'); |
80 | | - }); |
81 | | - }); |
82 | | - |
83 | 38 | describe('extractWWWAuthenticateParams', () => { |
84 | 39 | it('returns resource metadata url when present', async () => { |
85 | 40 | const resourceUrl = 'https://resource.example.com/.well-known/oauth-protected-resource'; |
@@ -140,6 +95,16 @@ describe('OAuth Authorization', () => { |
140 | 95 |
|
141 | 96 | expect(extractWWWAuthenticateParams(mockResponse)).toEqual({ scope: scope }); |
142 | 97 | }); |
| 98 | + |
| 99 | + it('returns error when present', async () => { |
| 100 | + const mockResponse = { |
| 101 | + headers: { |
| 102 | + get: vi.fn(name => (name === 'WWW-Authenticate' ? `Bearer error="insufficient_scope", scope="admin"` : null)) |
| 103 | + } |
| 104 | + } as unknown as Response; |
| 105 | + |
| 106 | + expect(extractWWWAuthenticateParams(mockResponse)).toEqual({ error: 'insufficient_scope', scope: 'admin' }); |
| 107 | + }); |
143 | 108 | }); |
144 | 109 |
|
145 | 110 | describe('discoverOAuthProtectedResourceMetadata', () => { |
|
0 commit comments