forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-headers-matrix.test.ts
More file actions
76 lines (63 loc) · 3.39 KB
/
Copy pathauth-headers-matrix.test.ts
File metadata and controls
76 lines (63 loc) · 3.39 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
import { describe, it, expect, vi } from "vitest";
import { createFetchHttpClient } from "../src/http/fetch-http-client.js";
describe("auth headers matrix (issue #121)", () => {
const baseConfig = {
baseUrl: new URL("https://api.lily.test/"),
timeoutMs: 2_000,
retry: { retries: 0, retryDelayMs: 0, retryableStatusCodes: [] },
defaultHeaders: {},
userAgent: "lily-sdk/test",
};
it("sends only x-api-key when apiKey is configured", async () => {
const fetchSpy = vi.fn(() =>
Promise.resolve(new Response(JSON.stringify({ ok: true }), { status: 200, headers: { "content-type": "application/json" } }))
);
const client = createFetchHttpClient({ ...baseConfig, apiKey: "test-key", fetch: fetchSpy });
await client.request({ method: "GET", path: "/v1/test" });
expect(fetchSpy).toHaveBeenCalled();
const calls = fetchSpy.mock.calls as unknown as [unknown, Record<string, unknown>?][];
const init = calls[0]?.[1];
const headers = init?.headers as Record<string, string> | undefined;
expect(headers?.["x-api-key"]).toBe("test-key");
expect(headers?.authorization).toBeUndefined();
});
it("sends only Bearer token when authToken is configured", async () => {
const fetchSpy = vi.fn(() =>
Promise.resolve(new Response(JSON.stringify({ ok: true }), { status: 200, headers: { "content-type": "application/json" } }))
);
const client = createFetchHttpClient({ ...baseConfig, authToken: "test-token", fetch: fetchSpy });
await client.request({ method: "GET", path: "/v1/test" });
expect(fetchSpy).toHaveBeenCalled();
const calls = fetchSpy.mock.calls as unknown as [unknown, Record<string, unknown>?][];
const init = calls[0]?.[1];
const headers = init?.headers as Record<string, string> | undefined;
expect(headers?.authorization).toBe("Bearer test-token");
expect(headers?.["x-api-key"]).toBeUndefined();
});
it("sends both headers when apiKey and authToken are configured", async () => {
const fetchSpy = vi.fn(() =>
Promise.resolve(new Response(JSON.stringify({ ok: true }), { status: 200, headers: { "content-type": "application/json" } }))
);
const client = createFetchHttpClient({ ...baseConfig, apiKey: "both-key", authToken: "both-token", fetch: fetchSpy });
await client.request({ method: "GET", path: "/v1/test" });
expect(fetchSpy).toHaveBeenCalled();
const calls = fetchSpy.mock.calls as unknown as [unknown, Record<string, unknown>?][];
const init = calls[0]?.[1];
const headers = init?.headers as Record<string, string> | undefined;
expect(headers?.["x-api-key"]).toBe("both-key");
expect(headers?.authorization).toBe("Bearer both-token");
});
it("sends neither header when no credentials are configured", async () => {
const fetchSpy = vi.fn(() =>
Promise.resolve(new Response(JSON.stringify({ ok: true }), { status: 200, headers: { "content-type": "application/json" } }))
);
const client = createFetchHttpClient({ ...baseConfig, fetch: fetchSpy });
await client.request({ method: "GET", path: "/v1/test" });
expect(fetchSpy).toHaveBeenCalled();
const calls = fetchSpy.mock.calls as unknown as [unknown, Record<string, unknown>?][];
const init = calls[0]?.[1];
const headers = init?.headers as Record<string, string> | undefined;
expect(headers?.["x-api-key"]).toBeUndefined();
expect(headers?.authorization).toBeUndefined();
});
});