Skip to content

Commit

Permalink
Support custom request headers (#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
dplewis authored Dec 3, 2019
1 parent 3316ac9 commit e203056
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/CoreManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ const config: Config & { [key: string]: mixed } = {
!!process.versions.node &&
!process.versions.electron),
REQUEST_ATTEMPT_LIMIT: 5,
REQUEST_HEADERS: {},
SERVER_URL: 'https://api.parse.com/1',
SERVER_AUTH_TYPE: null,
SERVER_AUTH_TOKEN: null,
Expand Down
7 changes: 5 additions & 2 deletions src/RESTController.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ if (typeof XDomainRequest !== 'undefined' &&
useXDomainRequest = true;
}

function ajaxIE9(method: string, url: string, data: any, options?: FullOptions) {
function ajaxIE9(method: string, url: string, data: any, headers?: any, options?: FullOptions) {
return new Promise((resolve, reject) => {
const xdr = new XDomainRequest();
if (options && typeof options.requestTask === 'function') {
Expand Down Expand Up @@ -163,7 +163,10 @@ const RESTController = {
if (CoreManager.get('SERVER_AUTH_TYPE') && CoreManager.get('SERVER_AUTH_TOKEN')) {
headers['Authorization'] = CoreManager.get('SERVER_AUTH_TYPE') + ' ' + CoreManager.get('SERVER_AUTH_TOKEN');
}

const customHeaders = CoreManager.get('REQUEST_HEADERS');
for (const key in customHeaders) {
headers[key] = customHeaders[key];
}
if(options && typeof options.progress === 'function') {
if (xhr.upload) {
xhr.upload.addEventListener('progress', (oEvent) => {
Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/RESTController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,4 +454,21 @@ describe('RESTController', () => {
done();
});
});

it('opens a XHR with the custom headers', () => {
CoreManager.set('REQUEST_HEADERS', { 'Cache-Control' : 'max-age=3600' });
const xhr = {
setRequestHeader: jest.fn(),
open: jest.fn(),
send: jest.fn()
};
RESTController._setXHR(function() { return xhr; });
RESTController.ajax('GET', 'users/me', {}, { 'X-Parse-Session-Token': '123' });
expect(xhr.setRequestHeader.mock.calls[3]).toEqual(
[ 'Cache-Control', 'max-age=3600' ]
);
expect(xhr.open.mock.calls[0]).toEqual([ 'GET', 'users/me', true ]);
expect(xhr.send.mock.calls[0][0]).toEqual({});
CoreManager.set('REQUEST_HEADERS', {});
});
});

0 comments on commit e203056

Please sign in to comment.