-
Notifications
You must be signed in to change notification settings - Fork 734
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update local AI fetcher to forward method and pathname to upstream (#…
…7315) * Update local AI fetcher to forward method and pathname to upstream * Add unit test to cover changes * Lint files * Rename x-forward-for to x-forward header name
- Loading branch information
Showing
3 changed files
with
80 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wrangler": minor | ||
--- | ||
|
||
Update local AI fetcher to forward method and url to upstream |
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 |
---|---|---|
@@ -0,0 +1,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", | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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