From c6eb6e9b4ba5d6078e3580f22b7cdd6493f407ff Mon Sep 17 00:00:00 2001 From: Boris Serdyuk Date: Tue, 18 Mar 2014 16:04:32 +0400 Subject: [PATCH] feat($http): pass success flag to transformResponse --- src/ng/http.js | 20 ++++++++++---------- test/ng/httpSpec.js | 13 +++++++++++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/ng/http.js b/src/ng/http.js index 9fcb45782f87..3f03064facab 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -64,14 +64,15 @@ function headersGetter(headers) { * @param {*} data Data to transform. * @param {function(string=)} headers Http headers getter fn. * @param {(Function|Array.)} fns Function or an array of functions. + * @param {boolean} success True when this is response and it is success * @returns {*} Transformed data. */ -function transformData(data, headers, fns) { +function transformData(data, headers, fns, success) { if (isFunction(fns)) - return fns(data, headers); + return fns(data, headers, success); forEach(fns, function(fn) { - data = fn(data, headers); + data = fn(data, headers, success); }); return data; @@ -739,13 +740,12 @@ function $HttpProvider() { return promise; function transformResponse(response) { - // make a copy since the response must be cacheable - var resp = extend({}, response, { - data: transformData(response.data, response.headers, config.transformResponse) - }); - return (isSuccess(response.status)) - ? resp - : $q.reject(resp); + var success = isSuccess(response.status), + // make a copy since the response must be cacheable + resp = extend({}, response, { + data: transformData(response.data, response.headers, config.transformResponse, success) + }); + return success ? resp : $q.reject(resp); } function mergeHeaders(config) { diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index 86ab72ea8bd8..05c93decfe35 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -1097,6 +1097,19 @@ describe('$http', function() { expect(callback.mostRecentCall.args[0]).toBe('header1'); }); + it('should know when response is failed', function() { + $httpBackend.expect('GET', '/url').respond(404, 'Not found'); + $http.get('/url', { + transformResponse: function(data, headers, success) { + return success; + } + }).error(callback); + $httpBackend.flush(); + + expect(callback).toHaveBeenCalledOnce(); + expect(callback.mostRecentCall.args[0]).toBe(false); + }); + it('should pipeline more functions', function() { function first(d, h) {return d + '-first' + ':' + h('h1')}