Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/react-native/Libraries/Network/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,9 @@ 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 ? existing + ', ' + String(value) : String(value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,32 @@ describe('XMLHttpRequest', function () {
expect(GlobalPerformanceLogger.stopTimespan).not.toHaveBeenCalled();
});

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 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');
Expand Down