]> source.dussan.org Git - jquery.git/commitdiff
Ajax: improve content-type detection
authorOleg Gaidarenko <markelog@gmail.com>
Mon, 12 Oct 2015 16:56:46 +0000 (19:56 +0300)
committerOleg Gaidarenko <markelog@gmail.com>
Mon, 12 Oct 2015 19:38:59 +0000 (22:38 +0300)
Cherry-picked from 239169bb2ede6ea6287d82d1d13b0c354f451749
Fixes gh-2584
Closes gh-2643

src/ajax.js
src/ajax/script.js
test/data/ajax/content-type.php [new file with mode: 0644]
test/unit/ajax.js

index b3cb54d39de2945a701d4378b01b28edf4e87be3..409e3dc43eb114179116042229fc2cb4b44115f4 100644 (file)
@@ -322,9 +322,9 @@ jQuery.extend( {
                },
 
                contents: {
-                       xml: /xml/,
-                       html: /html/,
-                       json: /json/
+                       xml: /\bxml\b/,
+                       html: /\bhtml/,
+                       json: /\bjson\b/
                },
 
                responseFields: {
index 1bd7c37b30bd49683ca0e44e7c0ecb16be194b8a..1d372872ba8a6e745c07a9362a0026d7d2c7a3c3 100644 (file)
@@ -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 ) {
diff --git a/test/data/ajax/content-type.php b/test/data/ajax/content-type.php
new file mode 100644 (file)
index 0000000..162e363
--- /dev/null
@@ -0,0 +1,5 @@
+<?php
+       $type = $_REQUEST['content-type'];
+       header("Content-type: $type");
+       echo $_REQUEST['response']
+?>
index 6c78618f42ac58113910c71a9dccb41864d8ef83..62d06c9b4fff77025ce264bc2cd050f68b8c8e30 100644 (file)
@@ -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 {