Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add authorization header as query option #1529

Open
wants to merge 1 commit into
base: alpha
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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