这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,89 @@ describe('zero-length body handling', () => {
expect(result.data).toBeInstanceOf(Blob);
expect((result.data as Blob).size).toBeGreaterThan(0);
});

it('serializes a json string to a json-parseable body', async () => {
const mockResponse = new Response(null, {
status: 200,
});

const mockFetch: MockFetch = vi.fn().mockResolvedValue(mockResponse);

const result = await client.request({
body: { key: 'value' },
fetch: mockFetch,
method: 'PUT',
url: '/test',
});

expect(await result.request.text()).toBe('{"key":"value"}');
});

it('serializes an empty string to a json-parseable body', async () => {
const mockResponse = new Response(null, {
status: 200,
});

const mockFetch: MockFetch = vi.fn().mockResolvedValue(mockResponse);

const result = await client.request({
body: '',
fetch: mockFetch,
method: 'PUT',
url: '/test',
});

expect(await result.request.text()).toBe('""');
});

it('serializes `0` to a json-parseable body', async () => {
const mockResponse = new Response(null, {
status: 200,
});

const mockFetch: MockFetch = vi.fn().mockResolvedValue(mockResponse);

const result = await client.request({
body: 0,
fetch: mockFetch,
method: 'PUT',
url: '/test',
});

expect(await result.request.text()).toBe('0');
});

it('does not serialize a `null` body', async () => {
const mockResponse = new Response(null, {
status: 200,
});

const mockFetch: MockFetch = vi.fn().mockResolvedValue(mockResponse);

const result = await client.request({
body: null,
fetch: mockFetch,
method: 'PUT',
url: '/test',
});

expect(await result.request.text()).toBe('');
});

it('does not serialize an `undefined` body', async () => {
const mockResponse = new Response(null, {
status: 200,
});

const mockFetch: MockFetch = vi.fn().mockResolvedValue(mockResponse);

const result = await client.request({
body: undefined,
fetch: mockFetch,
method: 'PUT',
url: '/test',
});

expect(await result.request.text()).toBe('');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const createClient = (config: Config = {}): Client => {
await opts.requestValidator(opts);
}

if (opts.body && opts.bodySerializer) {
if (opts.body !== null && opts.body !== undefined && opts.bodySerializer) {
opts.serializedBody = opts.bodySerializer(opts.body);
}

Expand Down
Loading