fix: enhance OAuth token handling in useConnection hook to prevent infinite auth loops

This commit is contained in:
Kent C. Dodds 2025-09-25 15:29:26 -06:00
commit 17ace55bbf

View file

@ -913,6 +913,35 @@ describe("useConnection", () => {
expect(headers).toHaveProperty("Authorization", "Bearer mock-token");
});
test("replaces empty Bearer token placeholder with OAuth token", async () => {
// This test prevents regression of the bug where default "Bearer " header
// prevented OAuth token injection, causing infinite auth loops
const customHeaders: CustomHeaders = [
{
name: "Authorization",
value: "Bearer ", // Empty Bearer token placeholder
enabled: true,
},
];
const propsWithEmptyBearer = {
...defaultProps,
customHeaders,
};
const { result } = renderHook(() => useConnection(propsWithEmptyBearer));
await act(async () => {
await result.current.connect();
});
const headers = mockSSETransport.options?.requestInit?.headers;
// Should replace the empty "Bearer " with actual OAuth token
expect(headers).toHaveProperty("Authorization", "Bearer mock-token");
// Should not have the x-custom-auth-headers since Authorization is standard
expect(headers).not.toHaveProperty("x-custom-auth-headers");
});
test("prioritizes custom headers over legacy auth", async () => {
const customHeaders: CustomHeaders = [
{ name: "Authorization", value: "Bearer custom-token", enabled: true },