mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
Fixing #537 Rejecting promise if request is cancelled by the browser
This commit is contained in:
Vendored
+2
-2
@@ -1,4 +1,3 @@
|
||||
/* axios v0.18.0 | (c) 2018 by Matt Zabriskie */
|
||||
(function webpackUniversalModuleDefinition(root, factory) {
|
||||
if(typeof exports === 'object' && typeof module === 'object')
|
||||
module.exports = factory();
|
||||
@@ -506,7 +505,7 @@ return /******/ (function(modules) { // webpackBootstrap
|
||||
}, arguments[1]);
|
||||
}
|
||||
|
||||
config = utils.merge(defaults, {method: 'get'}, this.defaults, config);
|
||||
config = utils.merge(defaults, this.defaults, config);
|
||||
config.method = config.method.toLowerCase();
|
||||
|
||||
// Hook up interceptors middleware
|
||||
@@ -585,6 +584,7 @@ return /******/ (function(modules) { // webpackBootstrap
|
||||
}
|
||||
|
||||
var defaults = {
|
||||
method: 'get',
|
||||
adapter: getDefaultAdapter(),
|
||||
|
||||
transformRequest: [function transformRequest(data, headers) {
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-2
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -80,6 +80,18 @@ module.exports = function xhrAdapter(config) {
|
||||
request = null;
|
||||
};
|
||||
|
||||
// Handle browser request cancellation (as opposed to a manual cancellation)
|
||||
request.onabort = function handleAbort() {
|
||||
if (!request) {
|
||||
return;
|
||||
}
|
||||
|
||||
reject(createError('Request aborted', config, 'ECONNABORTED', request));
|
||||
|
||||
// Clean up request
|
||||
request = null;
|
||||
};
|
||||
|
||||
// Handle low level network errors
|
||||
request.onerror = function handleError() {
|
||||
// Real errors are hidden from us by the browser
|
||||
|
||||
@@ -79,6 +79,31 @@ describe('requests', function () {
|
||||
.then(finish, finish);
|
||||
});
|
||||
|
||||
it('should reject on abort', function (done) {
|
||||
var resolveSpy = jasmine.createSpy('resolve');
|
||||
var rejectSpy = jasmine.createSpy('reject');
|
||||
|
||||
var finish = function () {
|
||||
expect(resolveSpy).not.toHaveBeenCalled();
|
||||
expect(rejectSpy).toHaveBeenCalled();
|
||||
var reason = rejectSpy.calls.first().args[0];
|
||||
expect(reason instanceof Error).toBe(true);
|
||||
expect(reason.config.method).toBe('get');
|
||||
expect(reason.config.url).toBe('/foo');
|
||||
expect(reason.request).toEqual(jasmine.any(XMLHttpRequest));
|
||||
|
||||
done();
|
||||
};
|
||||
|
||||
axios('/foo')
|
||||
.then(resolveSpy, rejectSpy)
|
||||
.then(finish, finish);
|
||||
|
||||
getAjaxRequest().then(function (request) {
|
||||
request.abort();
|
||||
});
|
||||
});
|
||||
|
||||
it('should reject when validateStatus returns false', function (done) {
|
||||
var resolveSpy = jasmine.createSpy('resolve');
|
||||
var rejectSpy = jasmine.createSpy('reject');
|
||||
|
||||
Reference in New Issue
Block a user