@@ -806,6 +806,121 @@ QUnit.module( "ajax", {
806806 } ;
807807 } ) ;
808808
809+ ajaxTest ( "jQuery.ajax() - do not execute scripts from unsuccessful responses (gh-4250)" , 11 , function ( assert ) {
810+ var globalEval = jQuery . globalEval ;
811+
812+ var failConverters = {
813+ "text script" : function ( ) {
814+ assert . ok ( false , "No converter for unsuccessful response" ) ;
815+ }
816+ } ;
817+
818+ function request ( title , options ) {
819+ var testMsg = title + ": expected file missing status" ;
820+ return jQuery . extend ( {
821+ beforeSend : function ( ) {
822+ jQuery . globalEval = function ( ) {
823+ assert . ok ( false , "Should not eval" ) ;
824+ } ;
825+ } ,
826+ complete : function ( ) {
827+ jQuery . globalEval = globalEval ;
828+ } ,
829+ // error is the significant assertion
830+ error : function ( xhr ) {
831+ assert . strictEqual ( xhr . status , 404 , testMsg ) ;
832+ } ,
833+ success : function ( ) {
834+ assert . ok ( false , "Unanticipated success" ) ;
835+ }
836+ } , options ) ;
837+ }
838+
839+ return [
840+ request (
841+ "HTML reply" ,
842+ {
843+ url : url ( "404.txt" )
844+ }
845+ ) ,
846+ request (
847+ "HTML reply with dataType" ,
848+ {
849+ dataType : "script" ,
850+ url : url ( "404.txt" )
851+ }
852+ ) ,
853+ request (
854+ "script reply" ,
855+ {
856+ url : url ( "mock.php?action=errorWithScript&withScriptContentType" )
857+ }
858+ ) ,
859+ request (
860+ "non-script reply" ,
861+ {
862+ url : url ( "mock.php?action=errorWithScript" )
863+ }
864+ ) ,
865+ request (
866+ "script reply with dataType" ,
867+ {
868+ dataType : "script" ,
869+ url : url ( "mock.php?action=errorWithScript&withScriptContentType" )
870+ }
871+ ) ,
872+ request (
873+ "non-script reply with dataType" ,
874+ {
875+ dataType : "script" ,
876+ url : url ( "mock.php?action=errorWithScript" )
877+ }
878+ ) ,
879+ request (
880+ "script reply with converter" ,
881+ {
882+ converters : failConverters ,
883+ url : url ( "mock.php?action=errorWithScript&withScriptContentType" )
884+ }
885+ ) ,
886+ request (
887+ "non-script reply with converter" ,
888+ {
889+ converters : failConverters ,
890+ url : url ( "mock.php?action=errorWithScript" )
891+ }
892+ ) ,
893+ request (
894+ "script reply with converter and dataType" ,
895+ {
896+ converters : failConverters ,
897+ dataType : "script" ,
898+ url : url ( "mock.php?action=errorWithScript&withScriptContentType" )
899+ }
900+ ) ,
901+ request (
902+ "non-script reply with converter and dataType" ,
903+ {
904+ converters : failConverters ,
905+ dataType : "script" ,
906+ url : url ( "mock.php?action=errorWithScript" )
907+ }
908+ ) ,
909+ request (
910+ "JSONP reply with dataType" ,
911+ {
912+ dataType : "jsonp" ,
913+ url : url ( "mock.php?action=errorWithScript" ) ,
914+ beforeSend : function ( ) {
915+ jQuery . globalEval = function ( response ) {
916+ assert . ok ( / " s t a t u s " : 4 0 4 , " m s g " : " N o t F o u n d " / . test ( response ) , "Error object returned" ) ;
917+ } ;
918+ }
919+ }
920+ )
921+ ] ;
922+ } ) ;
923+
809924 ajaxTest ( "jQuery.ajax() - synchronous request" , 1 , function ( assert ) {
810925 return {
811926 url : url ( "json_obj.js" ) ,
0 commit comments