forked from Lilly-Protocol/lily-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcors.test.ts
More file actions
39 lines (30 loc) · 1.15 KB
/
Copy pathcors.test.ts
File metadata and controls
39 lines (30 loc) · 1.15 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
import { describe, expect, it } from "vitest";
import { corsOptions } from "../src/config/cors";
const invokeOrigin = (origin: string | undefined) =>
new Promise<{ error: Error | null; allowed: unknown }>((resolve) => {
if (typeof corsOptions.origin !== "function") {
resolve({ error: null, allowed: true });
return;
}
corsOptions.origin(origin, (error, allow) => {
resolve({ error, allowed: allow });
});
});
describe("CORS options handler", () => {
it("allows non-browser requests with no origin", async () => {
const { error, allowed } = await invokeOrigin(undefined);
expect(error).toBeNull();
expect(allowed).toBe(true);
});
it("allows configured whitelist origins", async () => {
const { error, allowed } = await invokeOrigin("http://localhost:3000");
expect(error).toBeNull();
expect(allowed).toBe(true);
});
it("rejects unauthorized origins", async () => {
const { error } = await invokeOrigin("https://malicious-site.com");
expect(error).not.toBeNull();
expect(error instanceof Error).toBe(true);
expect((error as Error).message).toBe("Origin not allowed by CORS");
});
});