axios-axios/tests/unit/helpers/formDataToJSON.test.js
Jay d905b7598d
refactor: refresh test suite to be modernised (#7489)
* chore: port karma tests

* chore: port karma tests

* chore: port karma tests

* chore: tests

* chore: tests

* chore: tests

* chore: fix issues with port collisions

* refactor: utils tests

* refactor: utils tests

* refactor: utils tests

* refactor: tests to vitests

* refactor: tests to vitests

* refactor: tests to vitests

* refactor: tests to vitests

* refactor: tests to vitests

* refactor: tests to vitests

* refactor: tests to vitests

* refactor: ci

* chore: install pw deps

* chore: fixx ai feedback

* chore: wip compatability tests

* chore: wip compatability tests

* chore: wip compatability tests

* refactor: wip smoke

* chore: smoke test run

* chore: update unzip

* chore: update testing

* chore: update testing

* chore: update testing

* chore: update testing

* chore: update testing

* chore: skip tests that cannot run on node 16 and lower

* chore: fix 16x under tests

* chore: rest of tests

* fix: functions and runs

* feat: added tests for esm smoke

* feat: added smoke

* chore: ignore ai gen plans

* chore: ci fixes

* chore: fix small p2s
2026-03-12 15:27:09 +02:00

73 lines
1.7 KiB
JavaScript

import { describe, it, expect } from 'vitest';
import formDataToJSON from '../../../lib/helpers/formDataToJSON.js';
describe('formDataToJSON', () => {
it('should convert a FormData Object to JSON Object', () => {
const formData = new FormData();
formData.append('foo[bar][baz]', '123');
expect(formDataToJSON(formData)).toEqual({
foo: {
bar: {
baz: '123',
},
},
});
});
it('should convert repeatable values as an array', () => {
const formData = new FormData();
formData.append('foo', '1');
formData.append('foo', '2');
expect(formDataToJSON(formData)).toEqual({
foo: ['1', '2'],
});
});
it('should convert props with empty brackets to arrays', () => {
const formData = new FormData();
formData.append('foo[]', '1');
formData.append('foo[]', '2');
expect(formDataToJSON(formData)).toEqual({
foo: ['1', '2'],
});
});
it('should supported indexed arrays', () => {
const formData = new FormData();
formData.append('foo[0]', '1');
formData.append('foo[1]', '2');
expect(formDataToJSON(formData)).toEqual({
foo: ['1', '2'],
});
});
it('should resist prototype pollution CVE', () => {
const formData = new FormData();
formData.append('foo[0]', '1');
formData.append('foo[1]', '2');
formData.append('__proto__.x', 'hack');
formData.append('constructor.prototype.y', 'value');
expect(formDataToJSON(formData)).toEqual({
foo: ['1', '2'],
constructor: {
prototype: {
y: 'value',
},
},
});
expect({}.x).toEqual(undefined);
expect({}.y).toEqual(undefined);
});
});