mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Added the ability for the url-encoded-form serializer to respect the formSerializer config; (#4721)
Added test for `formSerializer` config in context of `url-encoded-form` serializer;
This commit is contained in:
@@ -77,7 +77,7 @@ var defaults = {
|
|||||||
|
|
||||||
if (isObjectPayload) {
|
if (isObjectPayload) {
|
||||||
if (contentType.indexOf('application/x-www-form-urlencoded') !== -1) {
|
if (contentType.indexOf('application/x-www-form-urlencoded') !== -1) {
|
||||||
return toURLEncodedForm(data).toString();
|
return toURLEncodedForm(data, this.formSerializer).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((isFileList = utils.isFileList(data)) || contentType.indexOf('multipart/form-data') !== -1) {
|
if ((isFileList = utils.isFileList(data)) || contentType.indexOf('multipart/form-data') !== -1) {
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ var utils = require('../utils');
|
|||||||
var toFormData = require('./toFormData');
|
var toFormData = require('./toFormData');
|
||||||
var platform = require('../platform/');
|
var platform = require('../platform/');
|
||||||
|
|
||||||
module.exports = function toURLEncodedForm(data) {
|
module.exports = function toURLEncodedForm(data, options) {
|
||||||
return toFormData(data, new platform.classes.URLSearchParams(), {
|
return toFormData(data, new platform.classes.URLSearchParams(), Object.assign({
|
||||||
visitor: function(value, key, path, helpers) {
|
visitor: function(value, key, path, helpers) {
|
||||||
if (platform.isNode && utils.isBuffer(value)) {
|
if (platform.isNode && utils.isBuffer(value)) {
|
||||||
this.append(key, value.toString('base64'));
|
this.append(key, value.toString('base64'));
|
||||||
@@ -14,5 +14,5 @@ module.exports = function toURLEncodedForm(data) {
|
|||||||
|
|
||||||
return helpers.defaultVisitor.apply(this, arguments);
|
return helpers.defaultVisitor.apply(this, arguments);
|
||||||
}
|
}
|
||||||
});
|
}, options));
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
module.exports = (function getURLSearchParams(nativeURLSearchParams) {
|
module.exports = typeof URLSearchParams !== 'undefined' ? URLSearchParams : (function defineURLSearchParams() {
|
||||||
if (typeof nativeURLSearchParams === 'function') return nativeURLSearchParams;
|
|
||||||
|
|
||||||
function encode(str) {
|
function encode(str) {
|
||||||
var charMap = {
|
var charMap = {
|
||||||
'!': '%21',
|
'!': '%21',
|
||||||
@@ -35,4 +33,4 @@ module.exports = (function getURLSearchParams(nativeURLSearchParams) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return URLSearchParams;
|
return URLSearchParams;
|
||||||
})(URLSearchParams);
|
})();
|
||||||
|
|||||||
@@ -1404,8 +1404,42 @@ describe('supports http with nodejs', function () {
|
|||||||
.then(function (res) {
|
.then(function (res) {
|
||||||
assert.deepStrictEqual(res.data, obj);
|
assert.deepStrictEqual(res.data, obj);
|
||||||
done();
|
done();
|
||||||
}, done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should respect formSerializer config', function (done) {
|
||||||
|
const obj = {
|
||||||
|
arr1: ['1', '2', '3'],
|
||||||
|
arr2: ['1', ['2'], '3'],
|
||||||
|
};
|
||||||
|
|
||||||
|
const form = new URLSearchParams();
|
||||||
|
|
||||||
|
form.append('arr1[0]', '1');
|
||||||
|
form.append('arr1[1]', '2');
|
||||||
|
form.append('arr1[2]', '3');
|
||||||
|
|
||||||
|
form.append('arr2[0]', '1');
|
||||||
|
form.append('arr2[1][0]', '2');
|
||||||
|
form.append('arr2[2]', '3');
|
||||||
|
|
||||||
|
server = http.createServer(function (req, res) {
|
||||||
|
req.pipe(res);
|
||||||
|
}).listen(3001, () => {
|
||||||
|
return axios.post('http://localhost:3001/', obj, {
|
||||||
|
headers: {
|
||||||
|
'content-type': 'application/x-www-form-urlencoded'
|
||||||
|
},
|
||||||
|
formSerializer: {
|
||||||
|
indexes: true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(function (res) {
|
||||||
|
assert.strictEqual(res.data, form.toString());
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user