mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
Added enhanced toFormData implementation with additional options support; (#4704)
Updated default notation for arrays and objects to bracket style; Added `multer/express.js` tests; Updated README.md; Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -1,6 +1,89 @@
|
||||
var toFormData = require('../../../lib/helpers/toFormData');
|
||||
|
||||
describe('toFormData', function () {
|
||||
it('should convert nested data object to FormData with dots option enabled', function () {
|
||||
var o = {
|
||||
val: 123,
|
||||
nested: {
|
||||
arr: ['hello', 'world']
|
||||
}
|
||||
};
|
||||
|
||||
var form = toFormData(o, null, {dots: true});
|
||||
expect(form instanceof FormData).toEqual(true);
|
||||
expect(Array.from(form.keys()).length).toEqual(3);
|
||||
expect(form.get('val')).toEqual('123');
|
||||
expect(form.get('nested.arr.0')).toEqual('hello');
|
||||
});
|
||||
|
||||
it('should respect metaTokens option', function () {
|
||||
var data = {
|
||||
'obj{}': {x: 1, y: 2}
|
||||
};
|
||||
|
||||
var str = JSON.stringify(data['obj{}']);
|
||||
|
||||
var form = toFormData(data, null, {metaTokens: false});
|
||||
|
||||
expect(Array.from(form.keys()).length).toEqual(1);
|
||||
expect(form.getAll('obj')).toEqual([str]);
|
||||
});
|
||||
|
||||
describe('Flat arrays serialization', function () {
|
||||
it('should include full indexes when the `indexes` option is set to true', function () {
|
||||
var data = {
|
||||
arr: [1, 2, 3],
|
||||
arr2: [1, [2], 3]
|
||||
};
|
||||
|
||||
var form = toFormData(data, null, {indexes: true});
|
||||
|
||||
expect(Array.from(form.keys()).length).toEqual(6);
|
||||
|
||||
expect(form.get('arr[0]')).toEqual('1');
|
||||
expect(form.get('arr[1]')).toEqual('2');
|
||||
expect(form.get('arr[2]')).toEqual('3');
|
||||
|
||||
expect(form.get('arr2[0]')).toEqual('1');
|
||||
expect(form.get('arr2[1][0]')).toEqual('2');
|
||||
expect(form.get('arr2[2]')).toEqual('3');
|
||||
});
|
||||
|
||||
it('should include brackets only when the `indexes` option is set to false', function () {
|
||||
var data = {
|
||||
arr: [1, 2, 3],
|
||||
arr2: [1, [2], 3]
|
||||
};
|
||||
|
||||
var form = toFormData(data, null, {indexes: false});
|
||||
|
||||
expect(Array.from(form.keys()).length).toEqual(6);
|
||||
|
||||
expect(form.getAll('arr[]')).toEqual(['1', '2', '3']);
|
||||
|
||||
expect(form.get('arr2[0]')).toEqual('1');
|
||||
expect(form.get('arr2[1][0]')).toEqual('2');
|
||||
expect(form.get('arr2[2]')).toEqual('3');
|
||||
});
|
||||
|
||||
it('should omit brackets when the `indexes` option is set to null', function () {
|
||||
var data = {
|
||||
arr: [1, 2, 3],
|
||||
arr2: [1, [2], 3]
|
||||
};
|
||||
|
||||
var form = toFormData(data, null, {indexes: null});
|
||||
|
||||
expect(Array.from(form.keys()).length).toEqual(6);
|
||||
|
||||
expect(form.getAll('arr')).toEqual(['1', '2', '3']);
|
||||
|
||||
expect(form.get('arr2[0]')).toEqual('1');
|
||||
expect(form.get('arr2[1][0]')).toEqual('2');
|
||||
expect(form.get('arr2[2]')).toEqual('3');
|
||||
});
|
||||
});
|
||||
|
||||
it('should convert nested data object to FormData', function () {
|
||||
var o = {
|
||||
val: 123,
|
||||
@@ -13,7 +96,7 @@ describe('toFormData', function () {
|
||||
expect(form instanceof FormData).toEqual(true);
|
||||
expect(Array.from(form.keys()).length).toEqual(3);
|
||||
expect(form.get('val')).toEqual('123');
|
||||
expect(form.get('nested.arr.0')).toEqual('hello');
|
||||
expect(form.get('nested[arr][0]')).toEqual('hello');
|
||||
});
|
||||
|
||||
it('should append value whose key ends with [] as separate values with the same key', function () {
|
||||
|
||||
+74
-34
@@ -12,6 +12,8 @@ var server, proxy;
|
||||
var AxiosError = require('../../../lib/core/AxiosError');
|
||||
var FormData = require('form-data');
|
||||
var formidable = require('formidable');
|
||||
const express = require('express');
|
||||
const multer = require('multer');
|
||||
|
||||
describe('supports http with nodejs', function () {
|
||||
|
||||
@@ -1244,44 +1246,82 @@ describe('supports http with nodejs', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should allow passing FormData', function (done) {
|
||||
var form = new FormData();
|
||||
var file1= Buffer.from('foo', 'utf8');
|
||||
describe('FormData', function () {
|
||||
it('should allow passing FormData', function (done) {
|
||||
var form = new FormData();
|
||||
var file1 = Buffer.from('foo', 'utf8');
|
||||
|
||||
form.append('foo', "bar");
|
||||
form.append('file1', file1, {
|
||||
filename: 'bar.jpg',
|
||||
filepath: 'temp/bar.jpg',
|
||||
contentType: 'image/jpeg'
|
||||
});
|
||||
|
||||
server = http.createServer(function (req, res) {
|
||||
var receivedForm = new formidable.IncomingForm();
|
||||
|
||||
receivedForm.parse(req, function (err, fields, files) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
|
||||
res.end(JSON.stringify({
|
||||
fields: fields,
|
||||
files: files
|
||||
}));
|
||||
form.append('foo', "bar");
|
||||
form.append('file1', file1, {
|
||||
filename: 'bar.jpg',
|
||||
filepath: 'temp/bar.jpg',
|
||||
contentType: 'image/jpeg'
|
||||
});
|
||||
}).listen(4444, function () {
|
||||
axios.post('http://localhost:4444/', form, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data'
|
||||
}
|
||||
}).then(function (res) {
|
||||
assert.deepStrictEqual(res.data.fields,{foo: 'bar'});
|
||||
|
||||
assert.strictEqual(res.data.files.file1.mimetype,'image/jpeg');
|
||||
assert.strictEqual(res.data.files.file1.originalFilename,'temp/bar.jpg');
|
||||
assert.strictEqual(res.data.files.file1.size,3);
|
||||
server = http.createServer(function (req, res) {
|
||||
var receivedForm = new formidable.IncomingForm();
|
||||
|
||||
done();
|
||||
}).catch(done);
|
||||
receivedForm.parse(req, function (err, fields, files) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
|
||||
res.end(JSON.stringify({
|
||||
fields: fields,
|
||||
files: files
|
||||
}));
|
||||
});
|
||||
}).listen(4444, function () {
|
||||
axios.post('http://localhost:4444/', form, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data'
|
||||
}
|
||||
}).then(function (res) {
|
||||
assert.deepStrictEqual(res.data.fields, {foo: 'bar'});
|
||||
|
||||
assert.strictEqual(res.data.files.file1.mimetype, 'image/jpeg');
|
||||
assert.strictEqual(res.data.files.file1.originalFilename, 'temp/bar.jpg');
|
||||
assert.strictEqual(res.data.files.file1.size, 3);
|
||||
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
describe('toFormData helper', function () {
|
||||
it('should properly serialize nested objects for parsing with multer.js (express.js)', function (done) {
|
||||
const app = express();
|
||||
var obj = {
|
||||
arr1: ['1', '2', '3'],
|
||||
arr2: ['1', ['2'], '3'],
|
||||
obj: {x: '1', y: {z: '1'}},
|
||||
users: [{name: 'Peter', surname: 'griffin'}, {name: 'Thomas', surname: 'Anderson'}]
|
||||
};
|
||||
|
||||
app.post('/', multer().none(), function (req, res, next) {
|
||||
res.send(JSON.stringify(req.body));
|
||||
});
|
||||
|
||||
server = app.listen(3001, function () {
|
||||
// multer can parse the following key/value pairs to an array (indexes: null, false, true):
|
||||
// arr: '1'
|
||||
// arr: '2'
|
||||
// -------------
|
||||
// arr[]: '1'
|
||||
// arr[]: '2'
|
||||
// -------------
|
||||
// arr[0]: '1'
|
||||
// arr[1]: '2'
|
||||
// -------------
|
||||
Promise.all([null, false, true].map(function (mode) {
|
||||
return axios.postForm('http://localhost:3001/', obj, {formSerializer: {indexes: mode}})
|
||||
.then(function (res) {
|
||||
assert.deepStrictEqual(res.data, obj, 'Index mode ' + mode);
|
||||
});
|
||||
})).then(function (){
|
||||
done();
|
||||
}, done)
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user