Skip to content

Commit e203056

Browse files
authored
Support custom request headers (#1019)
1 parent 3316ac9 commit e203056

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/CoreManager.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ const config: Config & { [key: string]: mixed } = {
171171
!!process.versions.node &&
172172
!process.versions.electron),
173173
REQUEST_ATTEMPT_LIMIT: 5,
174+
REQUEST_HEADERS: {},
174175
SERVER_URL: 'https://api.parse.com/1',
175176
SERVER_AUTH_TYPE: null,
176177
SERVER_AUTH_TOKEN: null,

src/RESTController.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ if (typeof XDomainRequest !== 'undefined' &&
4848
useXDomainRequest = true;
4949
}
5050

51-
function ajaxIE9(method: string, url: string, data: any, options?: FullOptions) {
51+
function ajaxIE9(method: string, url: string, data: any, headers?: any, options?: FullOptions) {
5252
return new Promise((resolve, reject) => {
5353
const xdr = new XDomainRequest();
5454
if (options && typeof options.requestTask === 'function') {
@@ -163,7 +163,10 @@ const RESTController = {
163163
if (CoreManager.get('SERVER_AUTH_TYPE') && CoreManager.get('SERVER_AUTH_TOKEN')) {
164164
headers['Authorization'] = CoreManager.get('SERVER_AUTH_TYPE') + ' ' + CoreManager.get('SERVER_AUTH_TOKEN');
165165
}
166-
166+
const customHeaders = CoreManager.get('REQUEST_HEADERS');
167+
for (const key in customHeaders) {
168+
headers[key] = customHeaders[key];
169+
}
167170
if(options && typeof options.progress === 'function') {
168171
if (xhr.upload) {
169172
xhr.upload.addEventListener('progress', (oEvent) => {

src/__tests__/RESTController-test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,4 +454,21 @@ describe('RESTController', () => {
454454
done();
455455
});
456456
});
457+
458+
it('opens a XHR with the custom headers', () => {
459+
CoreManager.set('REQUEST_HEADERS', { 'Cache-Control' : 'max-age=3600' });
460+
const xhr = {
461+
setRequestHeader: jest.fn(),
462+
open: jest.fn(),
463+
send: jest.fn()
464+
};
465+
RESTController._setXHR(function() { return xhr; });
466+
RESTController.ajax('GET', 'users/me', {}, { 'X-Parse-Session-Token': '123' });
467+
expect(xhr.setRequestHeader.mock.calls[3]).toEqual(
468+
[ 'Cache-Control', 'max-age=3600' ]
469+
);
470+
expect(xhr.open.mock.calls[0]).toEqual([ 'GET', 'users/me', true ]);
471+
expect(xhr.send.mock.calls[0][0]).toEqual({});
472+
CoreManager.set('REQUEST_HEADERS', {});
473+
});
457474
});

0 commit comments

Comments
 (0)