forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-http-client-build-url.test.ts
More file actions
191 lines (164 loc) · 5.97 KB
/
Copy pathfetch-http-client-build-url.test.ts
File metadata and controls
191 lines (164 loc) · 5.97 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { describe, expect, it } from 'vitest';
import { createFetchHttpClient } from '../src/http/fetch-http-client';
import type { ResolvedLilySdkConfig } from '../src/config/types';
function createConfig(overrides: Partial<ResolvedLilySdkConfig> = {}): ResolvedLilySdkConfig {
return {
baseUrl: new URL('https://api.lily.test/'),
timeoutMs: 2_000,
retry: {
retries: 0,
retryDelayMs: 0,
retryableStatusCodes: [],
},
defaultHeaders: {},
userAgent: 'lily-sdk/test',
fetch: globalThis.fetch,
...overrides,
};
}
describe('buildUrl — query serialization and value encoding', () => {
// We test buildUrl indirectly through the fetch client by checking
// the URL passed to the fetch implementation.
it('serializes string query params', async () => {
let capturedUrl: URL | undefined;
const fetchSpy = (_input: URL | RequestInfo, _init?: RequestInit) => {
capturedUrl = _input as URL;
return Promise.resolve(new Response('{}', {
status: 200,
headers: { 'content-type': 'application/json' },
}));
};
const client = createFetchHttpClient(createConfig({ fetch: fetchSpy as any }));
await client.request({
method: 'GET',
path: '/v1/agents',
query: { name: 'agent-001' },
});
expect(capturedUrl).toBeDefined();
expect(capturedUrl!.searchParams.get('name')).toBe('agent-001');
});
it('serializes number query params as strings', async () => {
let capturedUrl: URL | undefined;
const fetchSpy = (_input: URL | RequestInfo, _init?: RequestInit) => {
capturedUrl = _input as URL;
return Promise.resolve(new Response('{}', {
status: 200,
headers: { 'content-type': 'application/json' },
}));
};
const client = createFetchHttpClient(createConfig({ fetch: fetchSpy as any }));
await client.request({
method: 'GET',
path: '/v1/agents',
query: { page: 5, limit: 20 },
});
expect(capturedUrl!.searchParams.get('page')).toBe('5');
expect(capturedUrl!.searchParams.get('limit')).toBe('20');
});
it('serializes boolean query params as strings', async () => {
let capturedUrl: URL | undefined;
const fetchSpy = (_input: URL | RequestInfo, _init?: RequestInit) => {
capturedUrl = _input as URL;
return Promise.resolve(new Response('{}', {
status: 200,
headers: { 'content-type': 'application/json' },
}));
};
const client = createFetchHttpClient(createConfig({ fetch: fetchSpy as any }));
await client.request({
method: 'GET',
path: '/v1/agents',
query: { active: true, verified: false },
});
expect(capturedUrl!.searchParams.get('active')).toBe('true');
expect(capturedUrl!.searchParams.get('verified')).toBe('false');
});
it('omits undefined query params', async () => {
let capturedUrl: URL | undefined;
const fetchSpy = (_input: URL | RequestInfo, _init?: RequestInit) => {
capturedUrl = _input as URL;
return Promise.resolve(new Response('{}', {
status: 200,
headers: { 'content-type': 'application/json' },
}));
};
const client = createFetchHttpClient(createConfig({ fetch: fetchSpy as any }));
await client.request({
method: 'GET',
path: '/v1/agents',
query: { name: 'test', page: undefined, limit: 10 },
});
expect(capturedUrl!.searchParams.has('page')).toBe(false);
expect(capturedUrl!.searchParams.get('name')).toBe('test');
expect(capturedUrl!.searchParams.get('limit')).toBe('10');
});
it('encodes special characters in query values', async () => {
let capturedUrl: URL | undefined;
const fetchSpy = (_input: URL | RequestInfo, _init?: RequestInit) => {
capturedUrl = _input as URL;
return Promise.resolve(new Response('{}', {
status: 200,
headers: { 'content-type': 'application/json' },
}));
};
const client = createFetchHttpClient(createConfig({ fetch: fetchSpy as any }));
await client.request({
method: 'GET',
path: '/v1/agents',
query: { search: 'hello world & foo=bar' },
});
expect(capturedUrl!.searchParams.get('search')).toBe('hello world & foo=bar');
});
it('handles empty query object', async () => {
let capturedUrl: URL | undefined;
const fetchSpy = (_input: URL | RequestInfo, _init?: RequestInit) => {
capturedUrl = _input as URL;
return Promise.resolve(new Response('{}', {
status: 200,
headers: { 'content-type': 'application/json' },
}));
};
const client = createFetchHttpClient(createConfig({ fetch: fetchSpy as any }));
await client.request({
method: 'GET',
path: '/v1/system/health',
query: {},
});
expect(capturedUrl!.search).toBe('');
});
it('handles undefined query (no query at all)', async () => {
let capturedUrl: URL | undefined;
const fetchSpy = (_input: URL | RequestInfo, _init?: RequestInit) => {
capturedUrl = _input as URL;
return Promise.resolve(new Response('{}', {
status: 200,
headers: { 'content-type': 'application/json' },
}));
};
const client = createFetchHttpClient(createConfig({ fetch: fetchSpy as any }));
await client.request({
method: 'GET',
path: '/v1/system/health',
});
expect(capturedUrl!.search).toBe('');
});
it('combines path with baseUrl correctly', async () => {
let capturedUrl: URL | undefined;
const fetchSpy = (_input: URL | RequestInfo, _init?: RequestInit) => {
capturedUrl = _input as URL;
return Promise.resolve(new Response('{}', {
status: 200,
headers: { 'content-type': 'application/json' },
}));
};
const client = createFetchHttpClient(createConfig({
baseUrl: new URL('https://api.lily.test/'),
fetch: fetchSpy as any,
}));
await client.request({
method: 'GET',
path: '/v1/wallets/w-1',
});
expect(capturedUrl!.href).toBe('https://api.lily.test/v1/wallets/w-1');
});
});