From 1af563d43e74cb7be53e815b66fd91dd93986ed6 Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Mon, 16 Mar 2015 12:36:53 +0000 Subject: [PATCH] fix($http): throw error if `success` and `error` methods do not receive a function Closes #11330 Closes #11333 --- src/ng/http.js | 4 ++++ test/ng/httpSpec.js | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) 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'); + }); }); });