2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-11 18:02:32 +03:00

test: add Node unit tests for toFormData and refactor buildURL to avoid param reassignment (#7272)

Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
Nandan Acharya
2025-12-08 11:52:30 +05:30
committed by GitHub
parent f7bdcd1b6c
commit e0a120620e
2 changed files with 60 additions and 10 deletions
+53
View File
@@ -0,0 +1,53 @@
import assert from 'assert';
import toFormData from '../../../lib/helpers/toFormData.js';
import FormData from 'form-data';
describe('helpers::toFormData', function () {
it('should convert a flat object to FormData', function () {
const data = {
foo: 'bar',
baz: 123
};
const formData = toFormData(data, new FormData());
assert.ok(formData instanceof FormData);
// form-data package specific checks
assert.ok(formData._streams.length > 0);
});
it('should convert a nested object to FormData', function () {
const data = {
foo: {
bar: 'baz'
}
};
const formData = toFormData(data, new FormData());
assert.ok(formData instanceof FormData);
});
it('should throw Error on circular reference', function () {
const data = {
foo: 'bar'
};
data.self = data;
try {
toFormData(data, new FormData());
assert.fail('Should have thrown an error');
} catch (e) {
assert.strictEqual(e.message, 'Circular reference detected in self');
}
});
it('should handle arrays', function () {
const data = {
arr: [1, 2, 3]
};
const formData = toFormData(data, new FormData());
assert.ok(formData instanceof FormData);
});
});