-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathbrowser-traces.test.ts
More file actions
85 lines (75 loc) · 2.76 KB
/
Copy pathbrowser-traces.test.ts
File metadata and controls
85 lines (75 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { describe, expect, it } from "@effect/vitest";
import { browserTracesResponse } from "./browser-traces";
const makeRequest = (init?: RequestInit & { path?: string }) =>
new Request(`https://executor.sh${init?.path ?? "/v1/traces"}`, {
method: "POST",
body: "{}",
...init,
});
const baseEnv = {
AXIOM_TOKEN: "axiom-secret",
AXIOM_DATASET: "executor-cloud",
} as Env;
describe("browserTracesResponse", () => {
it("ignores non-/v1/traces requests entirely", () => {
expect(browserTracesResponse(makeRequest({ path: "/api/tools" }), baseEnv)).toBeNull();
});
it("drops batches silently when Axiom is not configured", async () => {
const response = await browserTracesResponse(
makeRequest({ headers: { cookie: "wos-session=abc" } }),
{} as Env,
);
expect(response?.status).toBe(204);
});
it("rejects anonymous posts", async () => {
const response = await browserTracesResponse(makeRequest(), baseEnv);
expect(response?.status).toBe(401);
});
it("forwards to Axiom with server-held credentials and hides the upstream body", async () => {
let seen: { url: string; auth: string | null; dataset: string | null } | undefined;
const response = await browserTracesResponse(
makeRequest({ headers: { cookie: "wos-session=abc" } }),
baseEnv,
(async (url: RequestInfo | URL, init?: RequestInit) => {
seen = {
url: String(url),
auth: new Headers(init?.headers).get("authorization"),
dataset: new Headers(init?.headers).get("x-axiom-dataset"),
};
return new Response("axiom-internals", { status: 200 });
}) as typeof fetch,
);
expect(seen?.url).toBe("https://api.axiom.co/v1/traces");
expect(seen?.auth).toBe("Bearer axiom-secret");
expect(seen?.dataset).toBe("executor-cloud");
expect(response?.status).toBe(204);
expect(await response?.text()).toBe("");
});
it("reports upstream failure as 502 without leaking detail", async () => {
const response = await browserTracesResponse(
makeRequest({ headers: { cookie: "wos-session=abc" } }),
baseEnv,
(async () => new Response("denied", { status: 403 })) as typeof fetch,
);
expect(response?.status).toBe(502);
});
it("refuses oversized batches", async () => {
const response = await browserTracesResponse(
makeRequest({
headers: {
cookie: "wos-session=abc",
"content-length": String(3_000_000),
},
}),
baseEnv,
);
expect(response?.status).toBe(413);
});
it("only accepts POST", async () => {
const response = await browserTracesResponse(
new Request("https://executor.sh/v1/traces", { method: "GET" }),
baseEnv,
);
expect(response?.status).toBe(405);
});
});