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

Commit 6e4955a

Browse files
amMattMtbosch
authored andcommitted
fix($http): don't run transformData on HEAD methods
7b6c1d0 created this issue by using `Content-Type` to determine when to run `fromJson`. Because `HEAD` methods do not contain a body but are supposed to return the `Content-Type` header that would have been returned if it was a `GET` this functionality fails. Closes #9528 Closes #9529
1 parent 9db70d3 commit 6e4955a

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/ng/http.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -770,9 +770,12 @@ function $HttpProvider() {
770770

771771
function transformResponse(response) {
772772
// make a copy since the response must be cacheable
773-
var resp = extend({}, response, {
774-
data: transformData(response.data, response.headers, config.transformResponse)
775-
});
773+
var resp = extend({}, response);
774+
if(config.method === 'HEAD'){
775+
resp.data = response.data;
776+
} else {
777+
resp.data = transformData(response.data, response.headers, config.transformResponse);
778+
}
776779
return (isSuccess(response.status))
777780
? resp
778781
: $q.reject(resp);

test/ng/httpSpec.js

+23
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,17 @@ describe('$http', function() {
10741074
});
10751075

10761076

1077+
it('should deserialize json empty string when response header contains application/json',
1078+
function() {
1079+
$httpBackend.expect('GET', '/url').respond('""', {'Content-Type': 'application/json'});
1080+
$http({method: 'GET', url: '/url'}).success(callback);
1081+
$httpBackend.flush();
1082+
1083+
expect(callback).toHaveBeenCalledOnce();
1084+
expect(callback.mostRecentCall.args[0]).toEqual('');
1085+
});
1086+
1087+
10771088
it('should deserialize json with security prefix', function() {
10781089
$httpBackend.expect('GET', '/url').respond(')]}\',\n[1, "abc", {"foo":"bar"}]');
10791090
$http({method: 'GET', url: '/url'}).success(callback);
@@ -1094,6 +1105,18 @@ describe('$http', function() {
10941105
});
10951106

10961107

1108+
it('should not attempt to deserialize json when HEAD request', function(){
1109+
//per http spec for Content-Type, HEAD request should return a Content-Type header
1110+
//set to what the content type would have been if a get was sent
1111+
$httpBackend.expect('HEAD', '/url').respond('', {'Content-Type': 'application/json'});
1112+
$http({method: 'HEAD', url: '/url'}).success(callback);
1113+
$httpBackend.flush();
1114+
1115+
expect(callback).toHaveBeenCalledOnce();
1116+
expect(callback.mostRecentCall.args[0]).toEqual('');
1117+
});
1118+
1119+
10971120
it('should not deserialize tpl beginning with ng expression', function() {
10981121
$httpBackend.expect('GET', '/url').respond('{{some}}');
10991122
$http.get('/url').success(callback);

0 commit comments

Comments
 (0)