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

Commit 7fe6201

Browse files
committed
fix($resource): don't allow using promises as timeout and log a warning
Promises never worked correctly as values for `timeout` in `$resource`, because the same value has to be re-used for multiple requests (and it is not possible to `angular.copy()` a promise). Now (in addition to ignoring a non-numeric `timeout`), a warning is logged to the console using `$log.debug()`. Partly fixes #13393.
1 parent 96e7d80 commit 7fe6201

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/ngResource/resource.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ angular.module('ngResource', ['ng']).
367367
}
368368
};
369369

370-
this.$get = ['$http', '$q', function($http, $q) {
370+
this.$get = ['$http', '$log', '$q', function($http, $log, $q) {
371371

372372
var noop = angular.noop,
373373
forEach = angular.forEach,
@@ -578,6 +578,14 @@ angular.module('ngResource', ['ng']).
578578
case 'isArray':
579579
case 'interceptor':
580580
break;
581+
case 'timeout':
582+
if (value && !angular.isNumber(value)) {
583+
$log.debug('ngResource:\n' +
584+
' Only numeric values are allowed as `timeout`.\n' +
585+
' Promises are not supported in $resource, because the same value ' +
586+
'would be used for multiple requests.');
587+
}
588+
break;
581589
}
582590
});
583591

test/ngResource/resourceSpec.js

+40
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,46 @@ describe('resource', function() {
13551355
/^\[\$resource:badcfg\] Error in resource configuration for action `get`\. Expected response to contain an object but got an array \(Request: GET \/Customer\/123\)/
13561356
);
13571357
});
1358+
});
1359+
1360+
describe('resource with promises as `timeout`', function() {
1361+
var httpSpy;
1362+
var $httpBackend;
1363+
var $resource;
1364+
1365+
beforeEach(module('ngResource', function($provide) {
1366+
$provide.decorator('$http', function($delegate) {
1367+
httpSpy = jasmine.createSpy('$http').andCallFake($delegate);
1368+
return httpSpy;
1369+
});
1370+
}));
1371+
1372+
beforeEach(inject(function(_$httpBackend_, _$resource_) {
1373+
$httpBackend = _$httpBackend_;
1374+
$resource = _$resource_;
1375+
}));
13581376

1377+
it('should ignore non-numeric timeouts in actions and log a $debug message',
1378+
inject(function($log, $q) {
1379+
spyOn($log, 'debug');
1380+
$httpBackend.whenGET('/CreditCard').respond({});
1381+
1382+
var CreditCard = $resource('/CreditCard', {}, {
1383+
get: {
1384+
method: 'GET',
1385+
timeout: $q.defer().promise
1386+
}
1387+
});
1388+
1389+
CreditCard.get();
1390+
$httpBackend.flush();
13591391

1392+
expect(httpSpy).toHaveBeenCalledOnce();
1393+
expect(httpSpy.calls[0].args[0].timeout).toBeUndefined();
1394+
expect($log.debug).toHaveBeenCalledOnceWith('ngResource:\n' +
1395+
' Only numeric values are allowed as `timeout`.\n' +
1396+
' Promises are not supported in $resource, because the same value ' +
1397+
'would be used for multiple requests.');
1398+
})
1399+
);
13601400
});

0 commit comments

Comments
 (0)