Fix #12751. Ensure parseJson throws in the same situations as JSON.parse. Close gh-993.

This commit is contained in:
James Huston
2012-10-16 14:51:54 -04:00
committed by Dave Methvin
parent c31539c8a2
commit ee9687d441
4 changed files with 90 additions and 19 deletions

View File

@@ -2759,4 +2759,18 @@ if ( jQuery.ajax && ( !isLocal || hasPHP ) ) {
start();
});
});
test( "jQuery.ajax - empty json gets to error callback instead of success callback.", function() {
expect( 1 );
stop();
jQuery.ajax( url("data/echoData.php"), {
error: function( _, __, error ) {
equal( typeof error === "object", true, "Didn't get back error object for empty json response" );
start();
},
dataType: "json"
});
});
}

View File

@@ -1150,9 +1150,13 @@ test("jQuery.parseHTML", function() {
test("jQuery.parseJSON", function(){
expect(8);
equal( jQuery.parseJSON(), null, "Nothing in, null out." );
equal( jQuery.parseJSON( null ), null, "Nothing in, null out." );
equal( jQuery.parseJSON( "" ), null, "Nothing in, null out." );
raises(function() {
jQuery.parseJSON();
}, null, "parseJson now matches JSON.parse for empty input." );
equal(jQuery.parseJSON( null ), null, "parseJson now matches JSON.parse on null input." );
raises( function() {
jQuery.parseJSON( "" );
}, null, "parseJson now matches JSON.parse for empty strings." );
deepEqual( jQuery.parseJSON("{}"), {}, "Plain object parsing." );
deepEqual( jQuery.parseJSON("{\"test\":1}"), {"test":1}, "Plain object parsing." );
@@ -1346,3 +1350,50 @@ test("jQuery.camelCase()", function() {
equal( jQuery.camelCase( key ), val, "Converts: " + key + " => " + val );
});
});
test( "JQuery.parseJSON() test internal parseJson (using fallback) to make sure that it throws like JSON.parse", function() {
expect( 10 );
var jsonParse = window.JSON;
window.JSON = null;
raises(function() {
jsonParse.parse("''");
});
raises(function() {
jQuery.parseJSON("''");
});
raises(function() {
jsonParse.parse("");
});
raises(function() {
jQuery.parseJSON("");
});
raises(function() {
jsonParse.parse({});
});
raises(function() {
jQuery.parseJSON({});
});
var parsedValue = jsonParse.parse(null);
equal( parsedValue, null );
parsedValue = jQuery.parseJSON(null);
equal( parsedValue, null );
parsedValue = jsonParse.parse("{}");
equal( (typeof parsedValue === "object"), true );
parsedValue = jQuery.parseJSON("{}");
equal( (typeof parsedValue === "object"), true );
window.JSON = jsonParse;
} );