Skip to content

Commit 0c56950

Browse files
committed
feat($resource): pass status/statusText to success callbacks
Fixes angular#8341 Closes angular#8841
1 parent 3cda897 commit 0c56950

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

src/ngResource/resource.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ function shallowClearAndCopy(src, dst) {
238238
* - non-GET instance actions: `instance.$action([parameters], [success], [error])`
239239
*
240240
*
241-
* Success callback is called with (value, responseHeaders) arguments, where the value is
242-
* the populated resource instance or collection object. The error callback is called
243-
* with (httpResponse) argument.
241+
* Success callback is called with (value (Object|Array), responseHeaders (Function),
242+
* status (number), statusText (string)) arguments, where the value is the populated resource
243+
* instance or collection object. The error callback is called with (httpResponse) argument.
244244
*
245245
* Class actions return empty instance (with additional properties below).
246246
* Instance actions return promise of the action.
@@ -773,7 +773,7 @@ angular.module('ngResource', ['ng']).
773773
promise = promise.then(
774774
function(response) {
775775
var value = responseInterceptor(response);
776-
(success || noop)(value, response.headers);
776+
(success || noop)(value, response.headers, response.status, response.statusText);
777777
return value;
778778
},
779779
responseErrorInterceptor || error ?

test/ngResource/resourceSpec.js

+60
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,7 @@ describe("basic usage", function() {
10241024
});
10251025
});
10261026

1027+
10271028
it('should allow per action response interceptor that gets full response', function() {
10281029
CreditCard = $resource('/CreditCard', {}, {
10291030
query: {
@@ -1082,6 +1083,65 @@ describe("basic usage", function() {
10821083
});
10831084

10841085

1086+
describe('success mode', function() {
1087+
it('should call the success callback (as 1st argument) on 2xx responses', function() {
1088+
var instance, headers, status, statusText;
1089+
var successCb = jasmine.createSpy('successCb').and.callFake(function(d, h, s, t) {
1090+
expect(d).toBe(instance);
1091+
expect(h()).toEqual(jasmine.objectContaining(headers));
1092+
expect(s).toBe(status);
1093+
expect(t).toBe(statusText);
1094+
});
1095+
1096+
instance = CreditCard.get(successCb);
1097+
headers = {foo: 'bar'};
1098+
status = 200;
1099+
statusText = 'OK';
1100+
$httpBackend.expect('GET', '/CreditCard').respond(status, {}, headers, statusText);
1101+
$httpBackend.flush();
1102+
1103+
expect(successCb).toHaveBeenCalledOnce();
1104+
1105+
instance = CreditCard.get(successCb);
1106+
headers = {baz: 'qux'};
1107+
status = 299;
1108+
statusText = 'KO';
1109+
$httpBackend.expect('GET', '/CreditCard').respond(status, {}, headers, statusText);
1110+
$httpBackend.flush();
1111+
1112+
expect(successCb).toHaveBeenCalledTimes(2);
1113+
});
1114+
1115+
1116+
it('should call the success callback (as 2nd argument) on 2xx responses', function() {
1117+
var instance, headers, status, statusText;
1118+
var successCb = jasmine.createSpy('successCb').and.callFake(function(d, h, s, t) {
1119+
expect(d).toBe(instance);
1120+
expect(h()).toEqual(jasmine.objectContaining(headers));
1121+
expect(s).toBe(status);
1122+
expect(t).toBe(statusText);
1123+
});
1124+
1125+
instance = CreditCard.get({id: 123}, successCb);
1126+
headers = {foo: 'bar'};
1127+
status = 200;
1128+
statusText = 'OK';
1129+
$httpBackend.expect('GET', '/CreditCard/123').respond(status, {}, headers, statusText);
1130+
$httpBackend.flush();
1131+
1132+
expect(successCb).toHaveBeenCalledOnce();
1133+
1134+
instance = CreditCard.get({id: 456}, successCb);
1135+
headers = {baz: 'qux'};
1136+
status = 299;
1137+
statusText = 'KO';
1138+
$httpBackend.expect('GET', '/CreditCard/456').respond(status, {}, headers, statusText);
1139+
$httpBackend.flush();
1140+
1141+
expect(successCb).toHaveBeenCalledTimes(2);
1142+
});
1143+
});
1144+
10851145
describe('failure mode', function() {
10861146
var ERROR_CODE = 500,
10871147
ERROR_RESPONSE = 'Server Error',

0 commit comments

Comments
 (0)