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:
+120
-55
@@ -3,102 +3,167 @@ describe('wrapper', function () {
|
||||
jasmine.Ajax.install();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
jasmine.Ajax.uninstall();
|
||||
});
|
||||
|
||||
it('should make an http request', function () {
|
||||
axios({
|
||||
url: '/foo'
|
||||
var request;
|
||||
|
||||
runs(function () {
|
||||
axios({
|
||||
url: '/foo'
|
||||
});
|
||||
});
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
expect(request.url).toBe('/foo');
|
||||
waitsFor(function () {
|
||||
return request = jasmine.Ajax.requests.mostRecent();
|
||||
}, 'waiting for the request', 100);
|
||||
|
||||
runs(function () {
|
||||
expect(request.url).toBe('/foo')
|
||||
});
|
||||
});
|
||||
|
||||
it('should default common headers', function () {
|
||||
axios({
|
||||
url: '/foo'
|
||||
var request;
|
||||
var headers = axios.defaults.headers.common;
|
||||
|
||||
runs(function () {
|
||||
axios({
|
||||
url: '/foo'
|
||||
});
|
||||
});
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
var headers = axios.defaults.headers.common;
|
||||
for (var key in headers) {
|
||||
if (headers.hasOwnProperty(key)) {
|
||||
expect(request.requestHeaders[key]).toEqual(headers[key]);
|
||||
waitsFor(function () {
|
||||
return request = jasmine.Ajax.requests.mostRecent();
|
||||
}, 'waiting for the request', 100);
|
||||
|
||||
runs(function () {
|
||||
for (var key in headers) {
|
||||
if (headers.hasOwnProperty(key)) {
|
||||
expect(request.requestHeaders[key]).toEqual(headers[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should add extra headers for post', function () {
|
||||
axios({
|
||||
method: 'post',
|
||||
url: '/foo',
|
||||
data: 'fizz=buzz'
|
||||
var request;
|
||||
var headers = axios.defaults.headers.common;
|
||||
|
||||
runs(function () {
|
||||
axios({
|
||||
method: 'post',
|
||||
url: '/foo',
|
||||
data: 'fizz=buzz'
|
||||
});
|
||||
});
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
var headers = axios.defaults.headers.post;
|
||||
for (var key in headers) {
|
||||
if (headers.hasOwnProperty(key)) {
|
||||
expect(request.requestHeaders[key]).toEqual(headers[key]);
|
||||
waitsFor(function () {
|
||||
return request = jasmine.Ajax.requests.mostRecent();
|
||||
}, 'waiting for the request', 100);
|
||||
|
||||
runs(function () {
|
||||
for (var key in headers) {
|
||||
if (headers.hasOwnProperty(key)) {
|
||||
expect(request.requestHeaders[key]).toEqual(headers[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should use application/json when posting an object', function () {
|
||||
axios({
|
||||
url: '/foo/bar',
|
||||
method: 'post',
|
||||
data: {
|
||||
firstName: 'foo',
|
||||
lastName: 'bar'
|
||||
}
|
||||
var request;
|
||||
|
||||
runs(function () {
|
||||
axios({
|
||||
url: '/foo/bar',
|
||||
method: 'post',
|
||||
data: {
|
||||
firstName: 'foo',
|
||||
lastName: 'bar'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
expect(request.requestHeaders['Content-Type']).toEqual('application/json;charset=utf-8');
|
||||
waitsFor(function () {
|
||||
return request = jasmine.Ajax.requests.mostRecent();
|
||||
}, 'waiting for the request', 100);
|
||||
|
||||
runs(function () {
|
||||
expect(request.requestHeaders['Content-Type']).toEqual('application/json;charset=utf-8');
|
||||
});
|
||||
});
|
||||
|
||||
it('should support binary data as array buffer', function () {
|
||||
var request;
|
||||
var input = new Int8Array(2);
|
||||
input[0] = 1;
|
||||
input[1] = 2;
|
||||
|
||||
axios({
|
||||
method: 'post',
|
||||
url: '/foo',
|
||||
data: input.buffer
|
||||
runs(function () {
|
||||
axios({
|
||||
method: 'post',
|
||||
url: '/foo',
|
||||
data: input.buffer
|
||||
});
|
||||
});
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
var output = new Int8Array(request.params.buffer);
|
||||
expect(output.length).toEqual(2);
|
||||
expect(output[0]).toEqual(1);
|
||||
expect(output[1]).toEqual(2);
|
||||
waitsFor(function () {
|
||||
return request = jasmine.Ajax.requests.mostRecent();
|
||||
}, 'waiting for the request', 100);
|
||||
|
||||
runs(function () {
|
||||
var output = new Int8Array(request.params.buffer);
|
||||
expect(output.length).toEqual(2);
|
||||
expect(output[0]).toEqual(1);
|
||||
expect(output[1]).toEqual(2);
|
||||
});
|
||||
});
|
||||
|
||||
it('should support binary data as array buffer view', function () {
|
||||
var request;
|
||||
var input = new Int8Array(2);
|
||||
input[0] = 1;
|
||||
input[1] = 2;
|
||||
|
||||
axios({
|
||||
method: 'post',
|
||||
url: '/foo',
|
||||
data: input
|
||||
runs(function () {
|
||||
axios({
|
||||
method: 'post',
|
||||
url: '/foo',
|
||||
data: input
|
||||
});
|
||||
});
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
var output = new Int8Array(request.params.buffer);
|
||||
expect(output.length).toEqual(2);
|
||||
expect(output[0]).toEqual(1);
|
||||
expect(output[1]).toEqual(2);
|
||||
waitsFor(function () {
|
||||
return request = jasmine.Ajax.requests.mostRecent();
|
||||
}, 'waiting for the request', 100);
|
||||
|
||||
runs(function () {
|
||||
var output = new Int8Array(request.params.buffer);
|
||||
expect(output.length).toEqual(2);
|
||||
expect(output[0]).toEqual(1);
|
||||
expect(output[1]).toEqual(2);
|
||||
});
|
||||
});
|
||||
|
||||
it('should remove content-type if data is empty', function () {
|
||||
axios({
|
||||
method: 'post',
|
||||
url: '/foo'
|
||||
var request;
|
||||
|
||||
runs(function () {
|
||||
axios({
|
||||
method: 'post',
|
||||
url: '/foo'
|
||||
});
|
||||
});
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
expect(request.requestHeaders['content-type']).toEqual(undefined);
|
||||
waitsFor(function () {
|
||||
return request = jasmine.Ajax.requests.mostRecent();
|
||||
}, 'waiting for the request', 100);
|
||||
|
||||
runs(function () {
|
||||
expect(request.requestHeaders['content-type']).toEqual(undefined);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user