mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
fix: added a option to choose between legacy and the new request/response interceptor ordering
* test: add request interceptor tests for legacy and ordered execution * feat: add legacy interceptor request/response ordering option --------- Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -91,6 +91,63 @@ describe('interceptors', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should execute request interceptor in legacy order', function (done) {
|
||||
let sequence = '';
|
||||
axios.interceptors.request.use(function (config) {
|
||||
sequence += '1';
|
||||
return config;
|
||||
});
|
||||
|
||||
axios.interceptors.request.use(function (config) {
|
||||
sequence += '2';
|
||||
return config;
|
||||
});
|
||||
|
||||
axios.interceptors.request.use(function (config) {
|
||||
sequence += '3';
|
||||
return config;
|
||||
});
|
||||
|
||||
axios({
|
||||
url: '/foo',
|
||||
});
|
||||
|
||||
getAjaxRequest().then(function (request) {
|
||||
expect(sequence).toBe('321');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should execute request interceptor in order', function (done) {
|
||||
let sequence = '';
|
||||
axios.interceptors.request.use(function (config) {
|
||||
sequence += '1';
|
||||
return config;
|
||||
});
|
||||
|
||||
axios.interceptors.request.use(function (config) {
|
||||
sequence += '2';
|
||||
return config;
|
||||
});
|
||||
|
||||
axios.interceptors.request.use(function (config) {
|
||||
sequence += '3';
|
||||
return config;
|
||||
});
|
||||
|
||||
axios({
|
||||
url: '/foo',
|
||||
transitional: {
|
||||
legacyInterceptorReqResOrdering: false
|
||||
}
|
||||
});
|
||||
|
||||
getAjaxRequest().then(function (request) {
|
||||
expect(sequence).toBe('123');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('runs the interceptor if runWhen function is provided and resolves to true', function (done) {
|
||||
function onGetCall(config) {
|
||||
return config.method === 'get';
|
||||
@@ -343,7 +400,7 @@ describe('interceptors', function () {
|
||||
|
||||
function fireRequestAndExpect(expectation) {
|
||||
let response;
|
||||
axios('/foo').then(function(data) {
|
||||
axios('/foo').then(function (data) {
|
||||
response = data;
|
||||
});
|
||||
getAjaxRequest().then(function (request) {
|
||||
@@ -352,7 +409,7 @@ describe('interceptors', function () {
|
||||
responseText: 'OK'
|
||||
});
|
||||
|
||||
setTimeout(function() {
|
||||
setTimeout(function () {
|
||||
expectation(response)
|
||||
}, 100);
|
||||
});
|
||||
@@ -384,10 +441,10 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('then only the last interceptor\'s result is returned', function (done) {
|
||||
axios.interceptors.response.use(function() {
|
||||
axios.interceptors.response.use(function () {
|
||||
return 'response 1';
|
||||
});
|
||||
axios.interceptors.response.use(function() {
|
||||
axios.interceptors.response.use(function () {
|
||||
return 'response 2';
|
||||
});
|
||||
|
||||
@@ -398,10 +455,10 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('then every interceptor receives the result of it\'s predecessor', function (done) {
|
||||
axios.interceptors.response.use(function() {
|
||||
axios.interceptors.response.use(function () {
|
||||
return 'response 1';
|
||||
});
|
||||
axios.interceptors.response.use(function(response) {
|
||||
axios.interceptors.response.use(function (response) {
|
||||
return [response, 'response 2'];
|
||||
});
|
||||
|
||||
@@ -414,7 +471,7 @@ describe('interceptors', function () {
|
||||
describe('and when the fulfillment-interceptor throws', function () {
|
||||
|
||||
function fireRequestCatchAndExpect(expectation) {
|
||||
axios('/foo').catch(function(data) {
|
||||
axios('/foo').catch(function (data) {
|
||||
// dont handle result
|
||||
});
|
||||
getAjaxRequest().then(function (request) {
|
||||
@@ -423,14 +480,14 @@ describe('interceptors', function () {
|
||||
responseText: 'OK'
|
||||
});
|
||||
|
||||
setTimeout(function() {
|
||||
setTimeout(function () {
|
||||
expectation()
|
||||
}, 100);
|
||||
});
|
||||
}
|
||||
|
||||
it('then the following fulfillment-interceptor is not called', function (done) {
|
||||
axios.interceptors.response.use(function() {
|
||||
axios.interceptors.response.use(function () {
|
||||
throw Error('throwing interceptor');
|
||||
});
|
||||
const interceptor2 = jasmine.createSpy('interceptor2');
|
||||
@@ -443,10 +500,10 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('then the following rejection-interceptor is called', function (done) {
|
||||
axios.interceptors.response.use(function() {
|
||||
axios.interceptors.response.use(function () {
|
||||
throw Error('throwing interceptor');
|
||||
});
|
||||
const unusedFulfillInterceptor = function() {};
|
||||
const unusedFulfillInterceptor = function () { };
|
||||
const rejectIntercept = jasmine.createSpy('rejectIntercept');
|
||||
axios.interceptors.response.use(unusedFulfillInterceptor, rejectIntercept);
|
||||
|
||||
@@ -457,12 +514,12 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('once caught, another following fulfill-interceptor is called again (just like in a promise chain)', function (done) {
|
||||
axios.interceptors.response.use(function() {
|
||||
axios.interceptors.response.use(function () {
|
||||
throw Error('throwing interceptor');
|
||||
});
|
||||
|
||||
const unusedFulfillInterceptor = function() {};
|
||||
const catchingThrowingInterceptor = function() {};
|
||||
const unusedFulfillInterceptor = function () { };
|
||||
const catchingThrowingInterceptor = function () { };
|
||||
axios.interceptors.response.use(unusedFulfillInterceptor, catchingThrowingInterceptor);
|
||||
|
||||
const interceptor3 = jasmine.createSpy('interceptor3');
|
||||
|
||||
Reference in New Issue
Block a user