From 4fc734665e5dddef26ed30a9d4f75632cd269481 Mon Sep 17 00:00:00 2001 From: Stanislav Komanec Date: Sun, 23 Aug 2015 20:06:17 +0200 Subject: [PATCH] fix($resource): allow XHR request to be cancelled via timeout promise Closes #12657 Closes #12675 Closes #10890 Closes #9332 --- src/ngResource/resource.js | 13 +++++++++++-- test/ngResource/resourceSpec.js | 32 +++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index b08341f1c88c..767d3ba7872c 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -574,8 +574,17 @@ angular.module('ngResource', ['ng']). undefined; forEach(action, function(value, key) { - if (key != 'params' && key != 'isArray' && key != 'interceptor') { - httpConfig[key] = copy(value); + switch (key) { + default: + httpConfig[key] = copy(value); + break; + case 'params': + case 'isArray': + case 'interceptor': + break; + case 'timeout': + httpConfig[key] = value; + break; } }); diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index fd26006d9bfc..41a90d2f1601 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -1316,7 +1316,7 @@ describe("resource", function() { }); describe('resource', function() { - var $httpBackend, $resource; + var $httpBackend, $resource, $q; beforeEach(module(function($exceptionHandlerProvider) { $exceptionHandlerProvider.mode('log'); @@ -1327,6 +1327,7 @@ describe('resource', function() { beforeEach(inject(function($injector) { $httpBackend = $injector.get('$httpBackend'); $resource = $injector.get('$resource'); + $q = $injector.get('$q'); })); @@ -1364,5 +1365,34 @@ describe('resource', function() { ); }); + it('should cancel the request if timeout promise is resolved', function() { + var canceler = $q.defer(); + + $httpBackend.when('GET', '/CreditCard').respond({data: '123'}); + + var CreditCard = $resource('/CreditCard', {}, { + query: { + method: 'GET', + timeout: canceler.promise + } + }); + + CreditCard.query(); + + canceler.resolve(); + expect($httpBackend.flush).toThrow(new Error("No pending request to flush !")); + + canceler = $q.defer(); + CreditCard = $resource('/CreditCard', {}, { + query: { + method: 'GET', + timeout: canceler.promise + } + }); + + CreditCard.query(); + expect($httpBackend.flush).not.toThrow(); + }); + });