Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit e3a378e

Browse files
gkalpakNarretz
authored andcommitted
feat($resource): pass status/statusText to success callbacks
Fixes #8341 Closes #8841 PR (#14836)
1 parent c75698d commit e3a378e

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
@@ -242,9 +242,9 @@ function shallowClearAndCopy(src, dst) {
242242
* - non-GET instance actions: `instance.$action([parameters], [success], [error])`
243243
*
244244
*
245-
* Success callback is called with (value, responseHeaders) arguments, where the value is
246-
* the populated resource instance or collection object. The error callback is called
247-
* with (httpResponse) argument.
245+
* Success callback is called with (value (Object|Array), responseHeaders (Function),
246+
* status (number), statusText (string)) arguments, where the value is the populated resource
247+
* instance or collection object. The error callback is called with (httpResponse) argument.
248248
*
249249
* Class actions return empty instance (with additional properties below).
250250
* Instance actions return promise of the action.
@@ -777,7 +777,7 @@ angular.module('ngResource', ['ng']).
777777
promise = promise.then(
778778
function(response) {
779779
var value = responseInterceptor(response);
780-
(success || noop)(value, response.headers);
780+
(success || noop)(value, response.headers, response.status, response.statusText);
781781
return value;
782782
},
783783
(hasError || hasResponseErrorInterceptor) ?

test/ngResource/resourceSpec.js

+59
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,65 @@ describe('basic usage', function() {
11241124
});
11251125

11261126

1127+
describe('success mode', function() {
1128+
it('should call the success callback (as 1st argument) on 2xx responses', function() {
1129+
var instance, headers, status, statusText;
1130+
var successCb = jasmine.createSpy('successCb').and.callFake(function(d, h, s, t) {
1131+
expect(d).toBe(instance);
1132+
expect(h()).toEqual(jasmine.objectContaining(headers));
1133+
expect(s).toBe(status);
1134+
expect(t).toBe(statusText);
1135+
});
1136+
1137+
instance = CreditCard.get(successCb);
1138+
headers = {foo: 'bar'};
1139+
status = 200;
1140+
statusText = 'OK';
1141+
$httpBackend.expect('GET', '/CreditCard').respond(status, {}, headers, statusText);
1142+
$httpBackend.flush();
1143+
1144+
expect(successCb).toHaveBeenCalledOnce();
1145+
1146+
instance = CreditCard.get(successCb);
1147+
headers = {baz: 'qux'};
1148+
status = 299;
1149+
statusText = 'KO';
1150+
$httpBackend.expect('GET', '/CreditCard').respond(status, {}, headers, statusText);
1151+
$httpBackend.flush();
1152+
1153+
expect(successCb).toHaveBeenCalledTimes(2);
1154+
});
1155+
1156+
1157+
it('should call the success callback (as 2nd argument) on 2xx responses', function() {
1158+
var instance, headers, status, statusText;
1159+
var successCb = jasmine.createSpy('successCb').and.callFake(function(d, h, s, t) {
1160+
expect(d).toBe(instance);
1161+
expect(h()).toEqual(jasmine.objectContaining(headers));
1162+
expect(s).toBe(status);
1163+
expect(t).toBe(statusText);
1164+
});
1165+
1166+
instance = CreditCard.get({id: 123}, successCb);
1167+
headers = {foo: 'bar'};
1168+
status = 200;
1169+
statusText = 'OK';
1170+
$httpBackend.expect('GET', '/CreditCard/123').respond(status, {}, headers, statusText);
1171+
$httpBackend.flush();
1172+
1173+
expect(successCb).toHaveBeenCalledOnce();
1174+
1175+
instance = CreditCard.get({id: 456}, successCb);
1176+
headers = {baz: 'qux'};
1177+
status = 299;
1178+
statusText = 'KO';
1179+
$httpBackend.expect('GET', '/CreditCard/456').respond(status, {}, headers, statusText);
1180+
$httpBackend.flush();
1181+
1182+
expect(successCb).toHaveBeenCalledTimes(2);
1183+
});
1184+
});
1185+
11271186
describe('failure mode', function() {
11281187
var ERROR_CODE = 500,
11291188
ERROR_RESPONSE = 'Server Error',

0 commit comments

Comments
 (0)