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

Commit 7c0731e

Browse files
gkalpakpetebacondarwin
authored andcommitted
fix($http): apply transformResponse even 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 appropriately. Fixes #12976 Closes #12979
1 parent 8fe781f commit 7c0731e

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/ng/http.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -999,11 +999,8 @@ function $HttpProvider() {
999999
function transformResponse(response) {
10001000
// make a copy since the response must be cacheable
10011001
var resp = extend({}, response);
1002-
if (!response.data) {
1003-
resp.data = response.data;
1004-
} else {
1005-
resp.data = transformData(response.data, response.headers, response.status, config.transformResponse);
1006-
}
1002+
resp.data = transformData(response.data, response.headers, response.status,
1003+
config.transformResponse);
10071004
return (isSuccess(response.status))
10081005
? resp
10091006
: $q.reject(resp);

test/ng/httpSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,25 @@ describe('$http', function() {
13911391
expect(callback).toHaveBeenCalledOnce();
13921392
expect(callback.mostRecentCall.args[0]).toBe('RESP-FIRST:V1');
13931393
});
1394+
1395+
1396+
it('should apply `transformResponse` even if the response data is empty', function(data) {
1397+
var callback = jasmine.createSpy('transformResponse');
1398+
var config = {transformResponse: callback};
1399+
1400+
$httpBackend.expect('GET', '/url1').respond(200, undefined);
1401+
$httpBackend.expect('GET', '/url2').respond(200, null);
1402+
$httpBackend.expect('GET', '/url3').respond(200, '');
1403+
$http.get('/url1', config);
1404+
$http.get('/url2', config);
1405+
$http.get('/url3', config);
1406+
$httpBackend.flush();
1407+
1408+
expect(callback.callCount).toBe(3);
1409+
expect(callback.calls[0].args[0]).toBe(undefined);
1410+
expect(callback.calls[1].args[0]).toBe(null);
1411+
expect(callback.calls[2].args[0]).toBe('');
1412+
});
13941413
});
13951414
});
13961415

0 commit comments

Comments
 (0)