forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ai.local.test.ts
69 lines (62 loc) · 1.71 KB
/
ai.local.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { Request } from "miniflare";
import { HttpResponse } from "msw";
import { AIFetcher } from "../ai/fetcher";
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 header", async () => {
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({
xForwarded: headers.get("X-Forwarded"),
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({
xForwarded: 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",
headers: {
"x-example": "test",
},
})
);
expect(await resp.json()).toEqual({
resource: "/accounts/123/ai/run/proxy",
});
});
});
});
});