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

Commit

Permalink
fix(ngResource): canceling XHR request using promise
Browse files Browse the repository at this point in the history
  • Loading branch information
netman92 committed Aug 23, 2015
1 parent f95bc42 commit 2955bbf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,10 @@ angular.module('ngResource', ['ng']).
undefined;

forEach(action, function(value, key) {
if (key != 'params' && key != 'isArray' && key != 'interceptor') {
if (key != 'params' && key != 'isArray' && key != 'interceptor' && key != 'timeout') {
httpConfig[key] = copy(value);
} else if (key == 'timeout') {
httpConfig[key] = value;
}
});

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

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

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


Expand Down Expand Up @@ -1356,5 +1357,36 @@ describe('resource', function() {
);
});

it('If timeout promise is resolved, cancel the request', 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(function() { $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(function() { $httpBackend.flush();}).not.toThrow(new Error("No pending request to flush !"));


});


});

0 comments on commit 2955bbf

Please sign in to comment.