forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-headers.test.ts
More file actions
43 lines (37 loc) · 1.34 KB
/
Copy pathauth-headers.test.ts
File metadata and controls
43 lines (37 loc) · 1.34 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
import { describe, expect, it } from 'vitest';
import { toHeaders } from '../src/http/auth-headers';
import type { ResolvedLilySdkConfig } from '../src/config/types';
function makeConfig(
overrides: Partial<ResolvedLilySdkConfig> = {},
): ResolvedLilySdkConfig {
return {
baseUrl: new URL('https://api.lily.test/'),
timeoutMs: 10_000,
retry: { retries: 2, retryDelayMs: 250, retryableStatusCodes: [] },
defaultHeaders: {},
userAgent: 'lily-sdk/test',
fetch: globalThis.fetch,
...overrides,
};
}
describe('toHeaders', () => {
it('returns only apiKey header when only apiKey is set', () => {
const headers = toHeaders(makeConfig({ apiKey: 'my-key' }));
expect(headers).toEqual({ 'x-api-key': 'my-key' });
});
it('returns only Authorization header when only authToken is set', () => {
const headers = toHeaders(makeConfig({ authToken: 'my-token' }));
expect(headers).toEqual({ authorization: 'Bearer my-token' });
});
it('returns both headers when apiKey and authToken are set', () => {
const headers = toHeaders(makeConfig({ apiKey: 'k', authToken: 't' }));
expect(headers).toEqual({
'x-api-key': 'k',
authorization: 'Bearer t',
});
});
it('returns empty object when neither is set', () => {
const headers = toHeaders(makeConfig());
expect(headers).toEqual({});
});
});