fix($http): allow empty json response

When a response contains an `application/json` header and
the response is empty the response should be left as is.

Fixes #9532
Closes #9562
This commit is contained in:
Tobias Bosch
2014-10-10 12:10:20 -07:00
parent 44746332fa
commit 9ba24c54d6
2 changed files with 11 additions and 1 deletions

View File

@@ -771,7 +771,7 @@ function $HttpProvider() {
function transformResponse(response) {
// make a copy since the response must be cacheable
var resp = extend({}, response);
if(config.method === 'HEAD'){
if (!response.data) {
resp.data = response.data;
} else {
resp.data = transformData(response.data, response.headers, config.transformResponse);

View File

@@ -1116,6 +1116,16 @@ describe('$http', function() {
expect(callback.mostRecentCall.args[0]).toEqual('');
});
it('should not attempt to deserialize json for an empty response whose header contains application/json', 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('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 not deserialize tpl beginning with ng expression', function() {
$httpBackend.expect('GET', '/url').respond('{{some}}');