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

Commit 4fc7346

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 b8736e6 commit 4fc7346

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
@@ -574,8 +574,17 @@ angular.module('ngResource', ['ng']).
574574
undefined;
575575

576576
forEach(action, function(value, key) {
577-
if (key != 'params' && key != 'isArray' && key != 'interceptor') {
578-
httpConfig[key] = copy(value);
577+
switch (key) {
578+
default:
579+
httpConfig[key] = copy(value);
580+
break;
581+
case 'params':
582+
case 'isArray':
583+
case 'interceptor':
584+
break;
585+
case 'timeout':
586+
httpConfig[key] = value;
587+
break;
579588
}
580589
});
581590

test/ngResource/resourceSpec.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ describe("resource", function() {
13161316
});
13171317

13181318
describe('resource', function() {
1319-
var $httpBackend, $resource;
1319+
var $httpBackend, $resource, $q;
13201320

13211321
beforeEach(module(function($exceptionHandlerProvider) {
13221322
$exceptionHandlerProvider.mode('log');
@@ -1327,6 +1327,7 @@ describe('resource', function() {
13271327
beforeEach(inject(function($injector) {
13281328
$httpBackend = $injector.get('$httpBackend');
13291329
$resource = $injector.get('$resource');
1330+
$q = $injector.get('$q');
13301331
}));
13311332

13321333

@@ -1364,5 +1365,34 @@ describe('resource', function() {
13641365
);
13651366
});
13661367

1368+
it('should cancel the request if timeout promise is resolved', function() {
1369+
var canceler = $q.defer();
1370+
1371+
$httpBackend.when('GET', '/CreditCard').respond({data: '123'});
1372+
1373+
var CreditCard = $resource('/CreditCard', {}, {
1374+
query: {
1375+
method: 'GET',
1376+
timeout: canceler.promise
1377+
}
1378+
});
1379+
1380+
CreditCard.query();
1381+
1382+
canceler.resolve();
1383+
expect($httpBackend.flush).toThrow(new Error("No pending request to flush !"));
1384+
1385+
canceler = $q.defer();
1386+
CreditCard = $resource('/CreditCard', {}, {
1387+
query: {
1388+
method: 'GET',
1389+
timeout: canceler.promise
1390+
}
1391+
});
1392+
1393+
CreditCard.query();
1394+
expect($httpBackend.flush).not.toThrow();
1395+
});
1396+
13671397

13681398
});

0 commit comments

Comments
 (0)