]> source.dussan.org Git - jquery.git/commitdiff
Ajax: Ensure ajaxSettings.traditional is still honored
authorDave Methvin <dave.methvin@gmail.com>
Tue, 26 Apr 2016 14:28:02 +0000 (10:28 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Wed, 27 Apr 2016 13:06:43 +0000 (09:06 -0400)
Fixes gh-3023
Closes gh-3081

Since .param() no longer looks at this setting we need unit tests
to ensure it is still honored by $.ajax().

test/unit/ajax.js
test/unit/serialize.js

index 1082ce88ef9b0a6e06aec36f990eae9c8cf814d0..1a4ff7ee4df51b337e5bd1fdd4f9dbdb84983c70 100644 (file)
@@ -373,6 +373,64 @@ QUnit.module( "ajax", {
                ];
        } );
 
+       ajaxTest( "jQuery.ajax() - traditional param encoding", 4, function( assert ) {
+               return [
+                       {
+                               url: "/",
+                               traditional: true,
+                               data: {
+                                       "devo": "hat",
+                                       "answer": 42,
+                                       "quux": "a space"
+                               },
+                               beforeSend: function( xhr, settings ) {
+                                       assert.equal( settings.url, "/?devo=hat&answer=42&quux=a%20space", "Simple case" );
+                                       return false;
+                               },
+                               error: true
+                       },
+                       {
+                               url: "/",
+                               traditional: true,
+                               data: {
+                                       "a": [ 1, 2, 3 ],
+                                       "b[]": [ "b1", "b2" ]
+                               },
+                               beforeSend: function( xhr, settings ) {
+                                       assert.equal( settings.url, "/?a=1&a=2&a=3&b%5B%5D=b1&b%5B%5D=b2", "Arrays" );
+                                       return false;
+                               },
+                               error: true
+                       },
+                       {
+                               url: "/",
+                               traditional: true,
+                               data: {
+                                       "a": [ [ 1, 2 ], [ 3, 4 ], 5 ]
+                               },
+                               beforeSend: function( xhr, settings ) {
+                                       assert.equal( settings.url, "/?a=1%2C2&a=3%2C4&a=5", "Nested arrays" );
+                                       return false;
+                               },
+                               error: true
+                       },
+                       {
+                               url: "/",
+                               traditional: true,
+                               data: {
+                                       "a": [ "w", [ [ "x", "y" ], "z" ] ]
+                               },
+                               cache: false,
+                               beforeSend: function( xhr, settings ) {
+                                       var url = settings.url.replace( /\d{3,}/, "" );
+                                       assert.equal( url, "/?a=w&a=x%2Cy%2Cz&_=", "Cache-buster" );
+                                       return false;
+                               },
+                               error: true
+                       }
+               ];
+       } );
+
        ajaxTest( "jQuery.ajax() - cross-domain detection", 8, function( assert ) {
                function request( url, title, crossDomainOrOptions ) {
                        return jQuery.extend( {
index 519b7a713ebc60c2cf917c52b820fe2d6574b0c3..4c141637301e877400a03e9c2400380fa8050556 100644 (file)
@@ -1,7 +1,7 @@
 QUnit.module( "serialize", { teardown: moduleTeardown } );
 
 QUnit.test( "jQuery.param()", function( assert ) {
-       assert.expect( 24 );
+       assert.expect( 23 );
 
        var params;
 
@@ -56,7 +56,7 @@ QUnit.test( "jQuery.param()", function( assert ) {
        assert.equal( jQuery.param( params, true ), "a=1&a=2&b=%5Bobject%20Object%5D&i=10&i=11&j=true&k=false&l=&l=0&m=cowboy%20hat%3F", "huge structure" );
 
        params = { "a": [ 0, [ 1, 2 ], [ 3, [ 4, 5 ], [ 6 ] ], { "b": [ 7, [ 8, 9 ], [ { "c": 10, d: 11 } ], [ [ 12 ] ], [ [ [ 13 ] ] ], { "e": { "f": { "g": [ 14, [ 15 ] ] } } }, 16 ] }, 17 ] };
-       assert.equal( jQuery.param( params, true ), "a=0&a=1%2C2&a=3%2C4%2C5%2C6&a=%5Bobject%20Object%5D&a=17", "nested arrays (not possible when jQuery.param.traditional == true)" );
+       assert.equal( jQuery.param( params, true ), "a=0&a=1%2C2&a=3%2C4%2C5%2C6&a=%5Bobject%20Object%5D&a=17", "nested arrays (not possible when traditional == true)" );
 
        params = { a:[ 1,2 ], b:{ c:3, d:[ 4,5 ], e:{ x:[ 6 ], y:7, z:[ 8,9 ] }, f:true, g:false, h:undefined }, i:[ 10,11 ], j:true, k:false, l:[ undefined,0 ], m:"cowboy hat?" };
        assert.equal( decodeURIComponent( jQuery.param( params ) ), "a[]=1&a[]=2&b[c]=3&b[d][]=4&b[d][]=5&b[e][x][]=6&b[e][y]=7&b[e][z][]=8&b[e][z][]=9&b[f]=true&b[g]=false&b[h]=&i[]=10&i[]=11&j=true&k=false&l[]=&l[]=0&m=cowboy hat?", "huge structure, forced not traditional" );
@@ -74,6 +74,19 @@ QUnit.test( "jQuery.param()", function( assert ) {
        assert.equal( jQuery.param( params ), "test%5B%5D=1&test%5B%5D=2&test%5B%5D=", "object with array property with null value" );
 } );
 
+QUnit.test( "jQuery.param() not affected by ajaxSettings", function( assert ) {
+       assert.expect( 1 );
+
+       var oldTraditional = jQuery.ajaxSettings.traditional;
+       jQuery.ajaxSettings.traditional = true;
+       assert.equal(
+               jQuery.param( { "foo": [ "a", "b", "c" ] } ),
+               "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c",
+               "ajaxSettings.traditional is ignored"
+       );
+       jQuery.ajaxSettings.traditional = oldTraditional;
+} );
+
 QUnit.test( "jQuery.param() Constructed prop values", function( assert ) {
        assert.expect( 4 );