Skip to content

Commit

Permalink
Adding a flag to disable automatic inclusion of the x-requested-with …
Browse files Browse the repository at this point in the history
…header, issue dojo#328
  • Loading branch information
rorticus committed Jul 31, 2017
1 parent 806b22c commit bbf0f44
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/request/providers/xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import SubscriptionPool from '../SubscriptionPool';
*/
export interface XhrRequestOptions extends RequestOptions {
blockMainThread?: boolean;
includeRequestedWithHeader?: boolean;
}

interface RequestData {
Expand Down Expand Up @@ -270,6 +271,7 @@ export default function xhr(url: string, options: XhrRequestOptions = {}): Uploa

let hasContentTypeHeader = false;
let hasRequestedWithHeader = false;
const { includeRequestedWithHeader = true } = options;

if (options.headers) {
const requestHeaders = new Headers(options.headers);
Expand All @@ -282,7 +284,7 @@ export default function xhr(url: string, options: XhrRequestOptions = {}): Uploa
});
}

if (!hasRequestedWithHeader) {
if (!hasRequestedWithHeader && includeRequestedWithHeader) {
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
}

Expand Down
12 changes: 12 additions & 0 deletions tests/unit/request/xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,18 @@ registerSuite({
}
});
});
},

'X-Requested-With headers can be disabled'(this: any) {
if (!echoServerAvailable) {
this.skip('No echo server available');
}
const options = {includeRequestedWithHeader: false};
return xhrRequest('/__echo/default', options).then(function (response: any) {
return response.json().then((data: any) => {
assert.isUndefined(data.headers['x-requested-with']);
});
});
}
}
},
Expand Down

0 comments on commit bbf0f44

Please sign in to comment.