Skip to content

Commit 0806e81

Browse files
committed
fix tests
1 parent 037fb1d commit 0806e81

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

src/client/auth.test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,46 +37,47 @@ 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) => name === "WWW-Authenticate" ? `Bearer realm="mcp", resource_metadata="${resourceUrl}", scope="read"` : null),
43+
get: jest.fn((name) => name === "WWW-Authenticate" ? `Bearer realm="mcp", scope="${scope}"` : null),
4444
}
4545
} as unknown as Response
4646

47-
expect(extractWWWAuthenticateParams(mockResponse)).toEqual({ resourceMetadataUrl: new URL(resourceUrl), scope: "read" });
47+
expect(extractWWWAuthenticateParams(mockResponse)).toEqual({ scope: "read" });
4848
});
4949

5050
it("returns empty object if not bearer", async () => {
5151
const resourceUrl = "https://resource.example.com/.well-known/oauth-protected-resource"
52+
const scope = "read";
5253
const mockResponse = {
5354
headers: {
54-
get: jest.fn((name) => name === "WWW-Authenticate" ? `Basic realm="mcp", resource_metadata="${resourceUrl}"` : null),
55+
get: jest.fn((name) => name === "WWW-Authenticate" ? `Basic realm="mcp", resource_metadata="${resourceUrl}", scope="${scope}"` : null),
5556
}
5657
} as unknown as Response
5758

58-
expect(extractWWWAuthenticateParams(mockResponse)).toBe({});
59+
expect(extractWWWAuthenticateParams(mockResponse)).toEqual({});
5960
});
6061

6162
it("returns empty object if resource_metadata and scope not present", async () => {
6263
const mockResponse = {
6364
headers: {
64-
get: jest.fn((name) => name === "WWW-Authenticate" ? `Basic realm="mcp"` : null),
65+
get: jest.fn((name) => name === "WWW-Authenticate" ? `Bearer realm="mcp"` : null),
6566
}
6667
} as unknown as Response
6768

68-
expect(extractWWWAuthenticateParams(mockResponse)).toBe({});
69+
expect(extractWWWAuthenticateParams(mockResponse)).toEqual({});
6970
});
7071

7172
it("returns undefined resourceMetadataUrl on invalid url", async () => {
7273
const resourceUrl = "invalid-url"
7374
const mockResponse = {
7475
headers: {
75-
get: jest.fn((name) => name === "WWW-Authenticate" ? `Basic realm="mcp", resource_metadata="${resourceUrl}" scope="read"` : null),
76+
get: jest.fn((name) => name === "WWW-Authenticate" ? `Bearer realm="mcp", resource_metadata="${resourceUrl}", scope="read"` : null),
7677
}
7778
} as unknown as Response
7879

79-
expect(extractWWWAuthenticateParams(mockResponse)).toBe({ scope: "read" });
80+
expect(extractWWWAuthenticateParams(mockResponse)).toEqual({ scope: "read" });
8081
});
8182
});
8283

src/client/middleware.test.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ jest.mock("../client/auth.js", () => {
1212
return {
1313
...actual,
1414
auth: jest.fn(),
15-
extractResourceMetadataUrl: jest.fn(),
15+
extractWWWAuthenticateParams: jest.fn(),
1616
};
1717
});
1818

19-
import { auth, extractResourceMetadataUrl } from "./auth.js";
19+
import { auth, extractWWWAuthenticateParams } from "./auth.js";
2020

2121
const mockAuth = auth as jest.MockedFunction<typeof auth>;
22-
const mockExtractResourceMetadataUrl =
23-
extractResourceMetadataUrl as jest.MockedFunction<
24-
typeof extractResourceMetadataUrl
22+
const mockExtractWWWAuthenticateParams =
23+
extractWWWAuthenticateParams as jest.MockedFunction<
24+
typeof extractWWWAuthenticateParams
2525
>;
2626

