Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue#2609 | Sasha | predictable axios requests (#2702)
* issue#2609 | Sasha | predictable axios requests - axios requests are not delayed by pre-emptive promise creation by default - add options to interceptors api ("synchronous" and "runWhen") - add documentation and unit tests * issue#2609 | Sasha | pull request feedback changes * issue#2609 | Sasha | additional feedback changes * issue#2609 | Sasha | put back try/catch * issue#2609 | Sasha | add 2 adapter unit tests - remove check for requestCancelled Co-authored-by: ak71845 <alexandre.korotkov@kroger.com> Co-authored-by: Xianming Zhong <chinesedfan@qq.com> Co-authored-by: Jay <jasonsaayman@gmail.com>
- Loading branch information
1 parent
535d0c4
commit 62d6256
Showing
5 changed files
with
337 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,98 @@ | ||
var axios = require('../../index'); | ||
|
||
describe('adapter', function () { | ||
beforeEach(function () { | ||
jasmine.Ajax.install(); | ||
}); | ||
|
||
afterEach(function () { | ||
jasmine.Ajax.uninstall(); | ||
}); | ||
|
||
it('should support custom adapter', function (done) { | ||
var called = false; | ||
axios('/foo', { | ||
adapter: function barAdapter(config) { | ||
return new Promise(function dispatchXhrRequest(resolve) { | ||
var request = new XMLHttpRequest(); | ||
request.open('GET', '/bar'); | ||
|
||
request.onreadystatechange = function () { | ||
resolve({ | ||
config: config, | ||
request: request | ||
}); | ||
}; | ||
|
||
request.send(null); | ||
}); | ||
} | ||
}).catch(console.log); | ||
|
||
getAjaxRequest().then(function(request) { | ||
expect(request.url).toBe('/bar'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should execute adapter code synchronously', function (done) { | ||
var asyncFlag = false; | ||
axios('/foo', { | ||
adapter: function (config) { | ||
called = true; | ||
adapter: function barAdapter(config) { | ||
return new Promise(function dispatchXhrRequest(resolve) { | ||
var request = new XMLHttpRequest(); | ||
request.open('GET', '/bar'); | ||
|
||
request.onreadystatechange = function () { | ||
resolve({ | ||
config: config, | ||
request: request | ||
}); | ||
}; | ||
|
||
expect(asyncFlag).toBe(false); | ||
request.send(null); | ||
}); | ||
} | ||
}).catch(console.log); | ||
|
||
asyncFlag = true; | ||
|
||
getAjaxRequest().then(function() { | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should execute adapter code asynchronously when interceptor is present', function (done) { | ||
var asyncFlag = false; | ||
|
||
axios.interceptors.request.use(function (config) { | ||
config.headers.async = 'async it!'; | ||
return config; | ||
}); | ||
|
||
axios('/foo', { | ||
adapter: function barAdapter(config) { | ||
return new Promise(function dispatchXhrRequest(resolve) { | ||
var request = new XMLHttpRequest(); | ||
request.open('GET', '/bar'); | ||
|
||
request.onreadystatechange = function () { | ||
resolve({ | ||
config: config, | ||
request: request | ||
}); | ||
}; | ||
|
||
expect(asyncFlag).toBe(true); | ||
request.send(null); | ||
}); | ||
} | ||
}).catch(console.log); | ||
|
||
setTimeout(function () { | ||
expect(called).toBe(true); | ||
asyncFlag = true; | ||
|
||
getAjaxRequest().then(function() { | ||
done(); | ||
}, 100); | ||
}); | ||
}); | ||
}); | ||
|
Oops, something went wrong.