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

Commit

Permalink
Browse files Browse the repository at this point in the history
fix($http): throw error if success and error methods do not recei…
…ve a function

Closes #11330
Closes #11333
  • Loading branch information
petebacondarwin committed Mar 17, 2015
1 parent 634e467 commit 731e1f6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ng/http.js
Expand Up @@ -806,13 +806,17 @@ function $HttpProvider() {
}

promise.success = function(fn) {
assertArgFn(fn, 'fn');

promise.then(function(response) {
fn(response.data, response.status, response.headers, config);
});
return promise;
};

promise.error = function(fn) {
assertArgFn(fn, 'fn');

promise.then(null, function(response) {
fn(response.data, response.status, response.headers, config);
});
Expand Down
56 changes: 56 additions & 0 deletions test/ng/httpSpec.js
Expand Up @@ -432,6 +432,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');
});
});


Expand All @@ -456,6 +484,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');
});
});
});

Expand Down

0 comments on commit 731e1f6

Please sign in to comment.