fix: attachment sending (#11015)

* fix: attachment sending

* test: add tests

---------

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
Almeida
2025-07-30 11:53:44 +01:00
committed by GitHub
parent 593369dbb8
commit c3dc58169b
2 changed files with 39 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
import { Blob, Buffer } from 'node:buffer';
import { URLSearchParams } from 'node:url';
import { MockAgent, setGlobalDispatcher } from 'undici';
import { MockAgent, setGlobalDispatcher, FormData as UndiciFormData } from 'undici';
import type { Interceptable, MockInterceptor } from 'undici/types/mock-interceptor.js';
import { beforeEach, afterEach, test, expect, vitest } from 'vitest';
import { REST } from '../src/index.js';
@@ -77,6 +77,26 @@ test('resolveBody', async () => {
};
await expect(resolveBody(asyncIterable)).resolves.toStrictEqual(Buffer.from([1, 2, 3, 1, 2, 3, 1, 2, 3]));
{
const fd = new globalThis.FormData();
fd.append('key', 'value');
const resolved = await resolveBody(fd);
expect(resolved).toBeInstanceOf(UndiciFormData);
expect([...(resolved as UndiciFormData).entries()]).toStrictEqual([['key', 'value']]);
}
{
const ufd = new UndiciFormData();
ufd.append('key', 'value');
const resolved = await resolveBody(ufd);
expect(resolved).toBeInstanceOf(UndiciFormData);
expect([...(resolved as UndiciFormData).entries()]).toStrictEqual([['key', 'value']]);
}
// Unknown type
// @ts-expect-error: This test is ensuring that this throws
await expect(resolveBody(true)).rejects.toThrow(TypeError);