2
0
mirror of https://github.com/tenrok/axios.git synced 2026-05-24 14:04:14 +03:00
Files
axios/test/specs/transform.spec.js
T
2014-10-05 17:27:25 -06:00

72 lines
1.6 KiB
JavaScript

describe('transform', function () {
beforeEach(function () {
jasmine.Ajax.install();
});
it('should transform JSON to string', function () {
var data = {
foo: 'bar'
};
axios({
url: '/foo',
method: 'post',
data: data
});
var request = jasmine.Ajax.requests.mostRecent();
expect(request.params).toEqual('{"foo":"bar"}');
});
it('should override default transform', function () {
var data = {
foo: 'bar'
};
axios({
url: '/foo',
method: 'post',
data: data,
transformRequest: function (data) {
return data;
}
});
var request = jasmine.Ajax.requests.mostRecent();
expect(typeof request.params).toEqual('object');
});
it('should allow an Array of transformers', function () {
var data = {
foo: 'bar'
};
axios({
url: '/foo',
method: 'post',
data: data,
transformRequest: axios.defaults.transformRequest.concat(
function (data) {
return data.replace('bar', 'baz');
}
)
});
var request = jasmine.Ajax.requests.mostRecent();
expect(request.params).toEqual('{"foo":"baz"}');
});
it('should allowing mutating headers', function () {
var token = Math.floor(Math.random() * Math.pow(2, 64)).toString(36);
axios({
url: '/foo',
transformRequest: function (data, headers) {
headers['X-Authorization'] = token;
}
});
var request = jasmine.Ajax.requests.mostRecent();
expect(request.requestHeaders['X-Authorization']).toEqual(token);
});
});