diff --git a/packages/react-native/Libraries/Network/XMLHttpRequest.js b/packages/react-native/Libraries/Network/XMLHttpRequest.js index 7edb4c2a24c..3affe5a0e19 100644 --- a/packages/react-native/Libraries/Network/XMLHttpRequest.js +++ b/packages/react-native/Libraries/Network/XMLHttpRequest.js @@ -513,7 +513,10 @@ class XMLHttpRequest extends EventTarget { if (this.readyState !== this.OPENED) { throw new Error('Request has not been opened'); } - this._headers[header.toLowerCase()] = String(value); + const key = header.toLowerCase(); + const existing = this._headers[key]; + this._headers[key] = + existing !== undefined ? existing + ', ' + String(value) : String(value); } /** diff --git a/packages/react-native/Libraries/Network/__tests__/XMLHttpRequest-test.js b/packages/react-native/Libraries/Network/__tests__/XMLHttpRequest-test.js index e1c5ac7fde3..70fa857e2a8 100644 --- a/packages/react-native/Libraries/Network/__tests__/XMLHttpRequest-test.js +++ b/packages/react-native/Libraries/Network/__tests__/XMLHttpRequest-test.js @@ -268,6 +268,39 @@ describe('XMLHttpRequest', function () { ); }); + it('should append values when setRequestHeader is called with the same header', function () { + xhr.open('GET', 'blabla'); + xhr.setRequestHeader('X-Custom', 'value1'); + xhr.setRequestHeader('X-Custom', 'value2'); + + // $FlowFixMe[prop-missing] + expect(xhr._headers['x-custom']).toBe('value1, value2'); + }); + + it('should append to a header previously set to an empty string', function () { + xhr.open('GET', 'blabla'); + xhr.setRequestHeader('X-Custom', ''); + xhr.setRequestHeader('X-Custom', 'value'); + + // $FlowFixMe[prop-missing] + expect(xhr._headers['x-custom']).toBe(', value'); + }); + + it('should lowercase header names in setRequestHeader', function () { + xhr.open('GET', 'blabla'); + xhr.setRequestHeader('Content-Type', 'application/json'); + xhr.setRequestHeader('content-type', 'text/plain'); + + // $FlowFixMe[prop-missing] + expect(xhr._headers['content-type']).toBe('application/json, text/plain'); + }); + + it('should throw when setRequestHeader is called before open', function () { + expect(() => { + xhr.setRequestHeader('foo', 'bar'); + }).toThrow('Request has not been opened'); + }); + it('should sort and lowercase response headers', function () { // Derived from XHR Web Platform Test: https://github.com/web-platform-tests/wpt/blob/master/xhr/getallresponseheaders.htm xhr.open('GET', 'blabla');