diff options
author | Sean Robinson <sean.robinson@scottsdalecc.edu> | 2019-04-26 07:25:08 -0700 |
---|---|---|
committer | Michał Gołębiowski-Owczarek <m.goleb@gmail.com> | 2020-04-06 22:33:56 +0200 |
commit | da3dd85b63c4e3a6a768132c2a83a1a6eec24840 (patch) | |
tree | c941a51b75c6ab462589c50ad37b5bc23fb1f0d3 /test/unit | |
parent | 065143c2e93512eb0c82d1b344b71d06eb7cf01c (diff) | |
download | jquery-da3dd85b63c4e3a6a768132c2a83a1a6eec24840.tar.gz jquery-da3dd85b63c4e3a6a768132c2a83a1a6eec24840.zip |
Ajax: Do not execute scripts for unsuccessful HTTP responses
The script transport used to evaluate fetched script sources which is
undesirable for unsuccessful HTTP responses. This is different to other data
types where such a convention was fine (e.g. in case of JSON).
(cherry picked from 50871a5a85cc802421b40cc67e2830601968affe)
Fixes gh-4250
Fixes gh-4655
Closes gh-4379
Diffstat (limited to 'test/unit')
-rw-r--r-- | test/unit/ajax.js | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/test/unit/ajax.js b/test/unit/ajax.js index d9b984799..8b3850605 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -837,6 +837,121 @@ QUnit.module( "ajax", { }; } ); + ajaxTest( "jQuery.ajax() - do not execute scripts from unsuccessful responses (gh-4250)", 11, function( assert ) { + var globalEval = jQuery.globalEval; + + var failConverters = { + "text script": function() { + assert.ok( false, "No converter for unsuccessful response" ); + } + }; + + function request( title, options ) { + var testMsg = title + ": expected file missing status"; + return jQuery.extend( { + beforeSend: function() { + jQuery.globalEval = function() { + assert.ok( false, "Should not eval" ); + }; + }, + complete: function() { + jQuery.globalEval = globalEval; + }, + // error is the significant assertion + error: function( xhr ) { + assert.strictEqual( xhr.status, 404, testMsg ); + }, + success: function() { + assert.ok( false, "Unanticipated success" ); + } + }, options ); + } + + return [ + request( + "HTML reply", + { + url: url( "404.txt" ) + } + ), + request( + "HTML reply with dataType", + { + dataType: "script", + url: url( "404.txt" ) + } + ), + request( + "script reply", + { + url: url( "mock.php?action=errorWithScript&withScriptContentType" ) + } + ), + request( + "non-script reply", + { + url: url( "mock.php?action=errorWithScript" ) + } + ), + request( + "script reply with dataType", + { + dataType: "script", + url: url( "mock.php?action=errorWithScript&withScriptContentType" ) + } + ), + request( + "non-script reply with dataType", + { + dataType: "script", + url: url( "mock.php?action=errorWithScript" ) + } + ), + request( + "script reply with converter", + { + converters: failConverters, + url: url( "mock.php?action=errorWithScript&withScriptContentType" ) + } + ), + request( + "non-script reply with converter", + { + converters: failConverters, + url: url( "mock.php?action=errorWithScript" ) + } + ), + request( + "script reply with converter and dataType", + { + converters: failConverters, + dataType: "script", + url: url( "mock.php?action=errorWithScript&withScriptContentType" ) + } + ), + request( + "non-script reply with converter and dataType", + { + converters: failConverters, + dataType: "script", + url: url( "mock.php?action=errorWithScript" ) + } + ), + request( + "JSONP reply with dataType", + { + dataType: "jsonp", + url: url( "mock.php?action=errorWithScript" ), + beforeSend: function() { + jQuery.globalEval = function( response ) { + assert.ok( /"status": 404, "msg": "Not Found"/.test( response ), "Error object returned" ); + }; + } + } + ) + ]; + } ); + ajaxTest( "jQuery.ajax() - synchronous request", 1, function( assert ) { return { url: url( "json_obj.js" ), |