diff --git a/src/wrapper/mppx.ts b/src/wrapper/mppx.ts index b1c92a5..71bba34 100644 --- a/src/wrapper/mppx.ts +++ b/src/wrapper/mppx.ts @@ -2,6 +2,7 @@ import { probe402, wsToHttp } from "./util.js"; export interface MppxClient { fetch: typeof globalThis.fetch; + rawFetch: typeof globalThis.fetch; transport: { setCredential(request: Request, credential: string): RequestInit; }; @@ -9,7 +10,7 @@ export interface MppxClient { } export async function getPaymentCredentials(wsUrl: string, mppx: MppxClient): Promise> { - const response = await probe402(wsUrl); + const response = await probe402(wsUrl, mppx.rawFetch); const credential = await mppx.createCredential(response); const signed = mppx.transport.setCredential(new Request(wsToHttp(wsUrl)), credential); diff --git a/src/wrapper/util.ts b/src/wrapper/util.ts index 39609ee..0066c4c 100644 --- a/src/wrapper/util.ts +++ b/src/wrapper/util.ts @@ -2,10 +2,10 @@ export function wsToHttp(wsUrl: string): string { return wsUrl.replace(/^wss:\/\//, "https://").replace(/^ws:\/\//, "http://"); } -export async function probe402(wsUrl: string): Promise { +export async function probe402(wsUrl: string, fetchFn: typeof fetch = fetch): Promise { const httpUrl = wsToHttp(wsUrl); - const response = await fetch(httpUrl); + const response = await fetchFn(httpUrl); if (response.status !== 402) { throw new Error(`Expected 402 from ${httpUrl} but got ${response.status}`); } diff --git a/tests/unit/wrapper/Client.test.ts b/tests/unit/wrapper/Client.test.ts index d0baa91..f9c2fdc 100644 --- a/tests/unit/wrapper/Client.test.ts +++ b/tests/unit/wrapper/Client.test.ts @@ -106,6 +106,7 @@ describe("AgentMailClient environment selection", () => { const mockMppFetch = vi.fn().mockResolvedValue(new Response()); const mockMppClient = { fetch: mockMppFetch, + rawFetch: vi.fn().mockResolvedValue(new Response()), transport: { setCredential: vi.fn() }, createCredential: vi.fn(), }; diff --git a/tests/unit/wrapper/WebsocketsClient.test.ts b/tests/unit/wrapper/WebsocketsClient.test.ts index 3ef0879..52ea752 100644 --- a/tests/unit/wrapper/WebsocketsClient.test.ts +++ b/tests/unit/wrapper/WebsocketsClient.test.ts @@ -99,6 +99,7 @@ describe("WebsocketsClient wrapper", () => { describe("with mppx", () => { const mockMppClient = { fetch: vi.fn(), + rawFetch: vi.fn(), transport: { setCredential: vi.fn() }, createCredential: vi.fn(), };