Skip to content

Commit f1261b8

Browse files
petebacondarwinnetman92
authored andcommitted
fix($http): throw error if success and error methods do not receive a function
Closes angular#11330 Closes angular#11333
1 parent 500e3f9 commit f1261b8

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/ng/http.js

+4
Original file line numberDiff line numberDiff line change
@@ -812,13 +812,17 @@ function $HttpProvider() {
812812
}
813813

814814
promise.success = function(fn) {
815+
assertArgFn(fn, 'fn');
816+
815817
promise.then(function(response) {
816818
fn(response.data, response.status, response.headers, config);
817819
});
818820
return promise;
819821
};
820822

821823
promise.error = function(fn) {
824+
assertArgFn(fn, 'fn');
825+
822826
promise.then(null, function(response) {
823827
fn(response.data, response.status, response.headers, config);
824828
});

test/ng/httpSpec.js

+56
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,34 @@ describe('$http', function() {
434434
var httpPromise = $http({url: '/url', method: 'GET'});
435435
expect(httpPromise.success(callback)).toBe(httpPromise);
436436
});
437+
438+
439+
it('should error if the callback is not a function', function() {
440+
expect(function() {
441+
$http({url: '/url', method: 'GET'}).success();
442+
}).toThrowMinErr('ng', 'areq');
443+
444+
expect(function() {
445+
$http({url: '/url', method: 'GET'}).success(undefined);
446+
}).toThrowMinErr('ng', 'areq');
447+
448+
expect(function() {
449+
$http({url: '/url', method: 'GET'}).success(null);
450+
}).toThrowMinErr('ng', 'areq');
451+
452+
453+
expect(function() {
454+
$http({url: '/url', method: 'GET'}).success({});
455+
}).toThrowMinErr('ng', 'areq');
456+
457+
expect(function() {
458+
$http({url: '/url', method: 'GET'}).success([]);
459+
}).toThrowMinErr('ng', 'areq');
460+
461+
expect(function() {
462+
$http({url: '/url', method: 'GET'}).success('error');
463+
}).toThrowMinErr('ng', 'areq');
464+
});
437465
});
438466

439467

@@ -458,6 +486,34 @@ describe('$http', function() {
458486
var httpPromise = $http({url: '/url', method: 'GET'});
459487
expect(httpPromise.error(callback)).toBe(httpPromise);
460488
});
489+
490+
491+
it('should error if the callback is not a function', function() {
492+
expect(function() {
493+
$http({url: '/url', method: 'GET'}).error();
494+
}).toThrowMinErr('ng', 'areq');
495+
496+
expect(function() {
497+
$http({url: '/url', method: 'GET'}).error(undefined);
498+
}).toThrowMinErr('ng', 'areq');
499+
500+
expect(function() {
501+
$http({url: '/url', method: 'GET'}).error(null);
502+
}).toThrowMinErr('ng', 'areq');
503+
504+
505+
expect(function() {
506+
$http({url: '/url', method: 'GET'}).error({});
507+
}).toThrowMinErr('ng', 'areq');
508+
509+
expect(function() {
510+
$http({url: '/url', method: 'GET'}).error([]);
511+
}).toThrowMinErr('ng', 'areq');
512+
513+
expect(function() {
514+
$http({url: '/url', method: 'GET'}).error('error');
515+
}).toThrowMinErr('ng', 'areq');
516+
});
461517
});
462518
});
463519

0 commit comments

Comments
 (0)