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

fix($http): don't run transformData on HEAD methods #9555

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,9 +755,12 @@ function $HttpProvider() {

function transformResponse(response) {
// make a copy since the response must be cacheable
var resp = extend({}, response, {
data: transformData(response.data, response.headers, config.transformResponse)
});
var resp = extend({}, response);
if(response.config.method.toLowerCase() === 'head'){
resp.data = response.data;
} else {
resp.data = transformData(response.data, response.headers, config.transformResponse);
}
return (isSuccess(response.status))
? resp
: $q.reject(resp);
Expand Down
23 changes: 23 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,17 @@ describe('$http', function() {
});


it('should deserialize json empty string when response header contains application/json',
function() {
$httpBackend.expect('GET', '/url').respond('""', {'Content-Type': 'application/json'});
$http({method: 'GET', url: '/url'}).success(callback);
$httpBackend.flush();

expect(callback).toHaveBeenCalledOnce();
expect(callback.mostRecentCall.args[0]).toEqual('');
});


it('should deserialize json with security prefix', function() {
$httpBackend.expect('GET', '/url').respond(')]}\',\n[1, "abc", {"foo":"bar"}]');
$http({method: 'GET', url: '/url'}).success(callback);
Expand All @@ -1094,6 +1105,18 @@ describe('$http', function() {
});


it('should not attempt to deserialize json when HEAD request', function(){
//per http spec for Content-Type, HEAD request should return a Content-Type header
//set to what the content type would have been if a get was sent
$httpBackend.expect('HEAD', '/url').respond('', {'Content-Type': 'application/json'});
$http({method: 'HEAD', url: '/url'}).success(callback);
$httpBackend.flush();

expect(callback).toHaveBeenCalledOnce();
expect(callback.mostRecentCall.args[0]).toEqual('');
});


it('should not deserialize tpl beginning with ng expression', function() {
$httpBackend.expect('GET', '/url').respond('{{some}}');
$http.get('/url').success(callback);
Expand Down