From f25ca33d5e18cd4d9c6559d1d0522a495dd66de8 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 (for non-HEAD requests) Fixes #12976 --- src/ng/http.js | 2 +- test/ng/httpSpec.js | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/ng/http.js b/src/ng/http.js index 12e2fd3020b6..e53ea29948a1 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -999,7 +999,7 @@ function $HttpProvider() { function transformResponse(response) { // make a copy since the response must be cacheable var resp = extend({}, response); - if (!response.data) { + if (config.method === 'HEAD') { resp.data = response.data; } else { resp.data = transformData(response.data, response.headers, response.status, config.transformResponse); diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index 168305eab34f..151a118d161f 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -1391,6 +1391,31 @@ describe('$http', function() { expect(callback).toHaveBeenCalledOnce(); expect(callback.mostRecentCall.args[0]).toBe('RESP-FIRST:V1'); }); + + + it('should not apply `transformResponse` on `HEAD` requests', function() { + var transformResponse = jasmine.createSpy('transformResponse'); + + $httpBackend.expect('HEAD', '/url').respond(200, 'Hello, world !'); + $http.head('/url', {transformResponse: transformResponse}); + $httpBackend.flush(); + + expect(transformResponse).not.toHaveBeenCalled(); + }); + + + it('should apply `transformResponse` on non-`HEAD` requests with empty response', + function() { + var transformResponse = jasmine.createSpy('transformResponse'); + + $httpBackend.expect('GET', '/url').respond(200, null); + $http.get('/url', {transformResponse: transformResponse}); + $httpBackend.flush(); + + expect(transformResponse).toHaveBeenCalledOnce(); + expect(transformResponse.mostRecentCall.args[0]).toBe(null); + } + ); }); });