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

Commit

Permalink
fix($resource): allow XHR request to be cancelled via timeout promise
Browse files Browse the repository at this point in the history
Closes #12657
Closes #12675
Closes #10890
Closes #9332
  • Loading branch information
netman92 authored and petebacondarwin committed Oct 28, 2015
1 parent b8736e6 commit 4fc7346
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});

Expand Down
32 changes: 31 additions & 1 deletion test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@ describe("resource", function() {
});

describe('resource', function() {
var $httpBackend, $resource;
var $httpBackend, $resource, $q;

beforeEach(module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
Expand All @@ -1327,6 +1327,7 @@ describe('resource', function() {
beforeEach(inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
$resource = $injector.get('$resource');
$q = $injector.get('$q');
}));


Expand Down Expand Up @@ -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();
});


});

0 comments on commit 4fc7346

Please sign in to comment.