mirror of
https://github.com/gethomepage/homepage.git
synced 2026-02-08 17:00:51 +08:00
test: cover middleware + core utils (logger, hooks, proxy)
This commit is contained in:
112
src/utils/proxy/http.test.js
Normal file
112
src/utils/proxy/http.test.js
Normal file
@@ -0,0 +1,112 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { state, cache, logger } = vi.hoisted(() => ({
|
||||
state: {
|
||||
response: {
|
||||
statusCode: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
body: Buffer.from(""),
|
||||
},
|
||||
error: null,
|
||||
},
|
||||
cache: {
|
||||
get: vi.fn(),
|
||||
put: vi.fn(),
|
||||
},
|
||||
logger: {
|
||||
debug: vi.fn(),
|
||||
error: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("follow-redirects", async () => {
|
||||
const { EventEmitter } = await import("node:events");
|
||||
const { Readable } = await import("node:stream");
|
||||
|
||||
function Agent(opts) {
|
||||
this.opts = opts;
|
||||
}
|
||||
|
||||
function makeRequest() {
|
||||
return (url, params, cb) => {
|
||||
const req = new EventEmitter();
|
||||
req.write = vi.fn();
|
||||
req.end = vi.fn(() => {
|
||||
if (state.error) {
|
||||
req.emit("error", state.error);
|
||||
return;
|
||||
}
|
||||
|
||||
const res = new Readable({
|
||||
read() {
|
||||
this.push(state.response.body);
|
||||
this.push(null);
|
||||
},
|
||||
});
|
||||
res.statusCode = state.response.statusCode;
|
||||
res.headers = state.response.headers;
|
||||
cb(res);
|
||||
});
|
||||
return req;
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
http: { request: makeRequest(), Agent },
|
||||
https: { request: makeRequest(), Agent },
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("memory-cache", () => ({
|
||||
default: cache,
|
||||
}));
|
||||
|
||||
vi.mock("utils/logger", () => ({
|
||||
default: () => logger,
|
||||
}));
|
||||
|
||||
describe("utils/proxy/http cachedRequest", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
state.error = null;
|
||||
state.response = {
|
||||
statusCode: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
body: Buffer.from(""),
|
||||
};
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
it("returns cached values without calling httpProxy", async () => {
|
||||
cache.get.mockReturnValueOnce({ ok: true });
|
||||
const httpMod = await import("./http");
|
||||
const spy = vi.spyOn(httpMod, "httpProxy");
|
||||
|
||||
const data = await httpMod.cachedRequest("http://example.com");
|
||||
|
||||
expect(data).toEqual({ ok: true });
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("parses json buffer responses and caches the result", async () => {
|
||||
cache.get.mockReturnValueOnce(null);
|
||||
state.response.body = Buffer.from('{"a":1}');
|
||||
const httpMod = await import("./http");
|
||||
|
||||
const data = await httpMod.cachedRequest("http://example.com/data", 1, "ua");
|
||||
|
||||
expect(data).toEqual({ a: 1 });
|
||||
expect(cache.put).toHaveBeenCalledWith("http://example.com/data", { a: 1 }, 1 * 1000 * 60);
|
||||
});
|
||||
|
||||
it("falls back to string when cachedRequest cannot parse json", async () => {
|
||||
cache.get.mockReturnValueOnce(null);
|
||||
state.response.body = Buffer.from("not-json");
|
||||
const httpMod = await import("./http");
|
||||
|
||||
const data = await httpMod.cachedRequest("http://example.com/data", 1, "ua");
|
||||
|
||||
expect(data).toBe("not-json");
|
||||
expect(logger.debug).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
49
src/utils/proxy/use-widget-api.test.js
Normal file
49
src/utils/proxy/use-widget-api.test.js
Normal file
@@ -0,0 +1,49 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { useSWR } = vi.hoisted(() => ({ useSWR: vi.fn() }));
|
||||
|
||||
vi.mock("swr", () => ({
|
||||
default: useSWR,
|
||||
}));
|
||||
|
||||
import useWidgetAPI from "./use-widget-api";
|
||||
|
||||
describe("utils/proxy/use-widget-api", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("formats the proxy url and passes refreshInterval when provided in options", () => {
|
||||
useSWR.mockReturnValue({ data: { ok: true }, error: undefined, mutate: "m" });
|
||||
|
||||
const widget = { service_group: "g", service_name: "s", index: 0 };
|
||||
const result = useWidgetAPI(widget, "status", { refreshInterval: 123, foo: "bar" });
|
||||
|
||||
expect(useSWR).toHaveBeenCalledWith(
|
||||
expect.stringContaining("/api/services/proxy?"),
|
||||
expect.objectContaining({ refreshInterval: 123 }),
|
||||
);
|
||||
expect(result.data).toEqual({ ok: true });
|
||||
expect(result.error).toBeUndefined();
|
||||
expect(result.mutate).toBe("m");
|
||||
});
|
||||
|
||||
it("returns data.error as the top-level error", () => {
|
||||
const dataError = { message: "nope" };
|
||||
useSWR.mockReturnValue({ data: { error: dataError }, error: undefined, mutate: vi.fn() });
|
||||
|
||||
const widget = { service_group: "g", service_name: "s", index: 0 };
|
||||
const result = useWidgetAPI(widget, "status", {});
|
||||
|
||||
expect(result.error).toBe(dataError);
|
||||
});
|
||||
|
||||
it("disables the request when endpoint is an empty string", () => {
|
||||
useSWR.mockReturnValue({ data: undefined, error: undefined, mutate: vi.fn() });
|
||||
|
||||
const widget = { service_group: "g", service_name: "s", index: 0 };
|
||||
useWidgetAPI(widget, "");
|
||||
|
||||
expect(useSWR).toHaveBeenCalledWith(null, {});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user