Skip to content

Commit 26e7640

Browse files
author
Stephan Nielsen
committed
feat: Add authorization header as query option
1 parent e4cdc30 commit 26e7640

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/RESTController.js

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type FullOptions = {
3535
installationId?: string,
3636
progress?: any,
3737
usePost?: boolean,
38+
authorizationHeader?: string,
3839
};
3940

4041
let XHR = null;
@@ -164,6 +165,9 @@ const RESTController = {
164165
headers['Authorization'] =
165166
CoreManager.get('SERVER_AUTH_TYPE') + ' ' + CoreManager.get('SERVER_AUTH_TOKEN');
166167
}
168+
if (options?.authorizationHeader) {
169+
headers['Authorization'] = options.authorizationHeader;
170+
}
167171
const customHeaders = CoreManager.get('REQUEST_HEADERS');
168172
for (const key in customHeaders) {
169173
headers[key] = customHeaders[key];

src/__tests__/RESTController-test.js

+48
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,54 @@ describe('RESTController', () => {
540540
CoreManager.set('SERVER_AUTH_TOKEN', null);
541541
});
542542

543+
it('sends auth header when auth header option is provided', async () => {
544+
const credentialsHeader = header => 'Authorization' === header[0];
545+
const xhr = {
546+
setRequestHeader: jest.fn(),
547+
open: jest.fn(),
548+
send: jest.fn(),
549+
};
550+
RESTController._setXHR(function () {
551+
return xhr;
552+
});
553+
RESTController.request(
554+
'GET',
555+
'classes/MyObject',
556+
{},
557+
{ authorizationHeader: 'Bearer some_random_token' }
558+
);
559+
await flushPromises();
560+
expect(xhr.setRequestHeader.mock.calls.filter(credentialsHeader)).toEqual([
561+
['Authorization', 'Bearer some_random_token'],
562+
]);
563+
});
564+
565+
it('auth header option overrides CoreManager auth header', async () => {
566+
CoreManager.set('SERVER_AUTH_TYPE', 'Bearer');
567+
CoreManager.set('SERVER_AUTH_TOKEN', 'some_random_token');
568+
const credentialsHeader = header => 'Authorization' === header[0];
569+
const xhr = {
570+
setRequestHeader: jest.fn(),
571+
open: jest.fn(),
572+
send: jest.fn(),
573+
};
574+
RESTController._setXHR(function () {
575+
return xhr;
576+
});
577+
RESTController.request(
578+
'GET',
579+
'classes/MyObject',
580+
{},
581+
{ authorizationHeader: 'Bearer some_other_random_token' }
582+
);
583+
await flushPromises();
584+
expect(xhr.setRequestHeader.mock.calls.filter(credentialsHeader)).toEqual([
585+
['Authorization', 'Bearer some_other_random_token'],
586+
]);
587+
CoreManager.set('SERVER_AUTH_TYPE', null);
588+
CoreManager.set('SERVER_AUTH_TOKEN', null);
589+
});
590+
543591
it('reports upload/download progress of the AJAX request when callback is provided', done => {
544592
const xhr = mockXHR([{ status: 200, response: { success: true } }], {
545593
progress: {

0 commit comments

Comments
 (0)