forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault-headers.test.ts
More file actions
115 lines (96 loc) · 4.12 KB
/
Copy pathdefault-headers.test.ts
File metadata and controls
115 lines (96 loc) · 4.12 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { createFetchHttpClient } from '../src/http/fetch-http-client';
import type { ResolvedLilySdkConfig } from '../src/config/types';
function createMockConfig(overrides: Partial<ResolvedLilySdkConfig> = {}): ResolvedLilySdkConfig {
return {
baseUrl: new URL('https://api.example.com'),
apiKey: 'test-key',
authToken: undefined,
userAgent: 'lily-sdk/test',
defaultHeaders: {},
timeoutMs: 5000,
retry: { retries: 0, retryDelayMs: 100, retryableStatusCodes: [] },
fetch: vi.fn().mockResolvedValue({
ok: true,
status: 200,
headers: new Headers({ 'content-type': 'application/json' }),
json: async () => ({}),
text: async () => '{}',
}),
...overrides,
} as unknown as ResolvedLilySdkConfig;
}
describe('defaultHeaders merging', () => {
it('includes default headers in every request', async () => {
const config = createMockConfig({
defaultHeaders: { 'x-custom-header': 'custom-value' },
});
const client = createFetchHttpClient(config);
await client.request({ method: 'GET', path: '/v1/test' });
const calledInit = vi.mocked(config.fetch).mock.calls[0][1] as RequestInit;
const headers = calledInit.headers as Record<string, string>;
expect(headers['x-custom-header']).toBe('custom-value');
});
it('merges default headers with standard headers', async () => {
const config = createMockConfig({
defaultHeaders: { 'x-org-id': 'org-123' },
});
const client = createFetchHttpClient(config);
await client.request({ method: 'GET', path: '/v1/test' });
const calledInit = vi.mocked(config.fetch).mock.calls[0][1] as RequestInit;
const headers = calledInit.headers as Record<string, string>;
expect(headers['accept']).toBe('application/json');
expect(headers['content-type']).toBe('application/json');
expect(headers['user-agent']).toBe('lily-sdk/test');
expect(headers['x-org-id']).toBe('org-123');
});
it('allows request headers to override default headers', async () => {
const config = createMockConfig({
defaultHeaders: { 'x-priority': 'low' },
});
const client = createFetchHttpClient(config);
await client.request({
method: 'GET',
path: '/v1/test',
headers: { 'x-priority': 'high' },
});
const calledInit = vi.mocked(config.fetch).mock.calls[0][1] as RequestInit;
const headers = calledInit.headers as Record<string, string>;
expect(headers['x-priority']).toBe('high');
});
it('includes api key header when configured', async () => {
const config = createMockConfig({ apiKey: 'my-secret-key' });
const client = createFetchHttpClient(config);
await client.request({ method: 'GET', path: '/v1/test' });
const calledInit = vi.mocked(config.fetch).mock.calls[0][1] as RequestInit;
const headers = calledInit.headers as Record<string, string>;
expect(headers['x-api-key']).toBe('my-secret-key');
});
it('includes authorization header when auth token is configured', async () => {
const config = createMockConfig({ authToken: 'bearer-token-xyz' });
const client = createFetchHttpClient(config);
await client.request({ method: 'GET', path: '/v1/test' });
const calledInit = vi.mocked(config.fetch).mock.calls[0][1] as RequestInit;
const headers = calledInit.headers as Record<string, string>;
expect(headers['authorization']).toBe('Bearer bearer-token-xyz');
});
it('request headers take precedence over api key and auth token', async () => {
const config = createMockConfig({
apiKey: 'default-key',
authToken: 'default-token',
});
const client = createFetchHttpClient(config);
await client.request({
method: 'GET',
path: '/v1/test',
headers: {
'x-api-key': 'override-key',
authorization: 'Bearer override-token',
},
});
const calledInit = vi.mocked(config.fetch).mock.calls[0][1] as RequestInit;
const headers = calledInit.headers as Record<string, string>;
expect(headers['x-api-key']).toBe('default-key');
expect(headers['authorization']).toBe('Bearer default-token');
});
});