Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import { Hono } from "hono";
|
||||
import { createDefaultMetrics, createMetricsRegistry, type DefaultMetrics, type MetricsRegistry } from "@toolctrl/metrics";
|
||||
import { handleApiMetricsRequest } from "./metrics.js";
|
||||
|
||||
const TOKEN = "x".repeat(64);
|
||||
|
||||
let registry: MetricsRegistry;
|
||||
let metrics: DefaultMetrics;
|
||||
|
||||
beforeEach(() => {
|
||||
registry = createMetricsRegistry({ serviceName: "api-test", collectDefaultProcessMetrics: false });
|
||||
metrics = createDefaultMetrics(registry);
|
||||
metrics.httpRequestsTotal.labels({ method: "GET", route: "/status", status: "200" }).inc();
|
||||
});
|
||||
|
||||
function app(enabled: boolean, token: string | undefined): Hono {
|
||||
const a = new Hono();
|
||||
a.get("/metrics", (c) => handleApiMetricsRequest(c, { enabled, configuredToken: token, registry }));
|
||||
return a;
|
||||
}
|
||||
|
||||
describe("handleApiMetricsRequest", () => {
|
||||
it("returns 404 when metrics are disabled", async () => {
|
||||
const res = await app(false, TOKEN).request("/metrics");
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
|
||||
it("returns 500 when enabled but no token configured", async () => {
|
||||
const res = await app(true, undefined).request("/metrics");
|
||||
expect(res.status).toBe(500);
|
||||
});
|
||||
|
||||
it("returns 401 without bearer header", async () => {
|
||||
const res = await app(true, TOKEN).request("/metrics");
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
it("returns 401 with wrong token", async () => {
|
||||
const res = await app(true, TOKEN).request("/metrics", {
|
||||
headers: { authorization: `Bearer ${"y".repeat(TOKEN.length)}` },
|
||||
});
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
it("returns 200 + prom text with valid token", async () => {
|
||||
const res = await app(true, TOKEN).request("/metrics", {
|
||||
headers: { authorization: `Bearer ${TOKEN}` },
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers.get("content-type")).toContain("text/plain");
|
||||
expect(res.headers.get("cache-control")).toBe("no-store");
|
||||
const body = await res.text();
|
||||
expect(body).toContain("http_requests_total");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user