From 0c5695039031ec59be872d9e3e6b5502141057d1 Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Tue, 28 Jun 2016 12:43:27 +0300 Subject: [PATCH] feat($resource): pass `status`/`statusText` to success callbacks Fixes #8341 Closes #8841 --- src/ngResource/resource.js | 8 ++--- test/ngResource/resourceSpec.js | 60 +++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index 31a594421e50..e8cc593140f5 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -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 ? diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index a848f06d1244..75a24ed22704 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -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',