bugfix: don't JSON.parse empty 200/201 bodies (0.19.1)
$(addMangaToCollection crashed when the backend returned 201/200 with no body — the shared client only short-circuited 204. Now any empty body returns undefined.) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach, type MockInstance } from 'vitest';
|
||||
import { ApiError } from './client';
|
||||
import { ApiError, request } from './client';
|
||||
import { getManga } from './mangas';
|
||||
|
||||
describe('request error envelope parsing', () => {
|
||||
@@ -48,6 +48,20 @@ describe('request error envelope parsing', () => {
|
||||
expect(err.code).toBe('http_error');
|
||||
});
|
||||
|
||||
it('treats empty 200/201 bodies as undefined (no JSON.parse crash)', async () => {
|
||||
// Regression: addMangaToCollection is typed `void` and the
|
||||
// backend returns 201 (created) / 200 (already there) with
|
||||
// no body. Without the empty-body short-circuit, `res.json()`
|
||||
// would throw `JSON.parse: unexpected end of data`.
|
||||
fetchSpy.mockResolvedValueOnce(new Response(null, { status: 201 }));
|
||||
const created = await request<void>('/v1/whatever', { method: 'POST' });
|
||||
expect(created).toBeUndefined();
|
||||
|
||||
fetchSpy.mockResolvedValueOnce(new Response(null, { status: 200 }));
|
||||
const ok200 = await request<void>('/v1/whatever', { method: 'POST' });
|
||||
expect(ok200).toBeUndefined();
|
||||
});
|
||||
|
||||
it('falls back to http_error code when JSON has no error envelope', async () => {
|
||||
fetchSpy.mockResolvedValueOnce(
|
||||
new Response(JSON.stringify({ message: 'oops' }), {
|
||||
|
||||
Reference in New Issue
Block a user