diff --git a/packages/wrangler/src/__tests__/ai.local.test.ts b/packages/wrangler/src/__tests__/ai.local.test.ts new file mode 100644 index 000000000000..7b11bf8eb8bb --- /dev/null +++ b/packages/wrangler/src/__tests__/ai.local.test.ts @@ -0,0 +1,41 @@ +import { Request } from "miniflare"; +import { http, HttpResponse } from "msw"; +import { AIFetcher } from "../ai/fetcher"; +import { msw } from "./helpers/msw"; + +describe("ai", () => { + describe("fetcher", () => { + describe("local", () => { + it("should send x-forwarded-host header", async () => { + mockAIProxyRequest(); + + const url = "http://internal.ai/ai/test/path?version=123"; + const resp = await AIFetcher( + new Request(url, { + method: 'PATCH', + headers: { + "x-example": "test", + }, + }) + ); + + expect(await resp.json()).toEqual({ xForwardedFor: url, method: 'PATCH' }); + }); + }); + }); +}); + +function mockAIProxyRequest() { + msw.use( + http.get( + "*/accounts/:accountId/ai/run/proxy", + (c) => { + return HttpResponse.json({ + xForwardedFor: c.request.headers.get("X-Forwarded-Host"), + method: c.request.method + }); + }, + { once: true } + ) + ); +} diff --git a/packages/wrangler/src/ai/fetcher.ts b/packages/wrangler/src/ai/fetcher.ts index 2fe8685c5636..9ef41fecc993 100644 --- a/packages/wrangler/src/ai/fetcher.ts +++ b/packages/wrangler/src/ai/fetcher.ts @@ -19,7 +19,7 @@ export async function AIFetcher(request: Request): Promise { const reqHeaders = new Headers(request.headers); reqHeaders.delete("Host"); reqHeaders.delete("Content-Length"); - reqHeaders.set("x-url", request.url); + reqHeaders.set("X-Forwarded-Host", request.url); const res = await performApiFetch(`/accounts/${accountId}/ai/run/proxy`, { method: request.method,