From ec3622eb3d5fd52ad29ed14512e83784a40d5b54 Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Wed, 30 Sep 2015 16:56:48 +0300 Subject: [PATCH] fix($http): apply `transformResponse` when `data` is empty Note, that (as a by-product of the previous implementation) only non-empty data was passed through the `transformResponse` pipeline. This is no longer the case. When using a custom `transformResponse` function, one should make sure it can also handle an empty (i.e. falsy) `data` argument appropriatelly. Fixes #12976 --- src/ng/http.js | 7 ++----- test/ng/httpSpec.js | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/ng/http.js b/src/ng/http.js index 12e2fd3020b6..0148515294bd 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -999,11 +999,8 @@ function $HttpProvider() { function transformResponse(response) { // make a copy since the response must be cacheable var resp = extend({}, response); - if (!response.data) { - resp.data = response.data; - } else { - resp.data = transformData(response.data, response.headers, response.status, config.transformResponse); - } + resp.data = transformData(response.data, response.headers, response.status, + config.transformResponse); return (isSuccess(response.status)) ? resp : $q.reject(resp); diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index 168305eab34f..7942dd9d567e 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -1391,6 +1391,25 @@ describe('$http', function() { expect(callback).toHaveBeenCalledOnce(); expect(callback.mostRecentCall.args[0]).toBe('RESP-FIRST:V1'); }); + + + it('should apply `transformResponse` even if the response data is empty', function(data) { + var callback = jasmine.createSpy('transformResponse'); + var config = {transformResponse: callback}; + + $httpBackend.expect('GET', '/url1').respond(200, undefined); + $httpBackend.expect('GET', '/url2').respond(200, null); + $httpBackend.expect('GET', '/url3').respond(200, ''); + $http.get('/url1', config); + $http.get('/url2', config); + $http.get('/url3', config); + $httpBackend.flush(); + + expect(callback.callCount).toBe(3); + expect(callback.calls[0].args[0]).toBe(undefined); + expect(callback.calls[1].args[0]).toBe(null); + expect(callback.calls[2].args[0]).toBe(''); + }); }); });