Skip to content

Commit 3444b67

Browse files
committed
revert / minimize diffs
1 parent c7f9e36 commit 3444b67

File tree

2 files changed

+85
-72
lines changed

2 files changed

+85
-72
lines changed

src/client/auth.test.ts

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,8 @@ const mockFetch = jest.fn();
1717
global.fetch = mockFetch;
1818

1919
describe("OAuth Authorization", () => {
20-
let mockProvider: OAuthClientProvider;
21-
2220
beforeEach(() => {
2321
mockFetch.mockReset();
24-
mockProvider = {
25-
get redirectUrl() { return "http://localhost:3000/callback"; },
26-
get clientMetadata() {
27-
return {
28-
redirect_uris: ["http://localhost:3000/callback"],
29-
client_name: "Test Client",
30-
};
31-
},
32-
clientInformation: jest.fn(),
33-
tokens: jest.fn(),
34-
saveTokens: jest.fn(),
35-
redirectToAuthorization: jest.fn(),
36-
saveCodeVerifier: jest.fn(),
37-
codeVerifier() { return "verifier123"; },
38-
};
3922
});
4023

4124
describe("extractResourceMetadataUrl", () => {
@@ -480,9 +463,9 @@ describe("OAuth Authorization", () => {
480463
{
481464
metadata: undefined,
482465
clientInformation: validClientInfo,
466+
redirectUrl: "http://localhost:3000/callback",
483467
resource: new URL("https://api.example.com/mcp-server"),
484-
},
485-
mockProvider
468+
}
486469
);
487470

488471
expect(authorizationUrl.toString()).toMatch(
@@ -505,9 +488,9 @@ describe("OAuth Authorization", () => {
505488
"https://auth.example.com",
506489
{
507490
clientInformation: validClientInfo,
491+
redirectUrl: "http://localhost:3000/callback",
508492
scope: "read write profile",
509-
},
510-
mockProvider
493+
}
511494
);
512495

513496
expect(authorizationUrl.searchParams.get("scope")).toBe("read write profile");
@@ -518,8 +501,8 @@ describe("OAuth Authorization", () => {
518501
"https://auth.example.com",
519502
{
520503
clientInformation: validClientInfo,
521-
},
522-
mockProvider
504+
redirectUrl: "http://localhost:3000/callback",
505+
}
523506
);
524507

525508
expect(authorizationUrl.searchParams.has("scope")).toBe(false);
@@ -530,9 +513,9 @@ describe("OAuth Authorization", () => {
530513
"https://auth.example.com",
531514
{
532515
clientInformation: validClientInfo,
516+
redirectUrl: "http://localhost:3000/callback",
533517
state: "foobar",
534-
},
535-
mockProvider
518+
}
536519
);
537520

538521
expect(authorizationUrl.searchParams.get("state")).toBe("foobar");
@@ -543,8 +526,8 @@ describe("OAuth Authorization", () => {
543526
"https://auth.example.com",
544527
{
545528
clientInformation: validClientInfo,
546-
},
547-
mockProvider
529+
redirectUrl: "http://localhost:3000/callback",
530+
}
548531
);
549532

550533
expect(authorizationUrl.searchParams.has("state")).toBe(false);
@@ -556,8 +539,8 @@ describe("OAuth Authorization", () => {
556539
{
557540
metadata: validMetadata,
558541
clientInformation: validClientInfo,
559-
},
560-
mockProvider
542+
redirectUrl: "http://localhost:3000/callback",
543+
}
561544
);
562545

563546
expect(authorizationUrl.toString()).toMatch(
@@ -575,7 +558,8 @@ describe("OAuth Authorization", () => {
575558
startAuthorization("https://auth.example.com", {
576559
metadata,
577560
clientInformation: validClientInfo,
578-
}, mockProvider)
561+
redirectUrl: "http://localhost:3000/callback",
562+
})
579563
).rejects.toThrow(/does not support response type/);
580564
});
581565

@@ -590,7 +574,8 @@ describe("OAuth Authorization", () => {
590574
startAuthorization("https://auth.example.com", {
591575
metadata,
592576
clientInformation: validClientInfo,
593-
}, mockProvider)
577+
redirectUrl: "http://localhost:3000/callback",
578+
})
594579
).rejects.toThrow(/does not support code challenge method/);
595580
});
596581
});
@@ -620,8 +605,10 @@ describe("OAuth Authorization", () => {
620605
const tokens = await exchangeAuthorization("https://auth.example.com", {
621606
clientInformation: validClientInfo,
622607
authorizationCode: "code123",
608+
codeVerifier: "verifier123",
609+
redirectUri: "http://localhost:3000/callback",
623610
resource: new URL("https://api.example.com/mcp-server"),
624-
}, mockProvider);
611+
});
625612

