forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapikey-auth-matrix.test.ts
More file actions
78 lines (67 loc) · 3 KB
/
Copy pathapikey-auth-matrix.test.ts
File metadata and controls
78 lines (67 loc) · 3 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
import { describe, it, expect, vi } from 'vitest';
import { createFetchHttpClient } from '../src/http/fetch-http-client';
import { resolveLilySdkConfig } from '../src/config/resolve-config';
describe('auth header matrix', () => {
function captureHeaders(configOverrides: Record<string, unknown>) {
const mockFetch = vi.fn().mockResolvedValue(
new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: { 'content-type': 'application/json' },
}),
);
const config = resolveLilySdkConfig({
baseUrl: 'https://api.example.com',
fetch: mockFetch as typeof fetch,
...configOverrides,
});
const client = createFetchHttpClient(config);
return { client, mockFetch };
}
it('sends x-api-key when only apiKey is configured', async () => {
const { client, mockFetch } = captureHeaders({ apiKey: 'test-key' });
await client.request({ method: 'GET', path: '/test' });
expect(mockFetch).toHaveBeenCalledTimes(1);
const call = mockFetch.mock.calls[0];
if (!call) throw new Error('fetch not called');
const init = call[1] as RequestInit | undefined;
const headers = new Headers(init?.headers);
expect(headers.get('x-api-key')).toBe('test-key');
expect(headers.get('authorization')).toBeNull();
});
it('sends Bearer token when only authToken is configured', async () => {
const { client, mockFetch } = captureHeaders({ authToken: 'test-token' });
await client.request({ method: 'GET', path: '/test' });
expect(mockFetch).toHaveBeenCalledTimes(1);
const call = mockFetch.mock.calls[0];
if (!call) throw new Error('fetch not called');
const init = call[1] as RequestInit | undefined;
const headers = new Headers(init?.headers);
expect(headers.get('authorization')).toBe('Bearer test-token');
expect(headers.get('x-api-key')).toBeNull();
});
it('sends both headers when apiKey and authToken are configured', async () => {
const { client, mockFetch } = captureHeaders({
apiKey: 'test-key',
authToken: 'test-token',
});
await client.request({ method: 'GET', path: '/test' });
expect(mockFetch).toHaveBeenCalledTimes(1);
const call = mockFetch.mock.calls[0];
if (!call) throw new Error('fetch not called');
const init = call[1] as RequestInit | undefined;
const headers = new Headers(init?.headers);
expect(headers.get('x-api-key')).toBe('test-key');
expect(headers.get('authorization')).toBe('Bearer test-token');
});
it('sends neither auth header when no credentials are configured', async () => {
const { client, mockFetch } = captureHeaders({});
await client.request({ method: 'GET', path: '/test' });
expect(mockFetch).toHaveBeenCalledTimes(1);
const call = mockFetch.mock.calls[0];
if (!call) throw new Error('fetch not called');
const init = call[1] as RequestInit | undefined;
const headers = new Headers(init?.headers);
expect(headers.get('x-api-key')).toBeNull();
expect(headers.get('authorization')).toBeNull();
});
});