Skip to content

Commit 4c28d87

Browse files
gkalpakpetebacondarwin
authored andcommitted
feat($resource): pass status/statusText to success callbacks
Fixes angular#8341 Closes angular#8841 PR (angular#14836)
1 parent 28bf011 commit 4c28d87

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

src/ngResource/resource.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ function shallowClearAndCopy(src, dst) {
239239
* - non-GET instance actions: `instance.$action([parameters], [success], [error])`
240240
*
241241
*
242-
* Success callback is called with (value, responseHeaders) arguments, where the value is
243-
* the populated resource instance or collection object. The error callback is called
244-
* with (httpResponse) argument.
242+
* Success callback is called with (value (Object|Array), responseHeaders (Function),
243+
* status (number), statusText (string)) arguments, where the value is the populated resource
244+
* instance or collection object. The error callback is called with (httpResponse) argument.
245245
*
246246
* Class actions return empty instance (with additional properties below).
247247
* Instance actions return promise of the action.
@@ -812,7 +812,7 @@ angular.module('ngResource', ['ng']).
812812
promise = promise.then(
813813
function(response) {
814814
var value = responseInterceptor(response);
815-
(success || noop)(value, response.headers);
815+
(success || noop)(value, response.headers, response.status, response.statusText);
816816
return value;
817817
},
818818
responseErrorInterceptor);

test/ngResource/resourceSpec.js

+59
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,65 @@ describe('basic usage', function() {
11031103
});
11041104

11051105

1106+
describe('success mode', function() {
1107+
it('should call the success callback (as 1st argument) on 2xx responses', function() {
1108+
var instance, headers, status, statusText;
1109+
var successCb = jasmine.createSpy('successCb').and.callFake(function(d, h, s, t) {
1110+
expect(d).toBe(instance);
1111+
expect(h()).toEqual(jasmine.objectContaining(headers));
1112+
expect(s).toBe(status);
1113+
expect(t).toBe(statusText);
1114+
});
1115+
1116+
instance = CreditCard.get(successCb);
1117+
headers = {foo: 'bar'};
1118+
status = 200;
1119+
statusText = 'OK';
1120+
$httpBackend.expect('GET', '/CreditCard').respond(status, {}, headers, statusText);
1121+
$httpBackend.flush();
1122+
1123+
expect(successCb).toHaveBeenCalledOnce();
1124+
1125+
instance = CreditCard.get(successCb);
1126+
headers = {baz: 'qux'};
1127+
status = 299;
1128+
statusText = 'KO';
1129+
$httpBackend.expect('GET', '/CreditCard').respond(status, {}, headers, statusText);
1130+
$httpBackend.flush();
1131+
1132+
expect(successCb).toHaveBeenCalledTimes(2);
1133+
});
1134+
1135+
1136+
it('should call the success callback (as 2nd argument) on 2xx responses', function() {
1137+
var instance, headers, status, statusText;
1138+
var successCb = jasmine.createSpy('successCb').and.callFake(function(d, h, s, t) {
1139+
expect(d).toBe(instance);
1140+
expect(h()).toEqual(jasmine.objectContaining(headers));
1141+
expect(s).toBe(status);
1142+
expect(t).toBe(statusText);
1143+
});
1144+
1145+
instance = CreditCard.get({id: 123}, successCb);
1146+
headers = {foo: 'bar'};
1147+
status = 200;
1148+
statusText = 'OK';
1149+
$httpBackend.expect('GET', '/CreditCard/123').respond(status, {}, headers, statusText);
1150+
$httpBackend.flush();
1151+
1152+
expect(successCb).toHaveBeenCalledOnce();
1153+
1154+
instance = CreditCard.get({id: 456}, successCb);
1155+
headers = {baz: 'qux'};
1156+
status = 299;
1157+
statusText = 'KO';
1158+
$httpBackend.expect('GET', '/CreditCard/456').respond(status, {}, headers, statusText);
1159+
$httpBackend.flush();
1160+
1161+
expect(successCb).toHaveBeenCalledTimes(2);
1162+
});
1163+
});
1164+
11061165
describe('failure mode', function() {
11071166
var ERROR_CODE = 500,
11081167
ERROR_RESPONSE = 'Server Error',

0 commit comments

Comments
 (0)