]> 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:15 +0000 (22:38 +0300)
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 157934ef1b838c23945758e2b781022eba33c3fd..efc4136505dd404f94a738d0ad304593b86c7c34 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 3978ba46cd31fb26ec1de159480d02241be2ba7a..485ba397be6d8bbdf2a29b8cde3c93d22ab6c2fb 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 64795877369d9cb833458da0fd86b072e56413fb..ec3e076135f4937be29d9213ae365995c43b581f 100644 (file)
@@ -1794,7 +1794,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 {