diff --git a/Libraries/Network/FormData.js b/Libraries/Network/FormData.js index 3392ada7e9..d1ec8116ad 100644 --- a/Libraries/Network/FormData.js +++ b/Libraries/Network/FormData.js @@ -64,6 +64,12 @@ class FormData { this._parts.push([key, value]); } + getAll(key: string): Array { + return this._parts + .filter(([name]) => name === key) + .map(([, value]) => value); + } + getParts(): Array { return this._parts.map(([name, value]) => { const contentDisposition = 'form-data; name="' + name + '"'; diff --git a/Libraries/Network/__tests__/FormData-test.js b/Libraries/Network/__tests__/FormData-test.js index e8316b30b1..329fc0d4cc 100644 --- a/Libraries/Network/__tests__/FormData-test.js +++ b/Libraries/Network/__tests__/FormData-test.js @@ -77,4 +77,35 @@ describe('FormData', function () { }; expect(formData.getParts()[0]).toMatchObject(expectedPart); }); + + it('should return values based on the given key', function () { + formData.append('username', 'Chris'); + formData.append('username', 'Bob'); + + expect(formData.getAll('username').length).toBe(2); + + expect(formData.getAll('username')).toMatchObject(['Chris', 'Bob']); + + formData.append('photo', { + uri: 'arbitrary/path', + type: 'image/jpeg', + name: 'photo3.jpg', + }); + + formData.append('photo', { + uri: 'arbitrary/path', + type: 'image/jpeg', + name: 'photo2.jpg', + }); + + const expectedPart = { + uri: 'arbitrary/path', + type: 'image/jpeg', + name: 'photo2.jpg', + }; + + expect(formData.getAll('photo')[1]).toMatchObject(expectedPart); + + expect(formData.getAll('file').length).toBe(0); + }); });