8 Star 48 Fork 0

Gitee 极速下载 / axios

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/mzabriskie/axios
克隆/下载
interceptors.spec.js 16.83 KB
一键复制 编辑 原始数据 按行查看 历史
Dmitriy Mozgovoy 提交于 2022-06-18 12:19 . Axios ES2017 (#4787)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
describe('interceptors', function () {
beforeEach(function () {
jasmine.Ajax.install();
});
afterEach(function () {
jasmine.Ajax.uninstall();
axios.interceptors.request.handlers = [];
axios.interceptors.response.handlers = [];
});
it('should add a request interceptor (asynchronous by default)', function (done) {
let asyncFlag = false;
axios.interceptors.request.use(function (config) {
config.headers.test = 'added by interceptor';
expect(asyncFlag).toBe(true);
return config;
});
axios('/foo');
asyncFlag = true;
getAjaxRequest().then(function (request) {
expect(request.requestHeaders.test).toBe('added by interceptor');
done();
});
});
it('should add a request interceptor (explicitly flagged as asynchronous)', function (done) {
let asyncFlag = false;
axios.interceptors.request.use(function (config) {
config.headers.test = 'added by interceptor';
expect(asyncFlag).toBe(true);
return config;
}, null, { synchronous: false });
axios('/foo');
asyncFlag = true;
getAjaxRequest().then(function (request) {
expect(request.requestHeaders.test).toBe('added by interceptor');
done();
});
});
it('should add a request interceptor that is executed synchronously when flag is provided', function (done) {
let asyncFlag = false;
axios.interceptors.request.use(function (config) {
config.headers.test = 'added by synchronous interceptor';
expect(asyncFlag).toBe(false);
return config;
}, null, { synchronous: true });
axios('/foo');
asyncFlag = true;
getAjaxRequest().then(function (request) {
expect(request.requestHeaders.test).toBe('added by synchronous interceptor');
done();
});
});
it('should execute asynchronously when not all interceptors are explicitly flagged as synchronous', function (done) {
let asyncFlag = false;
axios.interceptors.request.use(function (config) {
config.headers.foo = 'uh oh, async';
expect(asyncFlag).toBe(true);
return config;
});
axios.interceptors.request.use(function (config) {
config.headers.test = 'added by synchronous interceptor';
expect(asyncFlag).toBe(true);
return config;
}, null, { synchronous: true });
axios.interceptors.request.use(function (config) {
config.headers.test = 'added by the async interceptor';
expect(asyncFlag).toBe(true);
return config;
});
axios('/foo');
asyncFlag = true;
getAjaxRequest().then(function (request) {
expect(request.requestHeaders.foo).toBe('uh oh, async');
/* request interceptors have a reversed execution order */
expect(request.requestHeaders.test).toBe('added by synchronous interceptor');
done();
});
});
it('runs the interceptor if runWhen function is provided and resolves to true', function (done) {
function onGetCall(config) {
return config.method === 'get';
}
axios.interceptors.request.use(function (config) {
config.headers.test = 'special get headers';
return config;
}, null, { runWhen: onGetCall });
axios('/foo');
getAjaxRequest().then(function (request) {
expect(request.requestHeaders.test).toBe('special get headers');
done();
});
});
it('does not run the interceptor if runWhen function is provided and resolves to false', function (done) {
function onPostCall(config) {
return config.method === 'post';
}
axios.interceptors.request.use(function (config) {
config.headers.test = 'special get headers';
return config;
}, null, { runWhen: onPostCall });
axios('/foo');
getAjaxRequest().then(function (request) {
expect(request.requestHeaders.test).toBeUndefined()
done();
});
});
it('does not run async interceptor if runWhen function is provided and resolves to false (and run synchronously)', function (done) {
let asyncFlag = false;
function onPostCall(config) {
return config.method === 'post';
}
axios.interceptors.request.use(function (config) {
config.headers.test = 'special get headers';
return config;
}, null, { synchronous: false, runWhen: onPostCall });
axios.interceptors.request.use(function (config) {
config.headers.sync = 'hello world';
expect(asyncFlag).toBe(false);
return config;
}, null, { synchronous: true });
axios('/foo');
asyncFlag = true
getAjaxRequest().then(function (request) {
expect(request.requestHeaders.test).toBeUndefined()
expect(request.requestHeaders.sync).toBe('hello world')
done();
});
});
it('should add a request interceptor with an onRejected block that is called if interceptor code fails', function (done) {
const rejectedSpy = jasmine.createSpy('rejectedSpy');
const error = new Error('deadly error');
axios.interceptors.request.use(function () {
throw error;
}, rejectedSpy, { synchronous: true });
axios('/foo').catch(done);
getAjaxRequest().then(function () {
expect(rejectedSpy).toHaveBeenCalledWith(error);
done();
});
});
it('should add a request interceptor that returns a new config object', function (done) {
axios.interceptors.request.use(function () {
return {
url: '/bar',
method: 'post'
};
});
axios('/foo');
getAjaxRequest().then(function (request) {
expect(request.method).toBe('POST');
expect(request.url).toBe('/bar');
done();
});
});
it('should add a request interceptor that returns a promise', function (done) {
axios.interceptors.request.use(function (config) {
return new Promise(function (resolve) {
// do something async
setTimeout(function () {
config.headers.async = 'promise';
resolve(config);
}, 100);
});
});
axios('/foo');
getAjaxRequest().then(function (request) {
expect(request.requestHeaders.async).toBe('promise');
done();
});
});
it('should add multiple request interceptors', function (done) {
axios.interceptors.request.use(function (config) {
config.headers.test1 = '1';
return config;
});
axios.interceptors.request.use(function (config) {
config.headers.test2 = '2';
return config;
});
axios.interceptors.request.use(function (config) {
config.headers.test3 = '3';
return config;
});
axios('/foo');
getAjaxRequest().then(function (request) {
expect(request.requestHeaders.test1).toBe('1');
expect(request.requestHeaders.test2).toBe('2');
expect(request.requestHeaders.test3).toBe('3');
done();
});
});
it('should add a response interceptor', function (done) {
let response;
axios.interceptors.response.use(function (data) {
data.data = data.data + ' - modified by interceptor';
return data;
});
axios('/foo').then(function (data) {
response = data;
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 200,
responseText: 'OK'
});
setTimeout(function () {
expect(response.data).toBe('OK - modified by interceptor');
done();
}, 100);
});
});
it('should add a response interceptor when request interceptor is defined', function (done) {
let response;
axios.interceptors.request.use(function (data) {
return data;
});
axios.interceptors.response.use(function (data) {
data.data = data.data + ' - modified by interceptor';
return data;
});
axios('/foo').then(function (data) {
response = data;
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 200,
responseText: 'OK'
});
setTimeout(function () {
expect(response.data).toBe('OK - modified by interceptor');
done();
}, 100);
});
});
it('should add a response interceptor that returns a new data object', function (done) {
let response;
axios.interceptors.response.use(function () {
return {
data: 'stuff'
};
});
axios('/foo').then(function (data) {
response = data;
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 200,
responseText: 'OK'
});
setTimeout(function () {
expect(response.data).toBe('stuff');
done();
}, 100);
});
});
it('should add a response interceptor that returns a promise', function (done) {
let response;
axios.interceptors.response.use(function (data) {
return new Promise(function (resolve) {
// do something async
setTimeout(function () {
data.data = 'you have been promised!';
resolve(data);
}, 10);
});
});
axios('/foo').then(function (data) {
response = data;
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 200,
responseText: 'OK'
});
setTimeout(function () {
expect(response.data).toBe('you have been promised!');
done();
}, 100);
});
});
describe('given you add multiple response interceptors', function () {
describe('and when the response was fulfilled', function () {
function fireRequestAndExpect(expectation) {
let response;
axios('/foo').then(function(data) {
response = data;
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 200,
responseText: 'OK'
});
setTimeout(function() {
expectation(response)
}, 100);
});
}
it('then each interceptor is executed', function (done) {
const interceptor1 = jasmine.createSpy('interceptor1');
const interceptor2 = jasmine.createSpy('interceptor2');
axios.interceptors.response.use(interceptor1);
axios.interceptors.response.use(interceptor2);
fireRequestAndExpect(function () {
expect(interceptor1).toHaveBeenCalled();
expect(interceptor2).toHaveBeenCalled();
done();
});
});
it('then they are executed in the order they were added', function (done) {
const interceptor1 = jasmine.createSpy('interceptor1');
const interceptor2 = jasmine.createSpy('interceptor2');
axios.interceptors.response.use(interceptor1);
axios.interceptors.response.use(interceptor2);
fireRequestAndExpect(function () {
expect(interceptor1).toHaveBeenCalledBefore(interceptor2);
done();
});
});
it('then only the last interceptor\'s result is returned', function (done) {
axios.interceptors.response.use(function() {
return 'response 1';
});
axios.interceptors.response.use(function() {
return 'response 2';
});
fireRequestAndExpect(function (response) {
expect(response).toBe('response 2');
done();
});
});
it('then every interceptor receives the result of it\'s predecessor', function (done) {
axios.interceptors.response.use(function() {
return 'response 1';
});
axios.interceptors.response.use(function(response) {
return [response, 'response 2'];
});
fireRequestAndExpect(function (response) {
expect(response).toEqual(['response 1', 'response 2']);
done();
});
});
describe('and when the fulfillment-interceptor throws', function () {
function fireRequestCatchAndExpect(expectation) {
axios('/foo').catch(function(data) {
// dont handle result
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 200,
responseText: 'OK'
});
setTimeout(function() {
expectation()
}, 100);
});
}
it('then the following fulfillment-interceptor is not called', function (done) {
axios.interceptors.response.use(function() {
throw Error('throwing interceptor');
});
const interceptor2 = jasmine.createSpy('interceptor2');
axios.interceptors.response.use(interceptor2);
fireRequestCatchAndExpect(function () {
expect(interceptor2).not.toHaveBeenCalled();
done();
});
});
it('then the following rejection-interceptor is called', function (done) {
axios.interceptors.response.use(function() {
throw Error('throwing interceptor');
});
const unusedFulfillInterceptor = function() {};
const rejectIntercept = jasmine.createSpy('rejectIntercept');
axios.interceptors.response.use(unusedFulfillInterceptor, rejectIntercept);
fireRequestCatchAndExpect(function () {
expect(rejectIntercept).toHaveBeenCalled();
done();
});
});
it('once caught, another following fulfill-interceptor is called again (just like in a promise chain)', function (done) {
axios.interceptors.response.use(function() {
throw Error('throwing interceptor');
});
const unusedFulfillInterceptor = function() {};
const catchingThrowingInterceptor = function() {};
axios.interceptors.response.use(unusedFulfillInterceptor, catchingThrowingInterceptor);
const interceptor3 = jasmine.createSpy('interceptor3');
axios.interceptors.response.use(interceptor3);
fireRequestCatchAndExpect(function () {
expect(interceptor3).toHaveBeenCalled();
done();
});
});
});
});
});
it('should allow removing interceptors', function (done) {
let response, intercept;
axios.interceptors.response.use(function (data) {
data.data = data.data + '1';
return data;
});
intercept = axios.interceptors.response.use(function (data) {
data.data = data.data + '2';
return data;
});
axios.interceptors.response.use(function (data) {
data.data = data.data + '3';
return data;
});
axios.interceptors.response.eject(intercept);
axios('/foo').then(function (data) {
response = data;
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 200,
responseText: 'OK'
});
setTimeout(function () {
expect(response.data).toBe('OK13');
done();
}, 100);
});
});
it('should remove async interceptor before making request and execute synchronously', function (done) {
let asyncFlag = false;
const asyncIntercept = axios.interceptors.request.use(function (config) {
config.headers.async = 'async it!';
return config;
}, null, { synchronous: false });
const syncIntercept = axios.interceptors.request.use(function (config) {
config.headers.sync = 'hello world';
expect(asyncFlag).toBe(false);
return config;
}, null, { synchronous: true });
axios.interceptors.request.eject(asyncIntercept);
axios('/foo')
asyncFlag = true
getAjaxRequest().then(function (request) {
expect(request.requestHeaders.async).toBeUndefined();
expect(request.requestHeaders.sync).toBe('hello world');
done()
});
});
it('should execute interceptors before transformers', function (done) {
axios.interceptors.request.use(function (config) {
config.data.baz = 'qux';
return config;
});
axios.post('/foo', {
foo: 'bar'
});
getAjaxRequest().then(function (request) {
expect(request.params).toEqual('{"foo":"bar","baz":"qux"}');
done();
});
});
it('should modify base URL in request interceptor', function (done) {
const instance = axios.create({
baseURL: 'http://test.com/'
});
instance.interceptors.request.use(function (config) {
config.baseURL = 'http://rebase.com/';
return config;
});
instance.get('/foo');
getAjaxRequest().then(function (request) {
expect(request.url).toBe('http://rebase.com/foo');
done();
});
});
it('should clear all request interceptors', function () {
const instance = axios.create({
baseURL: 'http://test.com/'
});
instance.interceptors.request.use(function (config) {
return config
});
instance.interceptors.request.clear();
expect(instance.interceptors.request.handlers.length).toBe(0);
});
it('should clear all response interceptors', function () {
const instance = axios.create({
baseURL: 'http://test.com/'
});
instance.interceptors.response.use(function (config) {
return config
});
instance.interceptors.response.clear();
expect(instance.interceptors.response.handlers.length).toBe(0);
});
});
JavaScript
1
https://gitee.com/mirrors/axios.git
git@gitee.com:mirrors/axios.git
mirrors
axios
axios
v1.x

搜索帮助