Permalink
Browse files

Ajax: improve content-type detection

Cherry-picked from 239169b
Fixes gh-2584
Closes gh-2643
  • Loading branch information...
markelog committed Oct 12, 2015
1 parent 2a83417 commit 3ced5abe5c7e4c90f845012859108059b03770c0
Showing with 112 additions and 5 deletions.
  1. +3 −3 src/ajax.js
  2. +1 −1 src/ajax/script.js
  3. +5 −0 test/data/ajax/content-type.php
  4. +103 −1 test/unit/ajax.js
View
@@ -322,9 +322,9 @@ jQuery.extend( {
},
contents: {
- xml: /xml/,
- html: /html/,
- json: /json/
+ xml: /\bxml\b/,
+ html: /\bhtml/,
+ json: /\bjson\b/
},
responseFields: {
View
@@ -18,7 +18,7 @@ jQuery.ajaxSetup( {
"application/ecmascript, application/x-ecmascript"
},
contents: {
- script: /(?:java|ecma)script/
+ script: /\b(?:java|ecma)script\b/
},
converters: {
"text script": function( text ) {
@@ -0,0 +1,5 @@
+<?php
+ $type = $_REQUEST['content-type'];
+ header("Content-type: $type");
+ echo $_REQUEST['response']
+?>
View
@@ -1630,7 +1630,109 @@ QUnit.module( "ajax", {
}
);
-// //----------- jQuery.ajaxPrefilter()
+ ajaxTest( "gh-2587 - when content-type not xml, but looks like one", 1, function( assert ) {
+ return {
+ url: url( "data/ajax/content-type.php" ),
+ data: {
+ "content-type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
+ "response": "<test/>"
+ },
+ success: function( result ) {
+ assert.strictEqual(
+ typeof result,
+ "string",
+ "Should handle it as a string, not xml"
+ );
+ }
+ };
+ } );
+
+ ajaxTest( "gh-2587 - when content-type not xml, but looks like one", 1, function( assert ) {
+ return {
+ url: url( "data/ajax/content-type.php" ),
+ data: {
+ "content-type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
+ "response": "<test/>"
+ },
+ success: function( result ) {
+ assert.strictEqual(
+ typeof result,
+ "string",
+ "Should handle it as a string, not xml"
+ );
+ }
+ };
+ } );
+
+ ajaxTest( "gh-2587 - when content-type not json, but looks like one", 1, function( assert ) {
+ return {
+ url: url( "data/ajax/content-type.php" ),
+ data: {
+ "content-type": "test/jsontest",
+ "response": JSON.stringify({test: "test"})
+ },
+ success: function( result ) {
+ assert.strictEqual(
+ typeof result,
+ "string",
+ "Should handle it as a string, not json"
+ );
+ }
+ };
+ } );
+
+ ajaxTest( "gh-2587 - when content-type not html, but looks like one", 1, function( assert ) {
+ return {
+ url: url( "data/ajax/content-type.php" ),
+ data: {
+ "content-type": "test/htmltest",
+ "response": "<p>test</p>"
+ },
+ success: function( result ) {
+ assert.strictEqual(
+ typeof result,
+ "string",
+ "Should handle it as a string, not html"
+ );
+ }
+ };
+ } );
+
+ ajaxTest( "gh-2587 - when content-type not javascript, but looks like one", 1, function( assert ) {
+ return {
+ url: url( "data/ajax/content-type.php" ),
+ data: {
+ "content-type": "test/testjavascript",
+ "response": "alert(1)"
+ },
+ success: function( result ) {
+ assert.strictEqual(
+ typeof result,
+ "string",
+ "Should handle it as a string, not javascript"
+ );
+ }
+ };
+ } );
+
+ ajaxTest( "gh-2587 - when content-type not ecmascript, but looks like one", 1, function( assert ) {
+ return {
+ url: url( "data/ajax/content-type.php" ),
+ data: {
+ "content-type": "test/testjavascript",
+ "response": "alert(1)"
+ },
+ success: function( result ) {
+ assert.strictEqual(
+ typeof result,
+ "string",
+ "Should handle it as a string, not ecmascript"
+ );
+ }
+ };
+ } );
+
+//----------- jQuery.ajaxPrefilter()
ajaxTest( "jQuery.ajaxPrefilter() - abort", 1, function( assert ) {
return {

0 comments on commit 3ced5ab

Please sign in to comment.