-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
) | ||
); | ||
} |