Skip to content

Commit

Permalink
fix(common): INT-2280 Allow removal of XSRF Token for remote API call
Browse files Browse the repository at this point in the history
  • Loading branch information
HadesDX committed May 28, 2020
1 parent 1773aae commit 3413456
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/request-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,25 @@ describe('RequestFactory', () => {

expect(xhr.open).toHaveBeenCalledWith('GET', `${url}?bar=bar&foo=foo&foobar=foo,bar`, true);
});

it('configures XHR object without null headers', () => {
const xhr = requestFactory.createRequest(url, {
credentials: false,
method: 'POST',
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
'X-XSRF-TOKEN': null,
Authorization: 'auth_key',
},
});

expect(xhr.withCredentials).toEqual(false);
expect(xhr.open).toHaveBeenCalledWith('POST', url, true);
expect(xhr.setRequestHeader).toHaveBeenCalledWith('Accept', 'application/json, text/plain, */*');
expect(xhr.setRequestHeader).toHaveBeenCalledWith('Content-Type', 'application/json');
expect(xhr.setRequestHeader).not.toHaveBeenCalledWith('X-XSRF-TOKEN', null);
expect(xhr.setRequestHeader).toHaveBeenCalledWith('Authorization', 'auth_key');
});
});
});
4 changes: 3 additions & 1 deletion src/request-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export default class RequestFactory {
}

private _configureRequestHeaders(xhr: XMLHttpRequest, headers: Headers): void {
Object.keys(headers).forEach(key => {
Object.keys(headers)
.filter(key => headers[key] !== null)
.forEach(key => {
xhr.setRequestHeader(key, headers[key]);
});
}
Expand Down

0 comments on commit 3413456

Please sign in to comment.