Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 7170f9d

Browse files
netman92petebacondarwin
authored andcommitted
fix($resource): allow XHR request to be cancelled via timeout promise
Closes #12657 Closes #12675 Closes #10890 Closes #9332
1 parent 1c0f721 commit 7170f9d

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/ngResource/resource.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,17 @@ angular.module('ngResource', ['ng']).
568568
undefined;
569569

570570
forEach(action, function(value, key) {
571-
if (key != 'params' && key != 'isArray' && key != 'interceptor') {
572-
httpConfig[key] = copy(value);
571+
switch (key) {
572+
default:
573+
httpConfig[key] = copy(value);
574+
break;
575+
case 'params':
576+
case 'isArray':
577+
case 'interceptor':
578+
break;
579+
case 'timeout':
580+
httpConfig[key] = value;
581+
break;
573582
}
574583
});
575584

test/ngResource/resourceSpec.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ describe("resource", function() {
13081308
});
13091309

13101310
describe('resource', function() {
1311-
var $httpBackend, $resource;
1311+
var $httpBackend, $resource, $q;
13121312

13131313
beforeEach(module(function($exceptionHandlerProvider) {
13141314
$exceptionHandlerProvider.mode('log');
@@ -1319,6 +1319,7 @@ describe('resource', function() {
13191319
beforeEach(inject(function($injector) {
13201320
$httpBackend = $injector.get('$httpBackend');
13211321
$resource = $injector.get('$resource');
1322+
$q = $injector.get('$q');
13221323
}));
13231324

13241325

@@ -1356,5 +1357,34 @@ describe('resource', function() {
13561357
);
13571358
});
13581359

1360+
it('should cancel the request if timeout promise is resolved', function() {
1361+
var canceler = $q.defer();
1362+
1363+
$httpBackend.when('GET', '/CreditCard').respond({data: '123'});
1364+
1365+
var CreditCard = $resource('/CreditCard', {}, {
1366+
query: {
1367+
method: 'GET',
1368+
timeout: canceler.promise
1369+
}
1370+
});
1371+
1372+
CreditCard.query();
1373+
1374+
canceler.resolve();
1375+
expect($httpBackend.flush).toThrow(new Error("No pending request to flush !"));
1376+
1377+
canceler = $q.defer();
1378+
CreditCard = $resource('/CreditCard', {}, {
1379+
query: {
1380+
method: 'GET',
1381+
timeout: canceler.promise
1382+
}
1383+
});
1384+
1385+
CreditCard.query();
1386+
expect($httpBackend.flush).not.toThrow();
1387+
});
1388+
13591389

13601390
});

0 commit comments

Comments
 (0)