Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ const trustedHeaderValuePattern = /^[A-Za-z0-9._:-]+$/;
const clientIdentifierPattern = /^[A-Za-z0-9.:%_,-]+$/;
const maxTrustedHeaderLength = 128;

/* v8 ignore start */
function jsonResponse(body: StandardErrorResponse | StandardSuccessResponse<unknown>, status = 200): Response {
return new Response(JSON.stringify(body), {
status,
Expand All @@ -159,6 +158,7 @@ function traceIdFromRequest(request: Request): string {
|| crypto.randomUUID();
}

/* v8 ignore start */
function safeHash(input: string): string {
let hash = 2166136261;
for (let i = 0; i < input.length; i += 1) {
Expand Down
19 changes: 19 additions & 0 deletions test/coverage-ignore-operational-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";

const source = readFileSync(new URL("../src/index.ts", import.meta.url), "utf8");
const ignoredRegions = [...source.matchAll(/\/\* v8 ignore start \*\/[\s\S]*?\/\* v8 ignore stop \*\//g)]
.map((match) => match[0]);

describe("operational helper coverage exclusions", () => {
it.each([
"jsonResponse",
"trustedTraceHeader",
"traceIdFromRequest",
])("keeps %s inside measured production coverage", (functionName) => {
expect(
ignoredRegions.some((region) => region.includes(`function ${functionName}`)),
`${functionName} must not be hidden by a broad v8 ignore region`,
).toBe(false);
});
});
31 changes: 31 additions & 0 deletions test/trace-header-coverage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, it } from "vitest";
import worker, { type Env } from "../src/index";

const env = {} as Env;

describe("trace header selection", () => {
it("uses a valid request id as the response trace identity", async () => {
const response = await worker.fetch(new Request("https://noema.example/health", {
headers: { "x-request-id": "request.trace-123" },
}), env);

expect(response.status).toBe(200);
const payload = await response.json() as { trace_id: string };
expect(payload.trace_id).toBe("request.trace-123");
expect(response.headers.get("x-trace-id")).toBe("request.trace-123");
});

it("falls back to a valid correlation id when request id contains unsafe characters", async () => {
const response = await worker.fetch(new Request("https://noema.example/health", {
headers: {
"x-request-id": "unsafe request id",
"x-correlation-id": "correlation:trace_456",
},
}), env);

expect(response.status).toBe(200);
const payload = await response.json() as { trace_id: string };
expect(payload.trace_id).toBe("correlation:trace_456");
expect(response.headers.get("x-trace-id")).toBe("correlation:trace_456");
});
});
Loading