Skip to content

Commit 74cc51c

Browse files
ototheachipgpt
authored andcommitted
fix tests
1 parent d9251b2 commit 74cc51c

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

src/client/auth.test.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,50 +37,51 @@ describe('OAuth Authorization', () => {
3737
});
3838

3939
it('returns scope when present', async () => {
40-
const resourceUrl = 'https://resource.example.com/.well-known/oauth-protected-resource';
40+
const scope = 'read';
4141
const mockResponse = {
4242
headers: {
43-
get: jest.fn(name =>
44-
name === 'WWW-Authenticate' ? `Bearer realm="mcp", resource_metadata="${resourceUrl}", scope="read"` : null
45-
)
43+
get: jest.fn(name => (name === 'WWW-Authenticate' ? `Bearer realm="mcp", scope="${scope}"` : null))
4644
}
4745
} as unknown as Response;
4846

49-
expect(extractWWWAuthenticateParams(mockResponse)).toEqual({ resourceMetadataUrl: new URL(resourceUrl), scope: 'read' });
47+
expect(extractWWWAuthenticateParams(mockResponse)).toEqual({ scope: 'read' });
5048
});
5149

5250
it('returns empty object if not bearer', async () => {
5351
const resourceUrl = 'https://resource.example.com/.well-known/oauth-protected-resource';
52+
const scope = 'read';
5453
const mockResponse = {
5554
headers: {
56-
get: jest.fn(name => (name === 'WWW-Authenticate' ? `Basic realm="mcp", resource_metadata="${resourceUrl}"` : null))
55+
get: jest.fn(name =>
56+
name === 'WWW-Authenticate' ? `Basic realm="mcp", resource_metadata="${resourceUrl}", scope="${scope}"` : null
57+
)
5758
}
5859
} as unknown as Response;
5960

60-
expect(extractWWWAuthenticateParams(mockResponse)).toBe({});
61+
expect(extractWWWAuthenticateParams(mockResponse)).toEqual({});
6162
});
6263

6364
it('returns empty object if resource_metadata and scope not present', async () => {
6465
const mockResponse = {
6566
headers: {
66-
get: jest.fn(name => (name === 'WWW-Authenticate' ? `Basic realm="mcp"` : null))
67+
get: jest.fn(name => (name === 'WWW-Authenticate' ? `Bearer realm="mcp"` : null))
6768
}
6869
} as unknown as Response;
6970

70-
expect(extractWWWAuthenticateParams(mockResponse)).toBe({});
71+
expect(extractWWWAuthenticateParams(mockResponse)).toEqual({});
7172
});
7273

7374
it('returns undefined resourceMetadataUrl on invalid url', async () => {
7475
const resourceUrl = 'invalid-url';
7576
const mockResponse = {
7677
headers: {
7778
get: jest.fn(name =>
78-
name === 'WWW-Authenticate' ? `Basic realm="mcp", resource_metadata="${resourceUrl}" scope="read"` : null
79+
name === 'WWW-Authenticate' ? `Bearer realm="mcp", resource_metadata="${resourceUrl}", scope="read"` : null
7980
)
8081
}
8182
} as unknown as Response;
8283

83-
expect(extractWWWAuthenticateParams(mockResponse)).toBe({ scope: 'read' });
84+
expect(extractWWWAuthenticateParams(mockResponse)).toEqual({ scope: 'read' });
8485
});
8586
});
8687

src/client/middleware.test.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ jest.mock('../client/auth.js', () => {
77
return {
88
...actual,
99
auth: jest.fn(),
10-
extractResourceMetadataUrl: jest.fn()
10+
extractWWWAuthenticateParams: jest.fn()
1111
};
1212
});
1313

14-
import { auth, extractResourceMetadataUrl } from './auth.js';
14+
import { auth, extractWWWAuthenticateParams } from './auth.js';
1515

1616
const mockAuth = auth as jest.MockedFunction<typeof auth>;
17-
const mockExtractResourceMetadataUrl = extractResourceMetadataUrl as jest.MockedFunction<typeof extractResourceMetadataUrl>;
17+
const mockExtractWWWAuthenticateParams = extractWWWAuthenticateParams as jest.MockedFunction<typeof extractWWWAuthenticateParams>;
1818

