mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
Fixed merge conflicts
This commit is contained in:
@@ -33,6 +33,18 @@ describe('defaults', function () {
|
||||
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]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -24,7 +24,9 @@ describe('instance', function () {
|
||||
'getUri',
|
||||
'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]);
|
||||
});
|
||||
});
|
||||
@@ -10,6 +10,8 @@ var path = require('path');
|
||||
var pkg = require('./../../../package.json');
|
||||
var server, proxy;
|
||||
var AxiosError = require('../../../lib/core/AxiosError');
|
||||
var FormData = require('form-data');
|
||||
var formidable = require('formidable');
|
||||
|
||||
describe('supports http with nodejs', function () {
|
||||
|
||||
@@ -519,7 +521,7 @@ describe('supports http with nodejs', function () {
|
||||
|
||||
it('should display error while parsing params', function (done) {
|
||||
server = http.createServer(function () {
|
||||
|
||||
|
||||
}).listen(4444, function () {
|
||||
axios.get('http://localhost:4444/', {
|
||||
params: {
|
||||
@@ -1182,5 +1184,47 @@ describe('supports http with nodejs', 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
|
||||
}));
|
||||
});
|
||||
}).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);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
var assert = require('assert');
|
||||
var isFormData = require('../../../lib/utils').isFormData;
|
||||
var FormData = require('form-data');
|
||||
|
||||
describe('utils::isFormData', function () {
|
||||
it('should detect the FormData instance provided by the `form-data` package', function () {
|
||||
[1, 'str', {}, new RegExp()].forEach(function (thing) {
|
||||
assert.equal(isFormData(thing), false);
|
||||
});
|
||||
assert.equal(isFormData(new FormData()), true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user