Zod parsing for registration response

This commit is contained in:
Allen Zhou 2025-02-24 20:17:08 -08:00
commit aa5690b182
2 changed files with 16 additions and 22 deletions

View file

@ -64,6 +64,16 @@ describe("Proxy OAuth Server Provider", () => {
});
});
// Add helper function for failed responses
const mockFailedResponse = () => {
(global.fetch as jest.Mock).mockImplementation(() =>
Promise.resolve({
ok: false,
status: 400,
})
);
};
afterEach(() => {
global.fetch = originalFetch;
jest.clearAllMocks();
@ -178,13 +188,7 @@ describe("Proxy OAuth Server Provider", () => {
});
it("handles token exchange failure", async () => {
(global.fetch as jest.Mock).mockImplementation(() =>
Promise.resolve({
ok: false,
status: 400,
})
);
mockFailedResponse();
await expect(
provider.exchangeAuthorizationCode(validClient, "invalid-code")
).rejects.toThrow(ServerError);
@ -221,13 +225,7 @@ describe("Proxy OAuth Server Provider", () => {
});
it("handles registration failure", async () => {
(global.fetch as jest.Mock).mockImplementation(() =>
Promise.resolve({
ok: false,
status: 400,
})
);
mockFailedResponse();
const newClient: OAuthClientInformationFull = {
client_id: "new-client",
redirect_uris: ["https://new-client.com/callback"],
@ -265,13 +263,7 @@ describe("Proxy OAuth Server Provider", () => {
});
it("handles revocation failure", async () => {
(global.fetch as jest.Mock).mockImplementation(() =>
Promise.resolve({
ok: false,
status: 400,
})
);
mockFailedResponse();
await expect(
provider.revokeToken!(validClient, {
token: "invalid-token",

View file

@ -2,6 +2,7 @@ import { Response } from "express";
import { OAuthRegisteredClientsStore } from "./clients.js";
import {
OAuthClientInformationFull,
OAuthClientInformationFullSchema,
OAuthTokenRevocationRequest,
OAuthTokens,
OAuthTokensSchema,
@ -106,7 +107,8 @@ export class ProxyOAuthServerProvider implements OAuthServerProvider {
throw new ServerError(`Client registration failed: ${response.status}`);
}
return response.json();
const data = await response.json();
return OAuthClientInformationFullSchema.parse(data);
}
})
}