Skip to content

Commit

Permalink
Add unit test to cover changes
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym committed Nov 21, 2024
1 parent 77d5dfe commit 7e38dd1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions packages/wrangler/src/__tests__/ai.local.test.ts
Original file line number Diff line number Diff line change
@@ -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 }
)
);
}
2 changes: 1 addition & 1 deletion packages/wrangler/src/ai/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function AIFetcher(request: Request): Promise<Response> {
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,
Expand Down

0 comments on commit 7e38dd1

Please sign in to comment.