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

feat($resource): pass status/statusText to success callbacks #14836

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
@@ -238,9 +238,9 @@ function shallowClearAndCopy(src, dst) {
* - non-GET instance actions: `instance.$action([parameters], [success], [error])`
*
*
* Success callback is called with (value, responseHeaders) arguments, where the value is
* the populated resource instance or collection object. The error callback is called
* with (httpResponse) argument.
* Success callback is called with (value (Object|Array), responseHeaders (Function),
* status (number), statusText (string)) arguments, where the value is the populated resource
* instance or collection object. The error callback is called with (httpResponse) argument.
*
* Class actions return empty instance (with additional properties below).
* Instance actions return promise of the action.
@@ -773,7 +773,7 @@ angular.module('ngResource', ['ng']).
promise = promise.then(
function(response) {
var value = responseInterceptor(response);
(success || noop)(value, response.headers);
(success || noop)(value, response.headers, response.status, response.statusText);
return value;
},
responseErrorInterceptor || error ?
60 changes: 60 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
@@ -1024,6 +1024,7 @@ describe("basic usage", function() {
});
});


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


describe('success mode', function() {
it('should call the success callback (as 1st argument) on 2xx responses', function() {
var instance, headers, status, statusText;
var successCb = jasmine.createSpy('successCb').and.callFake(function(d, h, s, t) {
expect(d).toBe(instance);
expect(h()).toEqual(jasmine.objectContaining(headers));
expect(s).toBe(status);
expect(t).toBe(statusText);
});

instance = CreditCard.get(successCb);
headers = {foo: 'bar'};
status = 200;
statusText = 'OK';
$httpBackend.expect('GET', '/CreditCard').respond(status, {}, headers, statusText);
$httpBackend.flush();

expect(successCb).toHaveBeenCalledOnce();

instance = CreditCard.get(successCb);
headers = {baz: 'qux'};
status = 299;
statusText = 'KO';
$httpBackend.expect('GET', '/CreditCard').respond(status, {}, headers, statusText);
$httpBackend.flush();

expect(successCb).toHaveBeenCalledTimes(2);
});


it('should call the success callback (as 2nd argument) on 2xx responses', function() {
var instance, headers, status, statusText;
var successCb = jasmine.createSpy('successCb').and.callFake(function(d, h, s, t) {
expect(d).toBe(instance);
expect(h()).toEqual(jasmine.objectContaining(headers));
expect(s).toBe(status);
expect(t).toBe(statusText);
});

instance = CreditCard.get({id: 123}, successCb);
headers = {foo: 'bar'};
status = 200;
statusText = 'OK';
$httpBackend.expect('GET', '/CreditCard/123').respond(status, {}, headers, statusText);
$httpBackend.flush();

expect(successCb).toHaveBeenCalledOnce();

instance = CreditCard.get({id: 456}, successCb);
headers = {baz: 'qux'};
status = 299;
statusText = 'KO';
$httpBackend.expect('GET', '/CreditCard/456').respond(status, {}, headers, statusText);
$httpBackend.flush();

expect(successCb).toHaveBeenCalledTimes(2);
});
});

describe('failure mode', function() {
var ERROR_CODE = 500,
ERROR_RESPONSE = 'Server Error',