626613
expect(tokens).toEqual(validTokens);
627614
expect(mockFetch).toHaveBeenCalledWith(
@@ -647,12 +634,6 @@ describe("OAuth Authorization", () => {
647634
});
648635

649636
it("exchanges code for tokens with auth", async () => {
650-
mockProvider.addClientAuthentication = function(url: URL, headers: Headers, params: URLSearchParams) {
651-
headers.set("Authorization", "Basic " + btoa(validClientInfo.client_id + ":" + validClientInfo.client_secret));
652-
params.set("example_url", url.toString());
653-
params.set("example_param", "example_value");
654-
};
655-
656637
mockFetch.mockResolvedValueOnce({
657638
ok: true,
658639
status: 200,
@@ -662,7 +643,14 @@ describe("OAuth Authorization", () => {
662643
const tokens = await exchangeAuthorization("https://auth.example.com", {
663644
clientInformation: validClientInfo,
664645
authorizationCode: "code123",
665-
}, mockProvider);
646+
codeVerifier: "verifier123",
647+
redirectUri: "http://localhost:3000/callback",
648+
addClientAuthentication: (url: URL, headers: Headers, params: URLSearchParams) => {
649+
headers.set("Authorization", "Basic " + btoa(validClientInfo.client_id + ":" + validClientInfo.client_secret));
650+
params.set("example_url", url.toString());
651+
params.set("example_param", "example_value");
652+
},
653+
});
666654

667655
expect(tokens).toEqual(validTokens);
668656
expect(mockFetch).toHaveBeenCalledWith(
@@ -702,7 +690,9 @@ describe("OAuth Authorization", () => {
702690
exchangeAuthorization("https://auth.example.com", {
703691
clientInformation: validClientInfo,
704692
authorizationCode: "code123",
705-
}, mockProvider)
693+
redirectUri: "http://localhost:3000/callback",
694+
codeVerifier: "verifier123",
695+
})
706696
).rejects.toThrow();
707697
});
708698

@@ -715,8 +705,10 @@ describe("OAuth Authorization", () => {
715705
await expect(
716706
exchangeAuthorization("https://auth.example.com", {
717707
clientInformation: validClientInfo,
708+
redirectUri: "http://localhost:3000/callback",
718709
authorizationCode: "code123",
719-
}, mockProvider)
710+
codeVerifier: "verifier123",
711+
})
720712
).rejects.toThrow("Token exchange failed");
721713
});
722714
});
@@ -750,7 +742,7 @@ describe("OAuth Authorization", () => {
750742
clientInformation: validClientInfo,
751743
refreshToken: "refresh123",
752744
resource: new URL("https://api.example.com/mcp-server"),
753-
}, mockProvider);
745+
});
754746

