/
githubmirror
/
axios
Обзор
Документация
Войти
/
githubmirror
/
axios
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
v1.x
tests/browser/toFormData.browser.test.js
129 строк
4 KB
mixelburg
fix: replace Buffer.from with Uint8Array in toFormData (#11018)
25 июн 2026, 21:59
Не верифицирован
25 июн 2026, 21:59
3243566
Код
Авторство
О чём код?
import { describe, expect, it } from 'vitest'; import toFormData from '../../lib/helpers/toFormData.js'; describe('helpers::toFormData (vitest browser)', () => { it('converts nested data object to FormData with dots option enabled', () => { const data = { val: 123, nested: { arr: ['hello', 'world'], }, }; const form = toFormData(data, null, { dots: true }); expect(form).toBeInstanceOf(FormData); expect(Array.from(form.keys())).toHaveLength(3); expect(form.get('val')).toBe('123'); expect(form.get('nested.arr.0')).toBe('hello'); }); it('respects metaTokens option', () => { const data = { 'obj{}': { x: 1, y: 2 }, }; const serialized = JSON.stringify(data['obj{}']); const form = toFormData(data, null, { metaTokens: false }); expect(Array.from(form.keys())).toHaveLength(1); expect(form.getAll('obj')).toEqual([serialized]); }); describe('flat arrays serialization', () => { it('includes full indexes when indexes option is true', () => { const data = { arr: [1, 2, 3], arr2: [1, [2], 3], }; const form = toFormData(data, null, { indexes: true }); expect(Array.from(form.keys())).toHaveLength(6); expect(form.get('arr[0]')).toBe('1'); expect(form.get('arr[1]')).toBe('2'); expect(form.get('arr[2]')).toBe('3'); expect(form.get('arr2[0]')).toBe('1'); expect(form.get('arr2[1][0]')).toBe('2'); expect(form.get('arr2[2]')).toBe('3'); }); it('includes brackets only when indexes option is false', () => { const data = { arr: [1, 2, 3], arr2: [1, [2], 3], }; const form = toFormData(data, null, { indexes: false }); expect(Array.from(form.keys())).toHaveLength(6); expect(form.getAll('arr[]')).toEqual(['1', '2', '3']); expect(form.get('arr2[0]')).toBe('1'); expect(form.get('arr2[1][0]')).toBe('2'); expect(form.get('arr2[2]')).toBe('3'); }); it('omits brackets when indexes option is null', () => { const data = { arr: [1, 2, 3], arr2: [1, [2], 3], }; const form = toFormData(data, null, { indexes: null }); expect(Array.from(form.keys())).toHaveLength(6); expect(form.getAll('arr')).toEqual(['1', '2', '3']); expect(form.get('arr2[0]')).toBe('1'); expect(form.get('arr2[1][0]')).toBe('2'); expect(form.get('arr2[2]')).toBe('3'); }); }); it('converts nested data object to FormData', () => { const data = { val: 123, nested: { arr: ['hello', 'world'], }, }; const form = toFormData(data); expect(form).toBeInstanceOf(FormData); expect(Array.from(form.keys())).toHaveLength(3); expect(form.get('val')).toBe('123'); expect(form.get('nested[arr][0]')).toBe('hello'); }); it('appends value whose key ends with [] as separate values with the same key', () => { const data = { 'arr[]': [1, 2, 3], }; const form = toFormData(data); expect(Array.from(form.keys())).toHaveLength(3); expect(form.getAll('arr[]')).toEqual(['1', '2', '3']); }); it('appends value whose key ends with {} as a JSON string', () => { const data = { 'obj{}': { x: 1, y: 2 }, }; const serialized = JSON.stringify(data['obj{}']); const form = toFormData(data); expect(Array.from(form.keys())).toHaveLength(1); expect(form.getAll('obj{}')).toEqual([serialized]); }); it('converts typed array values to Blob for browser FormData', async () => { const form = toFormData({ file: new Uint8Array([1, 2, 3]) }); const value = form.get('file'); expect(value).toBeInstanceOf(Blob); expect([...new Uint8Array(await value.arrayBuffer())]).toEqual([1, 2, 3]); }); });