Repository: parse-community/Parse-SDK-JS
Pull request #1019 (2019-12-03 00:04:53)

Support custom request headers
Fixes: https://github.com/parse-community/Parse-SDK-JS/issues/1014

Feed back would be appreciated.

codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1019?src=pr&el=h1) Report
> Merging [#1019](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1019?src=pr&el=desc) into [master](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/3316ac92f41129a50ec802b20822b12e8e006da2?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `75%`.

[![Impacted file tree graph](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1019/graphs/tree.svg?width=650&token=8nCENM0QHj&height=150&src=pr)](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1019?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1019      +/-   ##
==========================================
+ Coverage   91.94%   91.94%   +<.01%     
==========================================
  Files          53       53              
  Lines        5115     5118       +3     
  Branches     1145     1145              
==========================================
+ Hits         4703     4706       +3     
  Misses        412      412
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1019?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/CoreManager.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1019/diff?src=pr&el=tree#diff-c3JjL0NvcmVNYW5hZ2VyLmpz) | `100% <ø> (ø)` | :arrow_up: |
| [src/RESTController.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1019/diff?src=pr&el=tree#diff-c3JjL1JFU1RDb250cm9sbGVyLmpz) | `81.36% <75%> (+0.35%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1019?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1019?src=pr&el=footer). Last update [3316ac9...f44d886](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1019?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/CoreManager.js b/src/CoreManager.js
index dc45d43..4621dbc 100644
--- a/src/CoreManager.js
+++ b/src/CoreManager.js
@@ -171,6 +171,7 @@ const config: Config & { [key: string]: mixed } = {
             !!process.versions.node &&
             !process.versions.electron),
   REQUEST_ATTEMPT_LIMIT: 5,
+  REQUEST_HEADERS: {},
   SERVER_URL: 'https://api.parse.com/1',
   SERVER_AUTH_TYPE: null,
   SERVER_AUTH_TOKEN: null,
diff --git a/src/RESTController.js b/src/RESTController.js
index 4ebf31f..f02d17e 100644
--- a/src/RESTController.js
+++ b/src/RESTController.js
@@ -48,7 +48,7 @@ if (typeof XDomainRequest !== 'undefined' &&
   useXDomainRequest = true;
 }
 
-function ajaxIE9(method: string, url: string, data: any, options?: FullOptions) {
+function ajaxIE9(method: string, url: string, data: any, headers?: any, options?: FullOptions) {
   return new Promise((resolve, reject) => {
     const xdr = new XDomainRequest();
     if (options && typeof options.requestTask === 'function') {
@@ -163,7 +163,10 @@ const RESTController = {
       if (CoreManager.get('SERVER_AUTH_TYPE') && CoreManager.get('SERVER_AUTH_TOKEN')) {
         headers['Authorization'] = CoreManager.get('SERVER_AUTH_TYPE') + ' ' + CoreManager.get('SERVER_AUTH_TOKEN');
       }
-
+      const customHeaders = CoreManager.get('REQUEST_HEADERS');
+      for (const key in customHeaders) {
+        headers[key] = customHeaders[key];
+      }
       if(options && typeof options.progress === 'function') {
         if (xhr.upload) {
           xhr.upload.addEventListener('progress', (oEvent) => {


Test patch:
diff --git a/src/__tests__/RESTController-test.js b/src/__tests__/RESTController-test.js
index b4b8d71..1d68742 100644
--- a/src/__tests__/RESTController-test.js
+++ b/src/__tests__/RESTController-test.js
@@ -454,4 +454,21 @@ describe('RESTController', () => {
       done();
     });
   });
+
+  it('opens a XHR with the custom headers', () => {
+    CoreManager.set('REQUEST_HEADERS', { 'Cache-Control' : 'max-age=3600' });
+    const xhr = {
+      setRequestHeader: jest.fn(),
+      open: jest.fn(),
+      send: jest.fn()
+    };
+    RESTController._setXHR(function() { return xhr; });
+    RESTController.ajax('GET', 'users/me', {}, { 'X-Parse-Session-Token': '123' });
+    expect(xhr.setRequestHeader.mock.calls[3]).toEqual(
+      [ 'Cache-Control', 'max-age=3600' ]
+    );
+    expect(xhr.open.mock.calls[0]).toEqual([ 'GET', 'users/me', true ]);
+    expect(xhr.send.mock.calls[0][0]).toEqual({});
+    CoreManager.set('REQUEST_HEADERS', {});
+  });
 });