mirror of
https://github.com/tenrok/axios.git
synced 2026-06-05 16:42:32 +03:00
feat(withXSRFToken): added withXSRFToken option as a workaround to achieve the old withCredentials behavior; (#6046)
This commit is contained in:
@@ -79,4 +79,68 @@ describe('xsrf', function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('withXSRFToken option', function(){
|
||||
|
||||
it('should set xsrf header for cross origin when withXSRFToken = true', function (done) {
|
||||
const token = '12345';
|
||||
|
||||
document.cookie = axios.defaults.xsrfCookieName + '=' + token;
|
||||
|
||||
axios('http://example.com/', {
|
||||
withXSRFToken: true
|
||||
});
|
||||
|
||||
getAjaxRequest().then(function (request) {
|
||||
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(token);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should not set xsrf header for the same origin when withXSRFToken = false', function (done) {
|
||||
const token = '12345';
|
||||
|
||||
document.cookie = axios.defaults.xsrfCookieName + '=' + token;
|
||||
|
||||
axios('/foo', {
|
||||
withXSRFToken: false
|
||||
});
|
||||
|
||||
getAjaxRequest().then(function (request) {
|
||||
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(undefined);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should not set xsrf header for the same origin when withXSRFToken = false', function (done) {
|
||||
const token = '12345';
|
||||
|
||||
document.cookie = axios.defaults.xsrfCookieName + '=' + token;
|
||||
|
||||
axios('/foo', {
|
||||
withXSRFToken: false
|
||||
});
|
||||
|
||||
getAjaxRequest().then(function (request) {
|
||||
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(undefined);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should support function resolver', (done) => {
|
||||
const token = '12345';
|
||||
|
||||
document.cookie = axios.defaults.xsrfCookieName + '=' + token;
|
||||
|
||||
axios('/foo', {
|
||||
withXSRFToken: (config) => config.userFlag === 'yes',
|
||||
userFlag: 'yes'
|
||||
});
|
||||
|
||||
getAjaxRequest().then(function (request) {
|
||||
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(token);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user