Skip to content

Commit d5406c0

Browse files
committed
test(client): add tests for zero-length response handling across various content types
1 parent 47231c4 commit d5406c0

File tree

1 file changed

+150
-1
lines changed
  • packages/openapi-ts/src/plugins/@hey-api/client-fetch/__tests__

1 file changed

+150
-1
lines changed

packages/openapi-ts/src/plugins/@hey-api/client-fetch/__tests__/client.test.ts

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from 'vitest';
1+
import { describe, expect, it, vi } from 'vitest';
22

33
import { createClient } from '../bundle/client';
44

@@ -68,3 +68,152 @@ describe('buildUrl', () => {
6868
expect(client.buildUrl(options)).toBe(url);
6969
});
7070
});
71+
72+
describe('zero-length body handling', () => {
73+
const client = createClient({ baseUrl: 'https://example.com' });
74+
75+
it('returns empty Blob for zero-length application/octet-stream response', async () => {
76+
const mockResponse = new Response(null, {
77+
headers: {
78+
'Content-Length': '0',
79+
'Content-Type': 'application/octet-stream',
80+
},
81+
status: 200,
82+
});
83+
84+
const mockFetch = vi.fn().mockResolvedValue(mockResponse);
85+
86+
const result = await client.request({
87+
fetch: mockFetch,
88+
method: 'GET',
89+
url: '/test',
90+
});
91+
92+
expect(result.data).toBeInstanceOf(Blob);
93+
expect((result.data as Blob).size).toBe(0);
94+
});
95+
96+
it('returns empty ArrayBuffer for zero-length response with arrayBuffer parseAs', async () => {
97+
const mockResponse = new Response(null, {
98+
headers: {
99+
'Content-Length': '0',
100+
},
101+
status: 200,
102+
});
103+
104+
const mockFetch = vi.fn().mockResolvedValue(mockResponse);
105+
106+
const result = await client.request({
107+
fetch: mockFetch,
108+
method: 'GET',
109+
parseAs: 'arrayBuffer',
110+
url: '/test',
111+
});
112+
113+
expect(result.data).toBeInstanceOf(ArrayBuffer);
114+
expect((result.data as ArrayBuffer).byteLength).toBe(0);
115+
});
116+
117+
it('returns empty string for zero-length text response', async () => {
118+
const mockResponse = new Response(null, {
119+
headers: {
120+
'Content-Length': '0',
121+
'Content-Type': 'text/plain',
122+
},
123+
status: 200,
124+
});
125+
126+
const mockFetch = vi.fn().mockResolvedValue(mockResponse);
127+
128+
const result = await client.request({
129+
fetch: mockFetch,
130+
method: 'GET',
131+
url: '/test',
132+
});
133+
134+
expect(result.data).toBe('');
135+
});
136+
137+
it('returns empty object for zero-length JSON response', async () => {
138+
const mockResponse = new Response(null, {
139+
headers: {
140+
'Content-Length': '0',
141+
'Content-Type': 'application/json',
142+
},
143+
status: 200,
144+
});
145+
146+
const mockFetch = vi.fn().mockResolvedValue(mockResponse);
147+
148+
const result = await client.request({
149+
fetch: mockFetch,
150+
method: 'GET',
151+
url: '/test',
152+
});
153+
154+
expect(result.data).toEqual({});
155+
});
156+
157+
it('returns empty FormData for zero-length multipart/form-data response', async () => {
158+
const mockResponse = new Response(null, {
159+
headers: {
160+
'Content-Length': '0',
161+
'Content-Type': 'multipart/form-data',
162+
},
163+
status: 200,
164+
});
165+
166+
const mockFetch = vi.fn().mockResolvedValue(mockResponse);
167+
168+
const result = await client.request({
169+
fetch: mockFetch,
170+
method: 'GET',
171+
url: '/test',
172+
});
173+
174+
expect(result.data).toBeInstanceOf(FormData);
175+
expect([...(result.data as FormData).entries()]).toHaveLength(0);
176+
});
177+
178+
it('returns stream body for zero-length stream response', async () => {
179+
const mockBody = new ReadableStream();
180+
const mockResponse = new Response(mockBody, {
181+
headers: {
182+
'Content-Length': '0',
183+
},
184+
status: 200,
185+
});
186+
187+
const mockFetch = vi.fn().mockResolvedValue(mockResponse);
188+
189+
const result = await client.request({
190+
fetch: mockFetch,
191+
method: 'GET',
192+
parseAs: 'stream',
193+
url: '/test',
194+
});
195+
196+
expect(result.data).toBe(mockBody);
197+
});
198+
199+
it('handles non-zero content correctly for comparison', async () => {
200+
const blobContent = new Blob(['test data']);
201+
const mockResponse = new Response(blobContent, {
202+
headers: {
203+
'Content-Type': 'application/octet-stream',
204+
},
205+
status: 200,
206+
});
207+
208+
const mockFetch = vi.fn().mockResolvedValue(mockResponse);
209+
210+
const result = await client.request({
211+
fetch: mockFetch,
212+
method: 'GET',
213+
url: '/test',
214+
});
215+
216+
expect(result.data).toBeInstanceOf(Blob);
217+
expect((result.data as Blob).size).toBeGreaterThan(0);
218+
});
219+
});

0 commit comments

Comments
 (0)