1919
describe('withOAuth', () => {
2020
let mockProvider: jest.Mocked<OAuthClientProvider>;
@@ -129,8 +129,11 @@ describe('withOAuth', () => {
129129

130130
mockFetch.mockResolvedValueOnce(unauthorizedResponse).mockResolvedValueOnce(successResponse);
131131

132-
const mockResourceUrl = new URL('https://oauth.example.com/.well-known/oauth-protected-resource');
133-
mockExtractResourceMetadataUrl.mockReturnValue(mockResourceUrl);
132+
const mockWWWAuthenticateParams = {
133+
resourceMetadataUrl: new URL('https://oauth.example.com/.well-known/oauth-protected-resource'),
134+
scope: 'read'
135+
};
136+
mockExtractWWWAuthenticateParams.mockReturnValue(mockWWWAuthenticateParams);
134137
mockAuth.mockResolvedValue('AUTHORIZED');
135138

136139
const enhancedFetch = withOAuth(mockProvider, 'https://api.example.com')(mockFetch);
@@ -141,7 +144,8 @@ describe('withOAuth', () => {
141144
expect(mockFetch).toHaveBeenCalledTimes(2);
142145
expect(mockAuth).toHaveBeenCalledWith(mockProvider, {
143146
serverUrl: 'https://api.example.com',
144-
resourceMetadataUrl: mockResourceUrl,
147+
resourceMetadataUrl: mockWWWAuthenticateParams.resourceMetadataUrl,
148+
scope: mockWWWAuthenticateParams.scope,
145149
fetchFn: mockFetch
146150
});
147151

@@ -172,8 +176,11 @@ describe('withOAuth', () => {
172176

173177
mockFetch.mockResolvedValueOnce(unauthorizedResponse).mockResolvedValueOnce(successResponse);
174178

175-
const mockResourceUrl = new URL('https://oauth.example.com/.well-known/oauth-protected-resource');
176-
mockExtractResourceMetadataUrl.mockReturnValue(mockResourceUrl);
179+
const mockWWWAuthenticateParams = {
180+
resourceMetadataUrl: new URL('https://oauth.example.com/.well-known/oauth-protected-resource'),
181+
scope: 'read'
182+
};
183+
mockExtractWWWAuthenticateParams.mockReturnValue(mockWWWAuthenticateParams);
177184
mockAuth.mockResolvedValue('AUTHORIZED');
178185

179186
// Test without baseUrl - should extract from request URL
@@ -185,7 +192,8 @@ describe('withOAuth', () => {
185192
expect(mockFetch).toHaveBeenCalledTimes(2);
186193
expect(mockAuth).toHaveBeenCalledWith(mockProvider, {
187194
serverUrl: 'https://api.example.com', // Should be extracted from request URL
188-
resourceMetadataUrl: mockResourceUrl,
195+
resourceMetadataUrl: mockWWWAuthenticateParams.resourceMetadataUrl,
196+
scope: mockWWWAuthenticateParams.scope,
189197
fetchFn: mockFetch
190198
});
191199

@@ -203,7 +211,7 @@ describe('withOAuth', () => {
203211
});
204212

205213
mockFetch.mockResolvedValue(new Response('Unauthorized', { status: 401 }));
206-
mockExtractResourceMetadataUrl.mockReturnValue(undefined);
214+
mockExtractWWWAuthenticateParams.mockReturnValue({});
207215
mockAuth.mockResolvedValue('REDIRECT');
208216

209217
// Test without baseUrl
@@ -222,7 +230,7 @@ describe('withOAuth', () => {
222230
});
223231

224232
mockFetch.mockResolvedValue(new Response('Unauthorized', { status: 401 }));
225-
mockExtractResourceMetadataUrl.mockReturnValue(undefined);
233+
mockExtractWWWAuthenticateParams.mockReturnValue({});
226234
mockAuth.mockRejectedValue(new Error('Network error'));
227235

228236
const enhancedFetch = withOAuth(mockProvider, 'https://api.example.com')(mockFetch);
@@ -239,7 +247,7 @@ describe('withOAuth', () => {
239247

240248
// Always return 401
241249
mockFetch.mockResolvedValue(new Response('Unauthorized', { status: 401 }));
242-
mockExtractResourceMetadataUrl.mockReturnValue(undefined);
250+
mockExtractWWWAuthenticateParams.mockReturnValue({});
243251
mockAuth.mockResolvedValue('AUTHORIZED');
244252

245253
const enhancedFetch = withOAuth(mockProvider, 'https://api.example.com')(mockFetch);
@@ -345,7 +353,7 @@ describe('withOAuth', () => {
345353

346354
mockFetch.mockResolvedValueOnce(unauthorizedResponse).mockResolvedValueOnce(successResponse);
347355

348-
mockExtractResourceMetadataUrl.mockReturnValue(undefined);
356+
mockExtractWWWAuthenticateParams.mockReturnValue({});
349357
mockAuth.mockResolvedValue('AUTHORIZED');
350358

351359
const enhancedFetch = withOAuth(mockProvider)(mockFetch);
@@ -876,7 +884,10 @@ describe('Integration Tests', () => {
876884

877885
mockFetch.mockResolvedValueOnce(unauthorizedResponse).mockResolvedValueOnce(successResponse);
878886

879-
mockExtractResourceMetadataUrl.mockReturnValue(new URL('https://auth.example.com/.well-known/oauth-protected-resource'));
887+
mockExtractWWWAuthenticateParams.mockReturnValue({
888+
resourceMetadataUrl: new URL('https://auth.example.com/.well-known/oauth-protected-resource'),
889+
scope: 'read'
890+
});
880891
mockAuth.mockResolvedValue('AUTHORIZED');
881892

882893
// Use custom logger to avoid console output
@@ -896,6 +907,7 @@ describe('Integration Tests', () => {
896907
expect(mockAuth).toHaveBeenCalledWith(mockProvider, {
897908
serverUrl: 'https://mcp-server.example.com',
898909
resourceMetadataUrl: new URL('https://auth.example.com/.well-known/oauth-protected-resource'),
910+
scope: 'read',
899911
fetchFn: mockFetch
900912
});
901913
});

0 commit comments

Comments
 (0)