Skip to content

Commit df2051c

Browse files
committed
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().
1 parent 4f27042 commit df2051c

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

test/unit/ajax.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,64 @@ QUnit.module( "ajax", {
373373
];
374374
} );
375375

376+
ajaxTest( "jQuery.ajax() - traditional param encoding", 4, function( assert ) {
377+
return [
378+
{
379+
url: "/",
380+
traditional: true,
381+
data: {
382+
"devo": "hat",
383+
"answer": 42,
384+
"quux": "a space"
385+
},
386+
beforeSend: function( xhr, settings ) {
387+
assert.equal( settings.url, "/?devo=hat&answer=42&quux=a%20space", "Simple case" );
388+
return false;
389+
},
390+
error: true
391+
},
392+
{
393+
url: "/",
394+
traditional: true,
395+
data: {
396+
"a": [ 1, 2, 3 ],
397+
"b[]": [ "b1", "b2" ]
398+
},
399+
beforeSend: function( xhr, settings ) {
400+
assert.equal( settings.url, "/?a=1&a=2&a=3&b%5B%5D=b1&b%5B%5D=b2", "Arrays" );
401+
return false;
402+
},
403+
error: true
404+
},
405+
{
406+
url: "/",
407+
traditional: true,
408+
data: {
409+
"a": [ [ 1, 2 ], [ 3, 4 ], 5 ]
410+
},
411+
beforeSend: function( xhr, settings ) {
412+
assert.equal( settings.url, "/?a=1%2C2&a=3%2C4&a=5", "Nested arrays" );
413+
return false;
414+
},
415+
error: true
416+
},
417+
{
418+
url: "/",
419+
traditional: true,
420+
data: {
421+
"a": [ "w", [ [ "x", "y" ], "z" ] ]
422+
},
423+
cache: false,
424+
beforeSend: function( xhr, settings ) {
425+
var url = settings.url.replace( /\d{3,}/, "" );
426+
assert.equal( url, "/?a=w&a=x%2Cy%2Cz&_=", "Cache-buster" );
427+
return false;
428+
},
429+
error: true
430+
}
431+
];
432+
} );
433+
376434
ajaxTest( "jQuery.ajax() - cross-domain detection", 8, function( assert ) {
377435
function request( url, title, crossDomainOrOptions ) {
378436
return jQuery.extend( {

test/unit/serialize.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
QUnit.module( "serialize", { teardown: moduleTeardown } );
22

33
QUnit.test( "jQuery.param()", function( assert ) {
4-
assert.expect( 24 );
4+
assert.expect( 23 );
55

66
var params;
77

@@ -56,7 +56,7 @@ QUnit.test( "jQuery.param()", function( assert ) {
5656
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" );
5757

5858
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 ] };
59-
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)" );
59+
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)" );
6060

6161
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?" };
6262
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 ) {
7474
assert.equal( jQuery.param( params ), "test%5B%5D=1&test%5B%5D=2&test%5B%5D=", "object with array property with null value" );
7575
} );
7676

77+
QUnit.test( "jQuery.param() not affected by ajaxSettings", function( assert ) {
78+
assert.expect( 1 );
79+
80+
var oldTraditional = jQuery.ajaxSettings.traditional;
81+
jQuery.ajaxSettings.traditional = true;
82+
assert.equal(
83+
jQuery.param( { "foo": [ "a", "b", "c" ] } ),
84+
"foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c",
85+
"ajaxSettings.traditional is ignored"
86+
);
87+
jQuery.ajaxSettings.traditional = oldTraditional;
88+
} );
89+
7790
QUnit.test( "jQuery.param() Constructed prop values", function( assert ) {
7891
assert.expect( 4 );
7992

0 commit comments

Comments
 (0)