From 802e8534074d2604552753806d58cfab78559e06 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Mon, 2 Feb 2026 21:54:32 -0800 Subject: [PATCH] PoC api-helpers test --- src/utils/proxy/api-helpers.test.js | 61 +++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/utils/proxy/api-helpers.test.js diff --git a/src/utils/proxy/api-helpers.test.js b/src/utils/proxy/api-helpers.test.js new file mode 100644 index 000000000..02185a87d --- /dev/null +++ b/src/utils/proxy/api-helpers.test.js @@ -0,0 +1,61 @@ +import { describe, expect, it } from "vitest"; + +import { asJson, formatApiCall, formatProxyUrl, getURLSearchParams, sanitizeErrorURL } from "./api-helpers"; + +describe("utils/proxy/api-helpers", () => { + it("formatApiCall replaces placeholders and trims trailing slashes for {url}", () => { + expect(formatApiCall("{url}/{endpoint}", { url: "http://localhost///", endpoint: "api" })).toBe( + "http://localhost/api", + ); + }); + + it("formatApiCall replaces repeated placeholders", () => { + expect(formatApiCall("{a}-{a}-{missing}", { a: "x" })).toBe("x-x-"); + }); + + it("getURLSearchParams includes group/service/index and optionally endpoint", () => { + const widget = { service_group: "g", service_name: "s", index: "0" }; + + const withEndpoint = getURLSearchParams(widget, "stats"); + expect(withEndpoint.get("group")).toBe("g"); + expect(withEndpoint.get("service")).toBe("s"); + expect(withEndpoint.get("index")).toBe("0"); + expect(withEndpoint.get("endpoint")).toBe("stats"); + + const withoutEndpoint = getURLSearchParams(widget); + expect(withoutEndpoint.get("endpoint")).toBeNull(); + }); + + it("formatProxyUrl builds expected proxy URL and encodes query params", () => { + const widget = { service_group: "g", service_name: "s", index: "2" }; + const url = formatProxyUrl(widget, "health", { a: 1, b: "x" }); + + expect(url.startsWith("/api/services/proxy?")).toBe(true); + + const qs = url.split("?")[1]; + const params = new URLSearchParams(qs); + expect(params.get("group")).toBe("g"); + expect(params.get("service")).toBe("s"); + expect(params.get("index")).toBe("2"); + expect(params.get("endpoint")).toBe("health"); + + expect(JSON.parse(params.get("query"))).toEqual({ a: 1, b: "x" }); + }); + + it("asJson parses JSON buffers and returns non-JSON values unchanged", () => { + expect(asJson(Buffer.from(JSON.stringify({ ok: true })))).toEqual({ ok: true }); + expect(asJson(Buffer.from(""))).toEqual(Buffer.from("")); + expect(asJson(null)).toBeNull(); + }); + + it("sanitizeErrorURL redacts sensitive query params and hash fragments", () => { + const input = "https://example.com/path?apikey=123&token=abc#access_token=xyz&other=1"; + const output = sanitizeErrorURL(input); + + const url = new URL(output); + expect(url.searchParams.get("apikey")).toBe("***"); + expect(url.searchParams.get("token")).toBe("***"); + expect(url.hash).toContain("access_token=***"); + expect(url.hash).toContain("other=1"); + }); +});