Skip to content

Commit

Permalink
Lint files
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym committed Nov 21, 2024
1 parent 43743f4 commit 8d93ebc
Showing 1 changed file with 48 additions and 20 deletions.
68 changes: 48 additions & 20 deletions packages/wrangler/src/__tests__/ai.local.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,69 @@
import { Request } from "miniflare";
import { http, HttpResponse } from "msw";
import { HttpResponse } from "msw";
import { AIFetcher } from "../ai/fetcher";
import { msw } from "./helpers/msw";
import * as internal from "../cfetch/internal";
import * as user from "../user";
import type { RequestInit } from "undici";

describe("ai", () => {
describe("fetcher", () => {
afterEach(() => {
vi.restoreAllMocks();
});

describe("local", () => {
it("should send x-forwarded-host header", async () => {
mockAIProxyRequest();
vi.spyOn(user, "getAccountId").mockImplementation(async () => "123");
vi.spyOn(internal, "performApiFetch").mockImplementation(
async (resource: string, init: RequestInit = {}) => {
const headers = new Headers(init.headers);
return HttpResponse.json({
xForwardedFor: headers.get("X-Forwarded-Host"),
method: init.method,
});
}
);

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",
});
});

it("account id should be set", async () => {
vi.spyOn(user, "getAccountId").mockImplementation(async () => "123");
vi.spyOn(internal, "performApiFetch").mockImplementation(
async (resource: string) => {
return HttpResponse.json({
resource: resource,
});
}
);

const url = "http://internal.ai/ai/test/path?version=123";
const resp = await AIFetcher(
new Request(url, {
method: 'PATCH',
method: "PATCH",
headers: {
"x-example": "test",
},
})
);

expect(await resp.json()).toEqual({ xForwardedFor: url, method: 'PATCH' });
expect(await resp.json()).toEqual({
resource: "/accounts/123/ai/run/proxy",
});
});
});
});
});

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 }
)
);
}

0 comments on commit 8d93ebc

Please sign in to comment.