Files
react-native/Libraries/Network/__tests__/FormData-test.js
T
bang9 d2e8e7d58e Fix FormData to properly handle appended arrays. (#32815)
Summary:
The Array appended to FormData must be transmitted in the form of a string.
However, it is treated as a file object and transmitted, because `typeof Array` is `'object'` too

In network
```js
form.append('array_name', ['a', 'b', 'c'])

// Browser
// Content-Disposition: form-data; name='array_name';
// a,b,c

// ReactNative
// Content-Disposition: form-data; name='array_name';
//
```

## Changelog
[General] [Fixed] - The Array appended to FormData is transmitted as a string

Pull Request resolved: https://github.com/facebook/react-native/pull/32815

Test Plan: Added test case

Reviewed By: lunaleaps

Differential Revision: D33369594

Pulled By: charlesbdudley

fbshipit-source-id: 0b5219a2c9f73cf16665dc417cceb4481428ad4e
2022-03-28 11:53:32 -07:00

81 lines
1.7 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @emails oncall+react_native
*/
'use strict';
const FormData = require('../FormData');
describe('FormData', function () {
var formData;
beforeEach(() => {
formData = new FormData();
});
afterEach(() => {
formData = null;
});
it('should return non blob null', function () {
formData.append('null', null);
const expectedPart = {
string: 'null',
headers: {
'content-disposition': 'form-data; name="null"',
},
fieldName: 'null',
};
expect(formData.getParts()[0]).toMatchObject(expectedPart);
});
it('should return blob', function () {
formData.append('photo', {
uri: 'arbitrary/path',
type: 'image/jpeg',
name: 'photo.jpg',
});
const expectedPart = {
uri: 'arbitrary/path',
type: 'image/jpeg',
name: 'photo.jpg',
headers: {
'content-disposition': 'form-data; name="photo"; filename="photo.jpg"',
'content-type': 'image/jpeg',
},
fieldName: 'photo',
};
expect(formData.getParts()[0]).toMatchObject(expectedPart);
});
it('should return non blob array', function () {
formData.append('array', [
true,
false,
undefined,
null,
{},
[],
'string',
0,
]);
const expectedPart = {
string: 'true,false,,,[object Object],,string,0',
headers: {
'content-disposition': 'form-data; name="array"',
},
fieldName: 'array',
};
expect(formData.getParts()[0]).toMatchObject(expectedPart);
});
});