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

Commit 6f19a6f

Browse files
gamplemanpkozlowski-opensource
authored andcommittedNov 15, 2014
fix($http): don't parse single space responses as JSON
Closes #9907
1 parent f30163e commit 6f19a6f

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed
 

‎src/ng/http.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function defaultHttpResponseTransform(data, headers) {
1111
// strip json vulnerability protection prefix
1212
data = data.replace(JSON_PROTECTION_PREFIX, '');
1313
var contentType = headers('Content-Type');
14-
if ((contentType && contentType.indexOf(APPLICATION_JSON) === 0) ||
14+
if ((contentType && contentType.indexOf(APPLICATION_JSON) === 0 && data.trim()) ||
1515
(JSON_START.test(data) && JSON_END.test(data))) {
1616
data = fromJson(data);
1717
}

‎test/ng/httpSpec.js

+11
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,17 @@ describe('$http', function() {
11331133
expect(callback.mostRecentCall.args[0]).toEqual('');
11341134
});
11351135

1136+
it('should not attempt to deserialize json for a blank response whose header contains application/json', function() {
1137+
//per http spec for Content-Type, HEAD request should return a Content-Type header
1138+
//set to what the content type would have been if a get was sent
1139+
$httpBackend.expect('GET', '/url').respond(' ', {'Content-Type': 'application/json'});
1140+
$http({method: 'GET', url: '/url'}).success(callback);
1141+
$httpBackend.flush();
1142+
1143+
expect(callback).toHaveBeenCalledOnce();
1144+
expect(callback.mostRecentCall.args[0]).toEqual(' ');
1145+
});
1146+
11361147
it('should not deserialize tpl beginning with ng expression', function() {
11371148
$httpBackend.expect('GET', '/url').respond('{{some}}');
11381149
$http.get('/url').success(callback);

0 commit comments

Comments
 (0)
This repository has been archived.