-
Notifications
You must be signed in to change notification settings - Fork 736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update local AI fetcher to forward method and pathname to upstream #7315
Merged
petebacondarwin
merged 7 commits into
cloudflare:main
from
G4brym:update-local-ai-fetcher-to-forward-request-and-method
Nov 22, 2024
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6346622
Update local AI fetcher to forward method and pathname to upstream
G4brym 77d5dfe
Merge branch 'main' into update-local-ai-fetcher-to-forward-request-a…
G4brym 7e38dd1
Add unit test to cover changes
G4brym 43743f4
Merge branch 'main' into update-local-ai-fetcher-to-forward-request-a…
G4brym 8d93ebc
Lint files
G4brym fb0b5a4
Rename x-forward-for to x-forward header name
G4brym 931acf7
Merge branch 'main' into update-local-ai-fetcher-to-forward-request-a…
G4brym File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-host 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({ | ||
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", | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to be clear I am not sure that
X-Forwarded-Host
is the correct thing here. It is what I was thinking of but I think is only for hosts not full URLs.Do you have a requirement for this header on the server?
What is the header it is looking for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm writing the server part as well, so we are free to decide what we like best 😄
the only requirement i have is for this local fetcher to send the full path and query string to the server, so any header name does the job