From df2051cf59d054d7ee91bdb554b583471942c6b4 Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Tue, 26 Apr 2016 10:28:02 -0400 Subject: [PATCH] Ajax: Ensure ajaxSettings.traditional is still honored 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 | 58 ++++++++++++++++++++++++++++++++++++++++++ test/unit/serialize.js | 17 +++++++++++-- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/test/unit/ajax.js b/test/unit/ajax.js index 1082ce88e..1a4ff7ee4 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -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( { diff --git a/test/unit/serialize.js b/test/unit/serialize.js index 519b7a713..4c1416373 100644 --- a/test/unit/serialize.js +++ b/test/unit/serialize.js @@ -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 ); -- 2.39.5