mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-22 20:53:52 +09:00
fix(dashboard): keep caller headers of any shape in the k8s client
The client spread init.headers as a plain object, so a Headers instance or a list of pairs passed by a caller was silently dropped and the request went out without them. Headers are now built with the Headers constructor, which accepts every shape fetch does. Assisted-by: LLM Signed-off-by: ZverGuy <maximbel2003@gmail.com>
This commit is contained in:
1 parent
9498c9bff5
commit
76441871b8
3 files changed
+55
-10
No files matched your search
+31
@@ -0,0 +1,31 @@
|
||||
import { describe, it, expect, vi, afterEach } from "vitest"
|
||||
import { K8sClient } from "@cozystack/k8s-client"
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
describe("K8sClient request headers", () => {
|
||||
it("lets a method's own content type replace the JSON default", async () => {
|
||||
const fetchMock = vi.fn(async () => new Response("{}"))
|
||||
vi.stubGlobal("fetch", fetchMock)
|
||||
const client = new K8sClient({ getToken: async () => "t0k" })
|
||||
|
||||
await client.patch("apps", "v1", "deployments", "web", { spec: {} }, "default", "merge")
|
||||
|
||||
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
||||
const headers = new Headers(init.headers)
|
||||
expect(headers.get("Content-Type")).toBe("application/merge-patch+json")
|
||||
expect(headers.get("Authorization")).toBe("Bearer t0k")
|
||||
})
|
||||
|
||||
it("sends JSON by default", async () => {
|
||||
const fetchMock = vi.fn(async () => new Response("{}"))
|
||||
vi.stubGlobal("fetch", fetchMock)
|
||||
|
||||
await new K8sClient().getApiGroups()
|
||||
|
||||
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
||||
expect(new Headers(init.headers).get("Content-Type")).toBe("application/json")
|
||||
})
|
||||
})
|
||||
+19
-1
@@ -24,7 +24,25 @@ describe("K8sClient.raw", () => {
|
||||
|
||||
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
||||
expect(url).toBe("/base/apis/x")
|
||||
expect(init.headers).toEqual({ Accept: "application/json", Authorization: "Bearer t0k" })
|
||||
const headers = new Headers(init.headers)
|
||||
expect(headers.get("Accept")).toBe("application/json")
|
||||
expect(headers.get("Authorization")).toBe("Bearer t0k")
|
||||
})
|
||||
|
||||
it.each([
|
||||
["a Headers instance", new Headers({ Accept: "application/x-ndjson" })],
|
||||
["a tuple list", [["Accept", "application/x-ndjson"]] as [string, string][]],
|
||||
])("keeps caller headers passed as %s", async (_, given) => {
|
||||
const fetchMock = vi.fn(async () => new Response("{}"))
|
||||
vi.stubGlobal("fetch", fetchMock)
|
||||
const client = new K8sClient({ getToken: async () => "t0k" })
|
||||
|
||||
await client.raw("/x", { headers: given })
|
||||
|
||||
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
||||
const headers = new Headers(init.headers)
|
||||
expect(headers.get("Accept")).toBe("application/x-ndjson")
|
||||
expect(headers.get("Authorization")).toBe("Bearer t0k")
|
||||
})
|
||||
|
||||
it("routes a 401 through the client's unauthorized handler", async () => {
|
||||
|
||||
@@ -50,14 +50,12 @@ export class K8sClient {
|
||||
}
|
||||
|
||||
private async request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/json",
|
||||
...(init?.headers as Record<string, string>),
|
||||
}
|
||||
const headers = new Headers(init?.headers)
|
||||
if (!headers.has("Content-Type")) headers.set("Content-Type", "application/json")
|
||||
|
||||
if (this.getToken) {
|
||||
const token = await this.getToken()
|
||||
headers["Authorization"] = `Bearer ${token}`
|
||||
headers.set("Authorization", `Bearer ${token}`)
|
||||
}
|
||||
|
||||
let res: Response
|
||||
@@ -247,11 +245,9 @@ export class K8sClient {
|
||||
* Kubernetes JSON, such as the observability proxy's streamed NDJSON.
|
||||
*/
|
||||
async raw(path: string, init?: RequestInit): Promise<Response> {
|
||||
const headers: Record<string, string> = {
|
||||
...(init?.headers as Record<string, string>),
|
||||
}
|
||||
const headers = new Headers(init?.headers)
|
||||
if (this.getToken) {
|
||||
headers["Authorization"] = `Bearer ${await this.getToken()}`
|
||||
headers.set("Authorization", `Bearer ${await this.getToken()}`)
|
||||
}
|
||||
const res = await fetch(`${this.baseUrl}${path}`, { ...init, headers })
|
||||
if (res.status === 401) this.handleUnauthorized()
|
||||
|
||||
Reference in new issue
Block a user