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-parse-response.test.ts
More file actions
69 lines (57 loc) · 2.36 KB
/
Copy pathfetch-http-client-parse-response.test.ts
File metadata and controls
69 lines (57 loc) · 2.36 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
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { createFetchHttpClient } from '../src/http/fetch-http-client';
import { resolveLilySdkConfig } from '../src/config';
import type { HttpRequest } from '../src/http';
describe('fetch-http-client parseResponse', () => {
let originalFetch: typeof globalThis.fetch;
beforeEach(() => {
originalFetch = globalThis.fetch;
});
afterEach(() => {
globalThis.fetch = originalFetch;
});
it('returns null data for 204 No Content responses', async () => {
globalThis.fetch = vi.fn(() => {
return Promise.resolve(new Response(null, {
status: 204,
headers: {},
}));
});
const config = resolveLilySdkConfig({ baseUrl: 'https://api.example.com', apiKey: 'test-key' });
const client = createFetchHttpClient(config);
const request: HttpRequest = { method: 'DELETE', path: '/v1/resource/1' };
const response = await client.request(request);
expect(response.status).toBe(204);
expect(response.data).toBeNull();
});
it('returns raw string for text/plain responses', async () => {
const body = 'plain text response body';
globalThis.fetch = vi.fn(() => {
return Promise.resolve(new Response(body, {
status: 200,
headers: { 'content-type': 'text/plain' },
}));
});
const config = resolveLilySdkConfig({ baseUrl: 'https://api.example.com', apiKey: 'test-key' });
const client = createFetchHttpClient(config);
const request: HttpRequest = { method: 'GET', path: '/v1/text-endpoint' };
const response = await client.request(request);
expect(response.status).toBe(200);
expect(response.data).toBe(body);
});
it('parses application/json responses into objects', async () => {
const body = { id: 'abc-123', status: 'active' };
globalThis.fetch = vi.fn(() => {
return Promise.resolve(new Response(JSON.stringify(body), {
status: 200,
headers: { 'content-type': 'application/json' },
}));
});
const config = resolveLilySdkConfig({ baseUrl: 'https://api.example.com', apiKey: 'test-key' });
const client = createFetchHttpClient(config);
const request: HttpRequest = { method: 'GET', path: '/v1/json-endpoint' };
const response = await client.request(request);
expect(response.status).toBe(200);
expect(response.data).toEqual(body);
});
});