diff --git a/src/RESTController.js b/src/RESTController.js index 4e0653239..ba338a6e0 100644 --- a/src/RESTController.js +++ b/src/RESTController.js @@ -35,6 +35,7 @@ export type FullOptions = { installationId?: string, progress?: any, usePost?: boolean, + authorizationHeader?: string, }; let XHR = null; @@ -164,6 +165,9 @@ const RESTController = { headers['Authorization'] = CoreManager.get('SERVER_AUTH_TYPE') + ' ' + CoreManager.get('SERVER_AUTH_TOKEN'); } + if (options?.authorizationHeader) { + headers['Authorization'] = options.authorizationHeader; + } const customHeaders = CoreManager.get('REQUEST_HEADERS'); for (const key in customHeaders) { headers[key] = customHeaders[key]; diff --git a/src/__tests__/RESTController-test.js b/src/__tests__/RESTController-test.js index f2074c47a..f1b17a5f4 100644 --- a/src/__tests__/RESTController-test.js +++ b/src/__tests__/RESTController-test.js @@ -540,6 +540,54 @@ describe('RESTController', () => { CoreManager.set('SERVER_AUTH_TOKEN', null); }); + it('sends auth header when auth header option is provided', async () => { + const credentialsHeader = header => 'Authorization' === header[0]; + const xhr = { + setRequestHeader: jest.fn(), + open: jest.fn(), + send: jest.fn(), + }; + RESTController._setXHR(function () { + return xhr; + }); + RESTController.request( + 'GET', + 'classes/MyObject', + {}, + { authorizationHeader: 'Bearer some_random_token' } + ); + await flushPromises(); + expect(xhr.setRequestHeader.mock.calls.filter(credentialsHeader)).toEqual([ + ['Authorization', 'Bearer some_random_token'], + ]); + }); + + it('auth header option overrides CoreManager auth header', async () => { + CoreManager.set('SERVER_AUTH_TYPE', 'Bearer'); + CoreManager.set('SERVER_AUTH_TOKEN', 'some_random_token'); + const credentialsHeader = header => 'Authorization' === header[0]; + const xhr = { + setRequestHeader: jest.fn(), + open: jest.fn(), + send: jest.fn(), + }; + RESTController._setXHR(function () { + return xhr; + }); + RESTController.request( + 'GET', + 'classes/MyObject', + {}, + { authorizationHeader: 'Bearer some_other_random_token' } + ); + await flushPromises(); + expect(xhr.setRequestHeader.mock.calls.filter(credentialsHeader)).toEqual([ + ['Authorization', 'Bearer some_other_random_token'], + ]); + CoreManager.set('SERVER_AUTH_TYPE', null); + CoreManager.set('SERVER_AUTH_TOKEN', null); + }); + it('reports upload/download progress of the AJAX request when callback is provided', done => { const xhr = mockXHR([{ status: 200, response: { success: true } }], { progress: {