mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Improved FormData support; (#4448)
* Fixed isFormData predicate;
Added support for automatic object serialization to FormData if `Content-Type` is `multipart/form-data`;
Added support for FormData to be overloaded using `config.env.FormData` option;
Added support for FormData in node.js environment through `form-data` package;
* Added the `form-data` package as a dependency for the server build;
Added tests for FormData payload;
* Added FormData automatic serialization section;
Refactored cancellation section;
* Reworked toFormData helper;
Expose toFormData helper as a static method;
Refactored transform request;
Added kindOf, kindOfTest, endsWith, isTypedArray util;
Refactored utils.js to use kindOf for tests;
* Fixed isFormData predicate; (#4413)
Added support for automatic object serialization to FormData if `Content-Type` is `multipart/form-data`;
Added support for FormData to be overloaded using `config.env.FormData` option;
Added support for FormData in node.js environment using `form-data` package;
(cherry picked from commit 73e3bdb883)
* Added shortcut methods `postForm`, `putForm`, `patchForm` to submit a Form;
Added ability to submit FileList object as a FormData;
Updated README.md;
* Updated README.md;
This commit is contained in:
@@ -31,7 +31,19 @@ describe('defaults', function () {
|
||||
expect(defaults.transformRequest[0](true, headers)).toEqual('true');
|
||||
expect(defaults.transformRequest[0](false, headers)).toEqual('false');
|
||||
expect(defaults.transformRequest[0](null, headers)).toEqual('null');
|
||||
});
|
||||
});
|
||||
|
||||
it("should transform the plain data object to a FormData instance 'Content-Type' if header is 'multipart/form-data'", function() {
|
||||
var headers = {
|
||||
'Content-Type': 'multipart/form-data'
|
||||
};
|
||||
|
||||
var payload = {x: 1};
|
||||
|
||||
var transformed = defaults.transformRequest[0](payload, headers);
|
||||
|
||||
expect(transformed).toEqual(jasmine.any(FormData));
|
||||
});
|
||||
|
||||
it('should do nothing to request string', function () {
|
||||
expect(defaults.transformRequest[0]('foo=bar')).toEqual('foo=bar');
|
||||
|
||||
@@ -1,18 +1,43 @@
|
||||
var toFormData = require("../../../lib/helpers/toFormData");
|
||||
var toFormData = require('../../../lib/helpers/toFormData');
|
||||
|
||||
describe("toFormData", function () {
|
||||
it("Convert nested data object to FormDAta", function () {
|
||||
describe('toFormData', function () {
|
||||
it('should convert nested data object to FormData', function () {
|
||||
var o = {
|
||||
val: 123,
|
||||
nested: {
|
||||
arr: ["hello", "world"],
|
||||
},
|
||||
arr: ['hello', 'world']
|
||||
}
|
||||
};
|
||||
|
||||
convertedVal = toFormData(o);
|
||||
expect(convertedVal instanceof FormData).toEqual(true);
|
||||
expect(Array.from(convertedVal.keys()).length).toEqual(3);
|
||||
expect(convertedVal.get("val")).toEqual("123")
|
||||
expect(convertedVal.get("nested.arr.0")).toEqual("hello")
|
||||
var form = toFormData(o);
|
||||
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 append value whose key ends with [] as separate values with the same key', function () {
|
||||
var data = {
|
||||
'arr[]': [1, 2, 3]
|
||||
};
|
||||
|
||||
var form = toFormData(data);
|
||||
|
||||
expect(Array.from(form.keys()).length).toEqual(3);
|
||||
expect(form.getAll('arr[]')).toEqual(['1', '2', '3']);
|
||||
});
|
||||
|
||||
it('should append value whose key ends with {} as a JSON string', function () {
|
||||
var data = {
|
||||
'obj{}': {x: 1, y: 2}
|
||||
};
|
||||
|
||||
var str = JSON.stringify(data['obj{}']);
|
||||
|
||||
var form = toFormData(data);
|
||||
|
||||
expect(Array.from(form.keys()).length).toEqual(1);
|
||||
expect(form.getAll('obj{}')).toEqual([str]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -21,7 +21,9 @@ describe('instance', function () {
|
||||
'spread',
|
||||
'isAxiosError',
|
||||
'VERSION',
|
||||
'default'].indexOf(prop) > -1) {
|
||||
'default',
|
||||
'toFormData'
|
||||
].indexOf(prop) > -1) {
|
||||
continue;
|
||||
}
|
||||
expect(typeof instance[prop]).toBe(typeof axios[prop]);
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
var kindOf = require('../../../lib/utils').kindOf;
|
||||
|
||||
describe('utils::kindOf', function () {
|
||||
it('should return object tag', function () {
|
||||
expect(kindOf({})).toEqual('object');
|
||||
// cached result
|
||||
expect(kindOf({})).toEqual('object');
|
||||
expect(kindOf([])).toEqual('array');
|
||||
});
|
||||
});
|
||||
@@ -77,4 +77,9 @@ describe('utils::isX', function () {
|
||||
expect(utils.isURLSearchParams(new URLSearchParams())).toEqual(true);
|
||||
expect(utils.isURLSearchParams('foo=1&bar=2')).toEqual(false);
|
||||
});
|
||||
|
||||
it('should validate TypedArray instance', function () {
|
||||
expect(utils.isTypedArray(new Uint8Array([1, 2, 3]))).toEqual(true);
|
||||
expect(utils.isTypedArray([1, 2, 3])).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
var kindOfTest = require('../../../lib/utils').kindOfTest;
|
||||
|
||||
describe('utils::endsWith', function () {
|
||||
it('should return true if the string ends with passed substring', function () {
|
||||
var test = kindOfTest('number');
|
||||
|
||||
expect(test(123)).toEqual(true);
|
||||
expect(test('123')).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
var kindOf = require('../../../lib/utils').kindOf;
|
||||
|
||||
describe('utils::kindOf', function () {
|
||||
it('should return object tag', function () {
|
||||
expect(kindOf({})).toEqual('object');
|
||||
// cached result
|
||||
expect(kindOf({})).toEqual('object');
|
||||
expect(kindOf([])).toEqual('array');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
var toArray = require('../../../lib/utils').toArray;
|
||||
|
||||
describe('utils::kindOf', function () {
|
||||
it('should return object tag', function () {
|
||||
expect(toArray()).toEqual(null);
|
||||
expect(toArray([])).toEqual([]);
|
||||
expect(toArray([1])).toEqual([1]);
|
||||
expect(toArray([1, 2, 3])).toEqual([1, 2, 3]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user