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-non-json.test.ts
More file actions
104 lines (87 loc) · 3.1 KB
/
Copy pathfetch-http-client-non-json.test.ts
File metadata and controls
104 lines (87 loc) · 3.1 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
import { describe, expect, it, vi } from 'vitest';
import { LilyApiError, LilyAuthenticationError, LilyTransportError } from '../src/errors/sdk-error';
import { createFetchHttpClient } from '../src/http/fetch-http-client';
import type { HttpRequest, HttpResponse } from '../src/http/types';
function createConfig(overrides: Record<string, unknown> = {}) {
return {
baseUrl: new URL('https://api.lily.test/'),
timeoutMs: 2_000,
retry: {
retries: 0,
retryDelayMs: 0,
retryableStatusCodes: [],
},
defaultHeaders: {},
userAgent: 'lily-sdk/test',
fetch: vi.fn(),
...overrides,
};
}
function jsonResponse(body: unknown, status = 200, headers: Record<string, string> = {}): Response {
return new Response(JSON.stringify(body), {
status,
headers: { 'content-type': 'application/json', ...headers },
});
}
function textResponse(body: string, status = 200): Response {
return new Response(body, {
status,
headers: { 'content-type': 'text/plain' },
});
}
function emptyResponse(status = 204): Response {
return new Response(null, {
status,
headers: { 'content-type': 'application/json' },
});
}
describe('fetch-http-client — non-JSON and 204 handling', () => {
it('returns null for 204 No Content responses', async () => {
const fetchSpy = vi.fn(() => Promise.resolve(emptyResponse(204)));
const client = createFetchHttpClient(createConfig({ fetch: fetchSpy }));
const response = await client.request({
method: 'GET',
path: '/v1/system/health',
});
expect(response.status).toBe(204);
expect(response.data).toBeNull();
});
it('returns text body for non-JSON content-type', async () => {
const fetchSpy = vi.fn(() => Promise.resolve(textResponse('plain text', 200)));
const client = createFetchHttpClient(createConfig({ fetch: fetchSpy }));
const response = await client.request({
method: 'GET',
path: '/v1/system/health',
});
expect(response.status).toBe(200);
expect(response.data).toBe('plain text');
});
it('returns text body for HTML content-type', async () => {
const htmlResponse = new Response('<html><body>Hello</body></html>', {
status: 200,
headers: { 'content-type': 'text/html' },
});
const fetchSpy = vi.fn(() => Promise.resolve(htmlResponse));
const client = createFetchHttpClient(createConfig({ fetch: fetchSpy }));
const response = await client.request({
method: 'GET',
path: '/v1/system/health',
});
expect(response.status).toBe(200);
expect(response.data).toBe('<html><body>Hello</body></html>');
});
it('returns text body when content-type is missing', async () => {
const noTypeResponse = new Response('no type', {
status: 200,
headers: {},
});
const fetchSpy = vi.fn(() => Promise.resolve(noTypeResponse));
const client = createFetchHttpClient(createConfig({ fetch: fetchSpy }));
const response = await client.request({
method: 'GET',
path: '/v1/system/health',
});
expect(response.status).toBe(200);
expect(typeof response.data).toBe('string');
});
});