diff --git a/src/ng/http.js b/src/ng/http.js index 95a33ae7a1fb..3a2e06c7474d 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -812,6 +812,8 @@ function $HttpProvider() { } promise.success = function(fn) { + assertArgFn(fn, 'fn'); + promise.then(function(response) { fn(response.data, response.status, response.headers, config); }); @@ -819,6 +821,8 @@ function $HttpProvider() { }; promise.error = function(fn) { + assertArgFn(fn, 'fn'); + promise.then(null, function(response) { fn(response.data, response.status, response.headers, config); }); diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index dbb6290323ae..a858c83c64bc 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -434,6 +434,34 @@ describe('$http', function() { var httpPromise = $http({url: '/url', method: 'GET'}); expect(httpPromise.success(callback)).toBe(httpPromise); }); + + + it('should error if the callback is not a function', function() { + expect(function() { + $http({url: '/url', method: 'GET'}).success(); + }).toThrowMinErr('ng', 'areq'); + + expect(function() { + $http({url: '/url', method: 'GET'}).success(undefined); + }).toThrowMinErr('ng', 'areq'); + + expect(function() { + $http({url: '/url', method: 'GET'}).success(null); + }).toThrowMinErr('ng', 'areq'); + + + expect(function() { + $http({url: '/url', method: 'GET'}).success({}); + }).toThrowMinErr('ng', 'areq'); + + expect(function() { + $http({url: '/url', method: 'GET'}).success([]); + }).toThrowMinErr('ng', 'areq'); + + expect(function() { + $http({url: '/url', method: 'GET'}).success('error'); + }).toThrowMinErr('ng', 'areq'); + }); }); @@ -458,6 +486,34 @@ describe('$http', function() { var httpPromise = $http({url: '/url', method: 'GET'}); expect(httpPromise.error(callback)).toBe(httpPromise); }); + + + it('should error if the callback is not a function', function() { + expect(function() { + $http({url: '/url', method: 'GET'}).error(); + }).toThrowMinErr('ng', 'areq'); + + expect(function() { + $http({url: '/url', method: 'GET'}).error(undefined); + }).toThrowMinErr('ng', 'areq'); + + expect(function() { + $http({url: '/url', method: 'GET'}).error(null); + }).toThrowMinErr('ng', 'areq'); + + + expect(function() { + $http({url: '/url', method: 'GET'}).error({}); + }).toThrowMinErr('ng', 'areq'); + + expect(function() { + $http({url: '/url', method: 'GET'}).error([]); + }).toThrowMinErr('ng', 'areq'); + + expect(function() { + $http({url: '/url', method: 'GET'}).error('error'); + }).toThrowMinErr('ng', 'areq'); + }); }); });