2727
describe("withOAuth", () => {
@@ -142,10 +142,13 @@ describe("withOAuth", () => {
142142
.mockResolvedValueOnce(unauthorizedResponse)
143143
.mockResolvedValueOnce(successResponse);
144144

145-
const mockResourceUrl = new URL(
146-
"https://oauth.example.com/.well-known/oauth-protected-resource",
147-
);
148-
mockExtractResourceMetadataUrl.mockReturnValue(mockResourceUrl);
145+
const mockWWWAuthenticateParams = {
146+
resourceMetadataUrl: new URL(
147+
"https://oauth.example.com/.well-known/oauth-protected-resource",
148+
),
149+
scope: "read",
150+
}
151+
mockExtractWWWAuthenticateParams.mockReturnValue(mockWWWAuthenticateParams);
149152
mockAuth.mockResolvedValue("AUTHORIZED");
150153

151154
const enhancedFetch = withOAuth(
@@ -159,7 +162,8 @@ describe("withOAuth", () => {
159162
expect(mockFetch).toHaveBeenCalledTimes(2);
160163
expect(mockAuth).toHaveBeenCalledWith(mockProvider, {
161164
serverUrl: "https://api.example.com",
162-
resourceMetadataUrl: mockResourceUrl,
165+
resourceMetadataUrl: mockWWWAuthenticateParams.resourceMetadataUrl,
166+
scope: mockWWWAuthenticateParams.scope,
163167
fetchFn: mockFetch,
164168
});
165169

@@ -192,10 +196,13 @@ describe("withOAuth", () => {
192196
.mockResolvedValueOnce(unauthorizedResponse)
193197
.mockResolvedValueOnce(successResponse);
194198

195-
const mockResourceUrl = new URL(
196-
"https://oauth.example.com/.well-known/oauth-protected-resource",
197-
);
198-
mockExtractResourceMetadataUrl.mockReturnValue(mockResourceUrl);
199+
const mockWWWAuthenticateParams = {
200+
resourceMetadataUrl: new URL(
201+
"https://oauth.example.com/.well-known/oauth-protected-resource",
202+
),
203+
scope: "read",
204+
}
205+
mockExtractWWWAuthenticateParams.mockReturnValue(mockWWWAuthenticateParams);
199206
mockAuth.mockResolvedValue("AUTHORIZED");
200207

201208
// Test without baseUrl - should extract from request URL
@@ -207,7 +214,8 @@ describe("withOAuth", () => {
207214
expect(mockFetch).toHaveBeenCalledTimes(2);
208215
expect(mockAuth).toHaveBeenCalledWith(mockProvider, {
209216
serverUrl: "https://api.example.com", // Should be extracted from request URL
210-
resourceMetadataUrl: mockResourceUrl,
217+
resourceMetadataUrl: mockWWWAuthenticateParams.resourceMetadataUrl,
218+
scope: mockWWWAuthenticateParams.scope,
211219
fetchFn: mockFetch,
212220
});
213221

@@ -225,7 +233,7 @@ describe("withOAuth", () => {
225233
});
226234

227235
mockFetch.mockResolvedValue(new Response("Unauthorized", { status: 401 }));
228-
mockExtractResourceMetadataUrl.mockReturnValue(undefined);
236+
mockExtractWWWAuthenticateParams.mockReturnValue({});
229237
mockAuth.mockResolvedValue("REDIRECT");
230238

231239
// Test without baseUrl
@@ -244,7 +252,7 @@ describe("withOAuth", () => {
244252
});
245253

246254
mockFetch.mockResolvedValue(new Response("Unauthorized", { status: 401 }));
247-
mockExtractResourceMetadataUrl.mockReturnValue(undefined);
255+
mockExtractWWWAuthenticateParams.mockReturnValue({});
248256
mockAuth.mockRejectedValue(new Error("Network error"));
249257

250258
const enhancedFetch = withOAuth(
@@ -266,7 +274,7 @@ describe("withOAuth", () => {
266274

267275
// Always return 401
268276
mockFetch.mockResolvedValue(new Response("Unauthorized", { status: 401 }));
269-
mockExtractResourceMetadataUrl.mockReturnValue(undefined);
277+
mockExtractWWWAuthenticateParams.mockReturnValue({});
270278
mockAuth.mockResolvedValue("AUTHORIZED");
271279

272280
const enhancedFetch = withOAuth(
@@ -383,7 +391,7 @@ describe("withOAuth", () => {
383391
.mockResolvedValueOnce(unauthorizedResponse)
384392
.mockResolvedValueOnce(successResponse);
385393

386-
mockExtractResourceMetadataUrl.mockReturnValue(undefined);
394+
mockExtractWWWAuthenticateParams.mockReturnValue({});
387395
mockAuth.mockResolvedValue("AUTHORIZED");
388396

389397
const enhancedFetch = withOAuth(mockProvider)(mockFetch);
@@ -964,8 +972,11 @@ describe("Integration Tests", () => {
964972
.mockResolvedValueOnce(unauthorizedResponse)
965973
.mockResolvedValueOnce(successResponse);
966974

967-
mockExtractResourceMetadataUrl.mockReturnValue(
968-
new URL("https://auth.example.com/.well-known/oauth-protected-resource"),
975+
mockExtractWWWAuthenticateParams.mockReturnValue(
976+
{
977+
resourceMetadataUrl: new URL("https://auth.example.com/.well-known/oauth-protected-resource"),
978+
scope: "read",
979+
}
969980
);
970981
mockAuth.mockResolvedValue("AUTHORIZED");
971982

@@ -994,6 +1005,7 @@ describe("Integration Tests", () => {
9941005
resourceMetadataUrl: new URL(
9951006
"https://auth.example.com/.well-known/oauth-protected-resource",
9961007
),
1008+
scope: "read",
9971009
fetchFn: mockFetch,
9981010
});
9991011
});

0 commit comments

Comments
 (0)