Skip to content

Commit

Permalink
feat: Add authorization header as query option
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Nielsen authored and stephannielsen committed Aug 16, 2022
1 parent e08f3b6 commit 005e8d4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/RESTController.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type FullOptions = {
installationId?: string,
progress?: any,
usePost?: boolean,
authorizationHeader?: string,
};

let XHR = null;
Expand Down Expand Up @@ -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];
Expand Down
48 changes: 48 additions & 0 deletions src/__tests__/RESTController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 005e8d4

Please sign in to comment.