2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-11 18:02:32 +03:00

Initial interceptor implementation.

This commit is contained in:
Jason Dobry
2014-12-02 15:45:19 -07:00
parent d93df704a2
commit 5bb39f3279
8 changed files with 680 additions and 168 deletions
+70 -34
View File
@@ -3,70 +3,106 @@ describe('transform', function () {
jasmine.Ajax.install();
});
afterEach(function () {
jasmine.Ajax.uninstall();
});
it('should transform JSON to string', function () {
var request;
var data = {
foo: 'bar'
};
axios({
url: '/foo',
method: 'post',
data: data
runs(function () {
axios({
url: '/foo',
method: 'post',
data: data
});
});
var request = jasmine.Ajax.requests.mostRecent();
expect(request.params).toEqual('{"foo":"bar"}');
waitsFor(function () {
return request = jasmine.Ajax.requests.mostRecent();
}, 'waiting for the request', 100);
runs(function () {
expect(request.params).toEqual('{"foo":"bar"}');
});
});
it('should override default transform', function () {
var request;
var data = {
foo: 'bar'
};
axios({
url: '/foo',
method: 'post',
data: data,
transformRequest: function (data) {
return data;
}
runs(function () {
axios({
url: '/foo',
method: 'post',
data: data,
transformRequest: function (data) {
return data;
}
});
});
var request = jasmine.Ajax.requests.mostRecent();
expect(typeof request.params).toEqual('object');
waitsFor(function () {
return request = jasmine.Ajax.requests.mostRecent();
}, 'waiting for the request', 100);
runs(function () {
expect(typeof request.params).toEqual('object');
});
});
it('should allow an Array of transformers', function () {
var request;
var data = {
foo: 'bar'
};
axios({
url: '/foo',
method: 'post',
data: data,
transformRequest: axios.defaults.transformRequest.concat(
function (data) {
return data.replace('bar', 'baz');
}
)
runs(function () {
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"}');
waitsFor(function () {
return request = jasmine.Ajax.requests.mostRecent();
}, 'waiting for the request', 100);
runs(function () {
expect(request.params).toEqual('{"foo":"baz"}');
});
});
it('should allowing mutating headers', function () {
var token = Math.floor(Math.random() * Math.pow(2, 64)).toString(36);
var request;
axios({
url: '/foo',
transformRequest: function (data, headers) {
headers['X-Authorization'] = token;
}
runs(function () {
axios({
url: '/foo',
transformRequest: function (data, headers) {
headers['X-Authorization'] = token;
}
});
});
var request = jasmine.Ajax.requests.mostRecent();
expect(request.requestHeaders['X-Authorization']).toEqual(token);
waitsFor(function () {
return request = jasmine.Ajax.requests.mostRecent();
}, 'waiting for the request', 100);
runs(function () {
expect(request.requestHeaders['X-Authorization']).toEqual(token);
});
});
});
});