-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix saving of OpenAI compatible headers #3415
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
This file contains hidden or 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
122 changes: 122 additions & 0 deletions
122
webview-ui/src/components/settings/utils/__tests__/headers.test.ts
This file contains hidden or 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,122 @@ | ||
| import { convertHeadersToObject } from "../headers" | ||
|
|
||
| describe("convertHeadersToObject", () => { | ||
| it("should convert headers array to object", () => { | ||
| const headers: [string, string][] = [ | ||
| ["Content-Type", "application/json"], | ||
| ["Authorization", "Bearer token123"], | ||
| ] | ||
|
|
||
| const result = convertHeadersToObject(headers) | ||
|
|
||
| expect(result).toEqual({ | ||
| "Content-Type": "application/json", | ||
| Authorization: "Bearer token123", | ||
| }) | ||
| }) | ||
|
|
||
| it("should trim whitespace from keys and values", () => { | ||
| const headers: [string, string][] = [ | ||
| [" Content-Type ", " application/json "], | ||
| [" Authorization ", " Bearer token123 "], | ||
| ] | ||
|
|
||
| const result = convertHeadersToObject(headers) | ||
|
|
||
| expect(result).toEqual({ | ||
| "Content-Type": "application/json", | ||
| Authorization: "Bearer token123", | ||
|
||
| }) | ||
| }) | ||
|
|
||
| it("should handle empty headers array", () => { | ||
| const headers: [string, string][] = [] | ||
|
|
||
| const result = convertHeadersToObject(headers) | ||
|
|
||
| expect(result).toEqual({}) | ||
| }) | ||
|
|
||
| it("should skip headers with empty keys", () => { | ||
| const headers: [string, string][] = [ | ||
| ["Content-Type", "application/json"], | ||
| ["", "This value should be skipped"], | ||
| [" ", "This value should also be skipped"], | ||
| ["Authorization", "Bearer token123"], | ||
| ] | ||
|
|
||
| const result = convertHeadersToObject(headers) | ||
|
|
||
| expect(result).toEqual({ | ||
| "Content-Type": "application/json", | ||
| Authorization: "Bearer token123", | ||
|
||
| }) | ||
|
|
||
| // Specifically verify empty keys are not present | ||
| expect(result[""]).toBeUndefined() | ||
| expect(result[" "]).toBeUndefined() | ||
| }) | ||
|
|
||
| it("should use last occurrence when handling duplicate keys", () => { | ||
| const headers: [string, string][] = [ | ||
| ["Content-Type", "application/json"], | ||
| ["Authorization", "Bearer token123"], | ||
| ["Content-Type", "text/plain"], // Duplicate key - should override previous value | ||
| ["Content-Type", "application/xml"], // Another duplicate - should override again | ||
| ] | ||
|
|
||
| const result = convertHeadersToObject(headers) | ||
|
|
||
| // Verify the last value for "Content-Type" is used | ||
| expect(result["Content-Type"]).toBe("application/xml") | ||
| expect(result).toEqual({ | ||
| "Content-Type": "application/xml", | ||
| Authorization: "Bearer token123", | ||
|
||
| }) | ||
| }) | ||
|
|
||
| it("should preserve case sensitivity while trimming keys", () => { | ||
| const headers: [string, string][] = [ | ||
| [" Content-Type", "application/json"], | ||
| ["content-type ", "text/plain"], // Different casing (lowercase) with spacing | ||
| ] | ||
|
|
||
| const result = convertHeadersToObject(headers) | ||
|
|
||
| // Keys should be trimmed but case sensitivity preserved | ||
| // JavaScript object keys are case-sensitive | ||
| expect(Object.keys(result)).toHaveLength(2) | ||
| expect(result["Content-Type"]).toBe("application/json") | ||
| expect(result["content-type"]).toBe("text/plain") | ||
| }) | ||
|
|
||
| it("should handle empty values", () => { | ||
| const headers: [string, string][] = [ | ||
| ["Empty-Value", ""], | ||
| ["Whitespace-Value", " "], | ||
| ] | ||
|
|
||
| const result = convertHeadersToObject(headers) | ||
|
|
||
| // Empty values should be included but trimmed | ||
| expect(result["Empty-Value"]).toBe("") | ||
| expect(result["Whitespace-Value"]).toBe("") | ||
| }) | ||
|
|
||
| it("should handle complex duplicate key scenarios with mixed casing and spacing", () => { | ||
| const headers: [string, string][] = [ | ||
| ["content-type", "application/json"], // Original entry | ||
| [" Content-Type ", "text/html"], // Different case with spacing | ||
| ["content-type", "application/xml"], // Same case as first, should override it | ||
| ["Content-Type", "text/plain"], // Same case as second, should override it | ||
| ] | ||
|
|
||
| const result = convertHeadersToObject(headers) | ||
|
|
||
| // JavaScript object keys are case-sensitive | ||
| // We should have two keys with different cases, each with the last value | ||
| expect(Object.keys(result).sort()).toEqual(["Content-Type", "content-type"].sort()) | ||
| expect(result["content-type"]).toBe("application/xml") | ||
| expect(result["Content-Type"]).toBe("text/plain") | ||
| }) | ||
| }) | ||
This file contains hidden or 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,25 @@ | ||
| /** | ||
| * Converts an array of header key-value pairs to a Record object. | ||
| * | ||
| * @param headers Array of [key, value] tuples representing HTTP headers | ||
| * @returns Record with trimmed keys and values | ||
| */ | ||
| export const convertHeadersToObject = (headers: [string, string][]): Record<string, string> => { | ||
mrubens marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const result: Record<string, string> = {} | ||
|
|
||
| // Process each header tuple. | ||
| for (const [key, value] of headers) { | ||
| const trimmedKey = key.trim() | ||
|
|
||
| // Skip empty keys. | ||
| if (!trimmedKey) { | ||
| continue | ||
| } | ||
|
|
||
| // For duplicates, the last one in the array wins. | ||
| // This matches how HTTP headers work in general. | ||
| result[trimmedKey] = value.trim() | ||
| } | ||
|
|
||
| return result | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.