755747
expect(tokens).toEqual(validTokensWithNewRefreshToken);
756748
expect(mockFetch).toHaveBeenCalledWith(
@@ -771,12 +763,6 @@ describe("OAuth Authorization", () => {
771763
});
772764

773765
it("exchanges refresh token for new tokens with auth", async () => {
774-
mockProvider.addClientAuthentication = function(url: URL, headers: Headers, params: URLSearchParams) {
775-
headers.set("Authorization", "Basic " + btoa(validClientInfo.client_id + ":" + validClientInfo.client_secret));
776-
params.set("example_url", url.toString());
777-
params.set("example_param", "example_value");
778-
};
779-
780766
mockFetch.mockResolvedValueOnce({
781767
ok: true,
782768
status: 200,
@@ -786,7 +772,12 @@ describe("OAuth Authorization", () => {
786772
const tokens = await refreshAuthorization("https://auth.example.com", {
787773
clientInformation: validClientInfo,
788774
refreshToken: "refresh123",
789-
}, mockProvider);
775+
addClientAuthentication: (url: URL, headers: Headers, params: URLSearchParams) => {
776+
headers.set("Authorization", "Basic " + btoa(validClientInfo.client_id + ":" + validClientInfo.client_secret));
777+
params.set("example_url", url.toString());
778+
params.set("example_param", "example_value");
779+
},
780+
});
790781

791782
expect(tokens).toEqual(validTokensWithNewRefreshToken);
792783
expect(mockFetch).toHaveBeenCalledWith(
@@ -821,7 +812,7 @@ describe("OAuth Authorization", () => {
821812
const tokens = await refreshAuthorization("https://auth.example.com", {
822813
clientInformation: validClientInfo,
823814
refreshToken,
824-
}, mockProvider);
815+
});
825816

826817
expect(tokens).toEqual({ refresh_token: refreshToken, ...validTokens });
827818
});
@@ -840,7 +831,7 @@ describe("OAuth Authorization", () => {
840831
refreshAuthorization("https://auth.example.com", {
841832
clientInformation: validClientInfo,
842833
refreshToken: "refresh123",
843-
}, mockProvider)
834+
})
844835
).rejects.toThrow();
845836
});
846837

@@ -854,7 +845,7 @@ describe("OAuth Authorization", () => {
854845
refreshAuthorization("https://auth.example.com", {
855846
clientInformation: validClientInfo,
856847
refreshToken: "refresh123",
857-
}, mockProvider)
848+
})
858849
).rejects.toThrow("Token refresh failed");
859850
});
860851
});
@@ -1599,7 +1590,9 @@ describe("OAuth Authorization", () => {
15991590
metadata: metadataWithBasicOnly,
16001591
clientInformation: validClientInfo,
16011592
authorizationCode: "code123",
1602-
}, mockProvider);
1593+
redirectUri: "http://localhost:3000/callback",
1594+
codeVerifier: "verifier123",
1595+
});
16031596

16041597
expect(tokens).toEqual(validTokens);
16051598
const request = mockFetch.mock.calls[0][1];
@@ -1625,7 +1618,9 @@ describe("OAuth Authorization", () => {
16251618
metadata: metadataWithPostOnly,
16261619
clientInformation: validClientInfo,
16271620
authorizationCode: "code123",
1628-
}, mockProvider);
1621+
redirectUri: "http://localhost:3000/callback",
1622+
codeVerifier: "verifier123",
1623+
});
16291624

16301625
expect(tokens).toEqual(validTokens);
16311626
const request = mockFetch.mock.calls[0][1];
@@ -1655,7 +1650,9 @@ describe("OAuth Authorization", () => {
16551650
metadata: metadataWithNoneOnly,
16561651
clientInformation: clientInfoWithoutSecret,
16571652
authorizationCode: "code123",
1658-
}, mockProvider);
1653+
redirectUri: "http://localhost:3000/callback",
1654+
codeVerifier: "verifier123",
1655+
});
16591656

16601657
expect(tokens).toEqual(validTokens);
16611658
const request = mockFetch.mock.calls[0][1];
@@ -1678,7 +1675,9 @@ describe("OAuth Authorization", () => {
16781675
const tokens = await exchangeAuthorization("https://auth.example.com", {
16791676
clientInformation: validClientInfo,
16801677
authorizationCode: "code123",
1681-
}, mockProvider);
1678+
redirectUri: "http://localhost:3000/callback",
1679+
codeVerifier: "verifier123",
1680+
});
16821681

16831682
expect(tokens).toEqual(validTokens);
16841683
const request = mockFetch.mock.calls[0][1];
@@ -1732,7 +1731,7 @@ describe("OAuth Authorization", () => {
17321731
metadata: metadataWithBasicOnly,
17331732
clientInformation: validClientInfo,
17341733
refreshToken: "refresh123",
1735-
}, mockProvider);
1734+
});
17361735

17371736
expect(tokens).toEqual(validTokens);
17381737
const request = mockFetch.mock.calls[0][1];
@@ -1759,7 +1758,7 @@ describe("OAuth Authorization", () => {
17591758
metadata: metadataWithPostOnly,
17601759
clientInformation: validClientInfo,
17611760
refreshToken: "refresh123",
1762-
}, mockProvider);
1761+
});
17631762

17641763
expect(tokens).toEqual(validTokens);
17651764
const request = mockFetch.mock.calls[0][1];

0 commit comments

Comments
 (0)