-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrumentation-client.test.ts
More file actions
45 lines (40 loc) · 1.79 KB
/
Copy pathinstrumentation-client.test.ts
File metadata and controls
45 lines (40 loc) · 1.79 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
import { describe, expect, test } from "bun:test";
import { readFile } from "node:fs/promises";
import { approvedPostHogEndpoint } from "./lib/posthog-endpoint";
test("browser instrumentation normalizes every event before production capture", async () => {
const source = await readFile(new URL("./instrumentation-client.ts", import.meta.url), "utf8");
expect(source).toContain("before_send(event)");
expect(source).toContain("normalizedPageAnalyticsProperties(");
expect(source).toContain('event.event === "$$client_ingestion_warning"');
expect(source).toContain("event.properties?.$current_url");
expect(source).toContain("approvedPostHogEndpoint(");
expect(source).toContain('capture_pageview: "history_change"');
expect(source).toContain("autocapture: false");
expect(source).toContain('cookieless_mode: "always"');
expect(source).toContain('persistence: "memory"');
expect(source).toContain('person_profiles: "never"');
expect(source).toContain("process.env.NODE_ENV === \"production\"");
});
describe("PostHog endpoint approval", () => {
test("accepts only the exact US and EU regional origins", () => {
expect(approvedPostHogEndpoint(undefined)).toEqual({
apiHost: "https://us.i.posthog.com",
uiHost: "https://us.posthog.com",
});
expect(approvedPostHogEndpoint("https://eu.i.posthog.com/")).toEqual({
apiHost: "https://eu.i.posthog.com",
uiHost: "https://eu.posthog.com",
});
});
test("rejects spoofed, credentialed, and path-bearing destinations", () => {
for (const value of [
"https://eu.i.posthog.com.evil.example",
"https://user:password@us.i.posthog.com",
"https://us.i.posthog.com/private",
"http://us.i.posthog.com",
"not a URL",
]) {
expect(approvedPostHogEndpoint(value)).toBeNull();
}
});
});