From 76441871b8c3ae73f40ecf29fd454a468f1e0573 Mon Sep 17 00:00:00 2001 From: ZverGuy Date: Wed, 4 Nov 2026 19:06:59 +0700 Subject: [PATCH] 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 --- .../k8s-client/client.headers.test.ts | 31 +++++++++++++++++++ .../__tests__/k8s-client/client.raw.test.ts | 20 +++++++++++- .../console/packages/k8s-client/src/client.ts | 14 +++------ 3 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 packages/system/dashboard/images/console/apps/console/src/__tests__/k8s-client/client.headers.test.ts diff --git a/packages/system/dashboard/images/console/apps/console/src/__tests__/k8s-client/client.headers.test.ts b/packages/system/dashboard/images/console/apps/console/src/__tests__/k8s-client/client.headers.test.ts new file mode 100644 index 000000000..37985fca5 --- /dev/null +++ b/packages/system/dashboard/images/console/apps/console/src/__tests__/k8s-client/client.headers.test.ts @@ -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") + }) +}) diff --git a/packages/system/dashboard/images/console/apps/console/src/__tests__/k8s-client/client.raw.test.ts b/packages/system/dashboard/images/console/apps/console/src/__tests__/k8s-client/client.raw.test.ts index ab6c056f5..0ac0d75ae 100644 --- a/packages/system/dashboard/images/console/apps/console/src/__tests__/k8s-client/client.raw.test.ts +++ b/packages/system/dashboard/images/console/apps/console/src/__tests__/k8s-client/client.raw.test.ts @@ -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 () => { diff --git a/packages/system/dashboard/images/console/packages/k8s-client/src/client.ts b/packages/system/dashboard/images/console/packages/k8s-client/src/client.ts index 04c528760..64af1131d 100644 --- a/packages/system/dashboard/images/console/packages/k8s-client/src/client.ts +++ b/packages/system/dashboard/images/console/packages/k8s-client/src/client.ts @@ -50,14 +50,12 @@ export class K8sClient { } private async request(path: string, init?: RequestInit): Promise { - const headers: Record = { - "Content-Type": "application/json", - ...(init?.headers as Record), - } + 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 { - const headers: Record = { - ...(init?.headers as Record), - } + 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()