From 4837a95b367faa613105a03fa0cb8b8fc50c02d4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Micha=C5=82=20Go=C5=82=C4=99biowski-Owczarek?= Date: Mon, 6 Feb 2023 18:52:58 +0100 Subject: [PATCH] Tests: Add tests for arary data in ajax PR gh-5197 started treating all non-string non-plain-object `data` values as binary. However, `jQuery.ajax` also supports arrays as values of `data`. This change didn't land on `3.x-stable`; however... Surprisingly, we had no tests for array `data` values. This change backports a few such missing tests added in gh-5203. Ref gh-5197 Ref gh-5203 --- test/unit/ajax.js | 268 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 201 insertions(+), 67 deletions(-) diff --git a/test/unit/ajax.js b/test/unit/ajax.js index 257130d76..af740ef45 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -368,7 +368,7 @@ QUnit.module( "ajax", { }; } ); - ajaxTest( "jQuery.ajax() - URL fragment component preservation", 4, function( assert ) { + ajaxTest( "jQuery.ajax() - URL fragment component preservation", 5, function( assert ) { return [ { url: baseURL + "name.html#foo", @@ -400,6 +400,25 @@ QUnit.module( "ajax", { }, error: true }, + { + url: baseURL + "name.html?abc#foo", + data: [ + { + name: "test", + value: 123 + }, + { + name: "devo", + value: "hat" + } + ], + beforeSend: function( xhr, settings ) { + assert.equal( settings.url, baseURL + "name.html?abc&test=123&devo=hat#foo", + "hash preserved for request with query component and array data." ); + return false; + }, + error: true + }, { url: baseURL + "name.html?abc#brownies", data: { @@ -1356,43 +1375,92 @@ QUnit.module( "ajax", { ]; } ); - ajaxTest( "jQuery.ajax() - JSON by content-type", 5, function( assert ) { - return { - url: baseURL + "mock.php?action=json", - data: { - "header": "json", - "array": "1" + ajaxTest( "jQuery.ajax() - JSON by content-type", 10, function( assert ) { + return [ + { + url: baseURL + "mock.php?action=json", + data: { + "header": "json", + "array": "1" + }, + success: function( json ) { + assert.ok( json.length >= 2, "Check length" ); + assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" ); + assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" ); + assert.strictEqual( json[ 1 ][ "name" ], "Peter", "Check JSON: second, name" ); + assert.strictEqual( json[ 1 ][ "age" ], 25, "Check JSON: second, age" ); + } }, - success: function( json ) { - assert.ok( json.length >= 2, "Check length" ); - assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" ); - assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" ); - assert.strictEqual( json[ 1 ][ "name" ], "Peter", "Check JSON: second, name" ); - assert.strictEqual( json[ 1 ][ "age" ], 25, "Check JSON: second, age" ); + { + url: baseURL + "mock.php?action=json", + data: [ + { + name: "header", + value: "json" + }, + { + name: "array", + value: "1" + } + ], + success: function( json ) { + assert.ok( json.length >= 2, "Check length" ); + assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" ); + assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" ); + assert.strictEqual( json[ 1 ][ "name" ], "Peter", "Check JSON: second, name" ); + assert.strictEqual( json[ 1 ][ "age" ], 25, "Check JSON: second, age" ); + } } - }; + ]; } ); - ajaxTest( "jQuery.ajax() - JSON by content-type disabled with options", 6, function( assert ) { - return { - url: url( "mock.php?action=json" ), - data: { - "header": "json", - "array": "1" - }, - contents: { - "json": false + ajaxTest( "jQuery.ajax() - JSON by content-type disabled with options", 12, function( assert ) { + return [ + { + url: url( "mock.php?action=json" ), + data: { + "header": "json", + "array": "1" + }, + contents: { + "json": false + }, + success: function( text ) { + assert.strictEqual( typeof text, "string", "json wasn't auto-determined" ); + var json = JSON.parse( text ); + assert.ok( json.length >= 2, "Check length" ); + assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" ); + assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" ); + assert.strictEqual( json[ 1 ][ "name" ], "Peter", "Check JSON: second, name" ); + assert.strictEqual( json[ 1 ][ "age" ], 25, "Check JSON: second, age" ); + } }, - success: function( text ) { - assert.strictEqual( typeof text, "string", "json wasn't auto-determined" ); - var json = JSON.parse( text ); - assert.ok( json.length >= 2, "Check length" ); - assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" ); - assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" ); - assert.strictEqual( json[ 1 ][ "name" ], "Peter", "Check JSON: second, name" ); - assert.strictEqual( json[ 1 ][ "age" ], 25, "Check JSON: second, age" ); + { + url: url( "mock.php?action=json" ), + data: [ + { + name: "header", + value: "json" + }, + { + name: "array", + value: "1" + } + ], + contents: { + "json": false + }, + success: function( text ) { + assert.strictEqual( typeof text, "string", "json wasn't auto-determined" ); + var json = JSON.parse( text ); + assert.ok( json.length >= 2, "Check length" ); + assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" ); + assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" ); + assert.strictEqual( json[ 1 ][ "name" ], "Peter", "Check JSON: second, name" ); + assert.strictEqual( json[ 1 ][ "age" ], 25, "Check JSON: second, age" ); + } } - }; + ]; } ); ajaxTest( "jQuery.ajax() - simple get", 1, function( assert ) { @@ -1440,18 +1508,36 @@ QUnit.module( "ajax", { }; } ); - ajaxTest( "jQuery.ajax() - data - text/plain (gh-2658)", 1, function( assert ) { - return { - url: "bogus.html", - data: { devo: "A Beautiful World" }, - type: "post", - contentType: "text/plain", - beforeSend: function( _, s ) { - assert.strictEqual( s.data, "devo=A%20Beautiful%20World", "data is %20-encoded" ); - return false; + ajaxTest( "jQuery.ajax() - data - text/plain (gh-2658)", 2, function( assert ) { + return [ + { + url: "bogus.html", + data: { devo: "A Beautiful World" }, + type: "post", + contentType: "text/plain", + beforeSend: function( _, s ) { + assert.strictEqual( s.data, "devo=A%20Beautiful%20World", "data is %20-encoded" ); + return false; + }, + error: true }, - error: true - }; + { + url: "bogus.html", + data: [ + { + name: "devo", + value: "A Beautiful World" + } + ], + type: "post", + contentType: "text/plain", + beforeSend: function( _, s ) { + assert.strictEqual( s.data, "devo=A%20Beautiful%20World", "data is %20-encoded" ); + return false; + }, + error: true + } + ]; } ); ajaxTest( "jQuery.ajax() - don't escape %20 with contentType override (gh-4119)", 1, function( assert ) { @@ -1500,34 +1586,82 @@ QUnit.module( "ajax", { }; } ); - ajaxTest( "jQuery.ajax() - data - no processing POST", 1, function( assert ) { - return { - url: "bogus.html", - data: { devo: "A Beautiful World" }, - type: "post", - contentType: "x-special-sauce", - processData: false, - beforeSend: function( _, s ) { - assert.deepEqual( s.data, { devo: "A Beautiful World" }, "data is not processed" ); - return false; + ajaxTest( "jQuery.ajax() - data - no processing POST", 2, function( assert ) { + return [ + { + url: "bogus.html", + data: { devo: "A Beautiful World" }, + type: "post", + contentType: "x-special-sauce", + processData: false, + beforeSend: function( _, s ) { + assert.deepEqual( s.data, { devo: "A Beautiful World" }, "data is not processed" ); + return false; + }, + error: true }, - error: true - }; + { + url: "bogus.html", + data: [ + { + name: "devo", + value: "A Beautiful World" + } + ], + type: "post", + contentType: "x-special-sauce", + processData: false, + beforeSend: function( _, s ) { + assert.deepEqual( s.data, [ + { + name: "devo", + value: "A Beautiful World" + } + ], "data is not processed" ); + return false; + }, + error: true + } + ]; } ); - ajaxTest( "jQuery.ajax() - data - no processing GET", 1, function( assert ) { - return { - url: "bogus.html", - data: { devo: "A Beautiful World" }, - type: "get", - contentType: "x-something-else", - processData: false, - beforeSend: function( _, s ) { - assert.deepEqual( s.data, { devo: "A Beautiful World" }, "data is not processed" ); - return false; + ajaxTest( "jQuery.ajax() - data - no processing GET", 2, function( assert ) { + return [ + { + url: "bogus.html", + data: { devo: "A Beautiful World" }, + type: "get", + contentType: "x-something-else", + processData: false, + beforeSend: function( _, s ) { + assert.deepEqual( s.data, { devo: "A Beautiful World" }, "data is not processed" ); + return false; + }, + error: true }, - error: true - }; + { + url: "bogus.html", + data: [ + { + name: "devo", + value: "A Beautiful World" + } + ], + type: "get", + contentType: "x-something-else", + processData: false, + beforeSend: function( _, s ) { + assert.deepEqual( s.data, [ + { + name: "devo", + value: "A Beautiful World" + } + ], "data is not processed" ); + return false; + }, + error: true + } + ]; } ); ajaxTest( "jQuery.ajax() - data - process string with GET", 2, function( assert ) { -- 2.39.5