forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-matrix.test.ts
More file actions
74 lines (60 loc) · 3.28 KB
/
Copy pathauth-matrix.test.ts
File metadata and controls
74 lines (60 loc) · 3.28 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
import { describe, expect, it, vi } from 'vitest';
import { createFetchHttpClient } from '../src/http/fetch-http-client';
import { resolveLilySdkConfig } from '../src/config/resolve-config';
describe('auth header matrix', () => {
const baseConfig = {
baseUrl: '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 only apiKey is configured', async () => {
const fetchSpy = vi.fn(() => Promise.resolve(new Response('{}', { status: 200 })));
const config = resolveLilySdkConfig({ ...baseConfig, apiKey: 'test-key', fetch: fetchSpy });
const client = createFetchHttpClient(config);
await client.request({ method: 'GET', path: '/test' });
expect(fetchSpy).toHaveBeenCalledTimes(1);
const calls = fetchSpy.mock.calls as unknown as [RequestInfo | URL, RequestInit | undefined][];
const init = calls[0]?.[1];
const headers = new Headers(init?.headers);
expect(headers.get('x-api-key')).toBe('test-key');
expect(headers.has('authorization')).toBe(false);
});
it('sends only authorization bearer when only authToken is configured', async () => {
const fetchSpy = vi.fn(() => Promise.resolve(new Response('{}', { status: 200 })));
const config = resolveLilySdkConfig({ ...baseConfig, authToken: 'test-token', fetch: fetchSpy });
const client = createFetchHttpClient(config);
await client.request({ method: 'GET', path: '/test' });
expect(fetchSpy).toHaveBeenCalledTimes(1);
const calls = fetchSpy.mock.calls as unknown as [RequestInfo | URL, RequestInit | undefined][];
const init = calls[0]?.[1];
const headers = new Headers(init?.headers);
expect(headers.get('authorization')).toBe('Bearer test-token');
expect(headers.has('x-api-key')).toBe(false);
});
it('sends both headers when both credentials are configured', async () => {
const fetchSpy = vi.fn(() => Promise.resolve(new Response('{}', { status: 200 })));
const config = resolveLilySdkConfig({ ...baseConfig, apiKey: 'test-key', authToken: 'test-token', fetch: fetchSpy });
const client = createFetchHttpClient(config);
await client.request({ method: 'GET', path: '/test' });
expect(fetchSpy).toHaveBeenCalledTimes(1);
const calls = fetchSpy.mock.calls as unknown as [RequestInfo | URL, RequestInit | undefined][];
const init = calls[0]?.[1];
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 fetchSpy = vi.fn(() => Promise.resolve(new Response('{}', { status: 200 })));
const config = resolveLilySdkConfig({ ...baseConfig, fetch: fetchSpy });
const client = createFetchHttpClient(config);
await client.request({ method: 'GET', path: '/test' });
expect(fetchSpy).toHaveBeenCalledTimes(1);
const calls = fetchSpy.mock.calls as unknown as [RequestInfo | URL, RequestInit | undefined][];
const init = calls[0]?.[1];
const headers = new Headers(init?.headers);
expect(headers.has('x-api-key')).toBe(false);
expect(headers.has('authorization')).toBe(false);
});
});