Allow DELETE requests to have a content body, and properly serialize data to the...
[jquery.git] / test / unit / ajax.js
1 module("ajax");
2
3 // Safari 3 randomly crashes when running these tests,
4 // but only in the full suite - you can run just the Ajax
5 // tests and they'll pass
6 //if ( !jQuery.browser.safari ) {
7
8 if ( !isLocal ) {
9
10 test("jQuery.ajax() - success callbacks", function() {
11         expect( 8 );
12
13         jQuery.ajaxSetup({ timeout: 0 });
14
15         stop();
16
17         jQuery('#foo').ajaxStart(function(){
18                 ok( true, "ajaxStart" );
19         }).ajaxStop(function(){
20                 ok( true, "ajaxStop" );
21                 start();
22         }).ajaxSend(function(){
23                 ok( true, "ajaxSend" );
24         }).ajaxComplete(function(){
25                 ok( true, "ajaxComplete" );
26         }).ajaxError(function(){
27                 ok( false, "ajaxError" );
28         }).ajaxSuccess(function(){
29                 ok( true, "ajaxSuccess" );
30         });
31
32         jQuery.ajax({
33                 url: url("data/name.html"),
34                 beforeSend: function(){ ok(true, "beforeSend"); },
35                 success: function(){ ok(true, "success"); },
36                 error: function(){ ok(false, "error"); },
37                 complete: function(){ ok(true, "complete"); }
38         });
39 });
40
41 test("jQuery.ajax() - error callbacks", function() {
42         expect( 8 );
43         stop();
44
45         jQuery('#foo').ajaxStart(function(){
46                 ok( true, "ajaxStart" );
47         }).ajaxStop(function(){
48                 ok( true, "ajaxStop" );
49                 start();
50         }).ajaxSend(function(){
51                 ok( true, "ajaxSend" );
52         }).ajaxComplete(function(){
53                 ok( true, "ajaxComplete" );
54         }).ajaxError(function(){
55                 ok( true, "ajaxError" );
56         }).ajaxSuccess(function(){
57                 ok( false, "ajaxSuccess" );
58         });
59
60         jQuery.ajaxSetup({ timeout: 500 });
61
62         jQuery.ajax({
63                 url: url("data/name.php?wait=5"),
64                 beforeSend: function(){ ok(true, "beforeSend"); },
65                 success: function(){ ok(false, "success"); },
66                 error: function(){ ok(true, "error"); },
67                 complete: function(){ ok(true, "complete"); }
68         });
69 });
70
71 test(".ajax() - hash", function() {
72         expect(3);
73
74         jQuery.ajax({
75                 url: "data/name.html#foo",
76                 beforeSend: function( xhr, settings ) {
77                         equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
78                         return false;
79                 }
80         });
81
82         jQuery.ajax({
83                 url: "data/name.html?abc#foo",
84                 beforeSend: function( xhr, settings ) {
85                         equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
86                         return false;
87                 }
88         });
89
90         jQuery.ajax({
91                 url: "data/name.html?abc#foo",
92                 data: { "test": 123 },
93                 beforeSend: function( xhr, settings ) {
94                         equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
95                         return false;
96                 }
97         });
98 });
99
100 test(".ajax() - 304", function() {
101         expect( 1 );
102         stop();
103
104         jQuery.ajax({
105                 url: url("data/notmodified.php"),
106                 success: function(){ ok(true, "304 ok"); },
107                 // Do this because opera simply refuses to implement 304 handling :(
108                 // A feature-driven way of detecting this would be appreciated
109                 // See: http://gist.github.com/599419
110                 error: function(){ ok(jQuery.browser.opera, "304 not ok "); },
111                 complete: function(xhr){ start(); }
112         });
113 });
114
115 test(".load()) - 404 error callbacks", function() {
116         expect( 6 );
117         stop();
118
119         jQuery('#foo').ajaxStart(function(){
120                 ok( true, "ajaxStart" );
121         }).ajaxStop(function(){
122                 ok( true, "ajaxStop" );
123                 start();
124         }).ajaxSend(function(){
125                 ok( true, "ajaxSend" );
126         }).ajaxComplete(function(){
127                 ok( true, "ajaxComplete" );
128         }).ajaxError(function(){
129                 ok( true, "ajaxError" );
130         }).ajaxSuccess(function(){
131                 ok( false, "ajaxSuccess" );
132         });
133
134         jQuery("<div/>").load("data/404.html", function(){
135                 ok(true, "complete");
136         });
137 });
138
139 test("jQuery.ajax() - abort", function() {
140         expect( 6 );
141         stop();
142
143         jQuery('#foo').ajaxStart(function(){
144                 ok( true, "ajaxStart" );
145         }).ajaxStop(function(){
146                 ok( true, "ajaxStop" );
147                 start();
148         }).ajaxSend(function(){
149                 ok( true, "ajaxSend" );
150         }).ajaxComplete(function(){
151                 ok( true, "ajaxComplete" );
152         });
153
154         var xhr = jQuery.ajax({
155                 url: url("data/name.php?wait=5"),
156                 beforeSend: function(){ ok(true, "beforeSend"); },
157                 complete: function(){ ok(true, "complete"); }
158         });
159
160         xhr.abort();
161 });
162
163 test("Ajax events with context", function() {
164         expect(14);
165         
166         stop();
167         var context = document.createElement("div");
168         
169         function event(e){
170                 equals( this, context, e.type );
171         }
172
173         function callback(msg){
174                 return function(){
175                         equals( this, context, "context is preserved on callback " + msg );
176                 };
177         }
178
179         function nocallback(msg){
180                 return function(){
181                         equals( typeof this.url, "string", "context is settings on callback " + msg );
182                 };
183         }
184         
185         jQuery('#foo').add(context)
186                         .ajaxSend(event)
187                         .ajaxComplete(event)
188                         .ajaxError(event)
189                         .ajaxSuccess(event);
190
191         jQuery.ajax({
192                 url: url("data/name.html"),
193                 beforeSend: callback("beforeSend"),
194                 success: callback("success"),
195                 error: callback("error"),
196                 complete:function(){
197                         callback("complete").call(this);
198
199                         jQuery.ajax({
200                                 url: url("data/404.html"),
201                                 context: context,
202                                 beforeSend: callback("beforeSend"),
203                                 error: callback("error"),
204                                 complete: function(){
205                                         callback("complete").call(this);
206
207                                         jQuery('#foo').add(context).unbind();
208
209                                         jQuery.ajax({
210                                                 url: url("data/404.html"),
211                                                 beforeSend: nocallback("beforeSend"),
212                                                 error: nocallback("error"),
213                                                 complete: function(){
214                                                         nocallback("complete").call(this);
215                                                         start();
216                                                 }
217                                         });
218                                 }
219                         });
220                 },
221                 context:context
222         });
223 });
224
225 test("jQuery.ajax context modification", function() {
226         expect(1);
227
228         stop();
229
230         var obj = {}
231
232         jQuery.ajax({
233                 url: url("data/name.html"),
234                 context: obj,
235                 beforeSend: function(){
236                         this.test = "foo";
237                 },
238                 complete: function() {
239                         start();
240                 }
241         });
242
243         equals( obj.test, "foo", "Make sure the original object is maintained." );
244 });
245
246 test("jQuery.ajax() - disabled globals", function() {
247         expect( 3 );
248         stop();
249
250         jQuery('#foo').ajaxStart(function(){
251                 ok( false, "ajaxStart" );
252         }).ajaxStop(function(){
253                 ok( false, "ajaxStop" );
254         }).ajaxSend(function(){
255                 ok( false, "ajaxSend" );
256         }).ajaxComplete(function(){
257                 ok( false, "ajaxComplete" );
258         }).ajaxError(function(){
259                 ok( false, "ajaxError" );
260         }).ajaxSuccess(function(){
261                 ok( false, "ajaxSuccess" );
262         });
263
264         jQuery.ajax({
265                 global: false,
266                 url: url("data/name.html"),
267                 beforeSend: function(){ ok(true, "beforeSend"); },
268                 success: function(){ ok(true, "success"); },
269                 error: function(){ ok(false, "error"); },
270                 complete: function(){
271                   ok(true, "complete");
272                   setTimeout(function(){ start(); }, 13);
273                 }
274         });
275 });
276
277 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
278         expect(3);
279         stop();
280         jQuery.ajax({
281           url: url("data/with_fries.xml"),
282           dataType: "xml",
283           success: function(resp) {
284                 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
285                 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
286                 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
287                 start();
288           }
289         });
290 });
291
292 test("jQuery.ajax - HEAD requests", function() {
293         expect(2);
294
295         stop();
296         jQuery.ajax({
297                 url: url("data/name.html"),
298                 type: "HEAD",
299                 success: function(data, status, xhr){
300                         var h = xhr.getAllResponseHeaders();
301                         ok( /Date/i.test(h), 'No Date in HEAD response' );
302                         
303                         jQuery.ajax({
304                                 url: url("data/name.html"),
305                                 data: { whip_it: "good" },
306                                 type: "HEAD",
307                                 success: function(data, status, xhr){
308                                         var h = xhr.getAllResponseHeaders();
309                                         ok( /Date/i.test(h), 'No Date in HEAD response with data' );
310                                         start();
311                                 }
312                         });
313                 }
314         });
315         
316 });
317
318 test("jQuery.ajax - beforeSend", function() {
319         expect(1);
320         stop();
321
322         var check = false;
323
324         jQuery.ajaxSetup({ timeout: 0 });
325
326         jQuery.ajax({
327                 url: url("data/name.html"),
328                 beforeSend: function(xml) {
329                         check = true;
330                 },
331                 success: function(data) {
332                         ok( check, "check beforeSend was executed" );
333                         start();
334                 }
335         });
336 });
337
338 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
339         expect(2);
340         var request = jQuery.ajax({
341                 url: url("data/name.html"),
342                 beforeSend: function() {
343                         ok( true, "beforeSend got called, canceling" );
344                         return false;
345                 },
346                 success: function() {
347                         ok( false, "request didn't get canceled" );
348                 },
349                 complete: function() {
350                         ok( false, "request didn't get canceled" );
351                 },
352                 error: function() {
353                         ok( false, "request didn't get canceled" );
354                 }
355         });
356         ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
357 });
358
359 window.foobar = null;
360 window.testFoo = undefined;
361
362 test("jQuery.ajax - dataType html", function() {
363         expect(5);
364         stop();
365
366         var verifyEvaluation = function() {
367                 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
368                 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
369
370                 start();
371         };
372
373         jQuery.ajax({
374           dataType: "html",
375           url: url("data/test.html"),
376           success: function(data) {
377                 jQuery("#ap").html(data);
378                 ok( data.match(/^html text/), 'Check content for datatype html' );
379                 setTimeout(verifyEvaluation, 600);
380           }
381         });
382 });
383
384 test("serialize()", function() {
385         expect(5);
386
387         // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
388         jQuery("#search").after(
389                 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
390                 '<input type="number" id="html5number" name="number" value="43" />'
391         );
392
393         equals( jQuery('#form').serialize(),
394                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3",
395                 'Check form serialization as query string');
396
397         equals( jQuery('#form :input').serialize(),
398                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3",
399                 'Check input serialization as query string');
400
401         equals( jQuery('#testForm').serialize(),
402                 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
403                 'Check form serialization as query string');
404
405         equals( jQuery('#testForm :input').serialize(),
406                 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
407                 'Check input serialization as query string');
408
409         equals( jQuery('#form, #testForm').serialize(),
410                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
411                 'Multiple form serialization as query string');
412
413   /* Temporarily disabled. Opera 10 has problems with form serialization.
414         equals( jQuery('#form, #testForm :input').serialize(),
415                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
416                 'Mixed form/input serialization as query string');
417         */
418         jQuery("#html5email, #html5number").remove();
419 });
420
421 test("jQuery.param()", function() {
422         expect(22);
423         
424         equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
425   
426         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
427         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
428
429         params = {someName: [1, 2, 3], regularThing: "blah" };
430         equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3&regularThing=blah", "with array" );
431
432         params = {foo: ['a', 'b', 'c']};
433         equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
434
435         params = {foo: ["baz", 42, "All your base are belong to us"] };
436         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
437
438         params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
439         equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" );
440         
441         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?" };
442         equals( 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]=undefined&i[]=10&i[]=11&j=true&k=false&l[]=undefined&l[]=0&m=cowboy+hat?", "huge structure" );
443         
444         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 ] };
445         equals( decodeURIComponent( jQuery.param(params) ), "a[]=0&a[1][]=1&a[1][]=2&a[2][]=3&a[2][1][]=4&a[2][1][]=5&a[2][2][]=6&a[3][b][]=7&a[3][b][1][]=8&a[3][b][1][]=9&a[3][b][2][0][c]=10&a[3][b][2][0][d]=11&a[3][b][3][0][]=12&a[3][b][4][0][0][]=13&a[3][b][5][e][f][g][]=14&a[3][b][5][e][f][g][1][]=15&a[3][b][]=16&a[]=17", "nested arrays" );
446         
447         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?" };
448         equals( jQuery.param(params,true), "a=1&a=2&b=%5Bobject+Object%5D&i=10&i=11&j=true&k=false&l=undefined&l=0&m=cowboy+hat%3F", "huge structure, forced traditional" );
449
450         equals( decodeURIComponent( jQuery.param({ a: [1,2,3], 'b[]': [4,5,6], 'c[d]': [7,8,9], e: { f: [10], g: [11,12], h: 13 } }) ), "a[]=1&a[]=2&a[]=3&b[]=4&b[]=5&b[]=6&c[d][]=7&c[d][]=8&c[d][]=9&e[f][]=10&e[g][]=11&e[g][]=12&e[h]=13", "Make sure params are not double-encoded." );
451
452         // Make sure empty arrays and objects are handled #6481
453         equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
454         equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
455         equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
456         
457         jQuery.ajaxSetup({ traditional: true });
458         
459         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
460         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
461
462         params = {someName: [1, 2, 3], regularThing: "blah" };
463         equals( jQuery.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
464
465         params = {foo: ['a', 'b', 'c']};
466         equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
467
468         params = {"foo[]":["baz", 42, "All your base are belong to us"]};
469         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
470
471         params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
472         equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" );
473         
474         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?" };
475         equals( jQuery.param(params), "a=1&a=2&b=%5Bobject+Object%5D&i=10&i=11&j=true&k=false&l=undefined&l=0&m=cowboy+hat%3F", "huge structure" );
476         
477         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 ] };
478         equals( jQuery.param(params), "a=0&a=1%2C2&a=3%2C4%2C5%2C6&a=%5Bobject+Object%5D&a=17", "nested arrays (not possible when jQuery.param.traditional == true)" );
479         
480         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?" };
481         equals( decodeURIComponent( jQuery.param(params,false) ), "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]=undefined&i[]=10&i[]=11&j=true&k=false&l[]=undefined&l[]=0&m=cowboy+hat?", "huge structure, forced not traditional" );
482         
483         params = { param1: null };
484         equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
485 });
486
487 test("synchronous request", function() {
488         expect(1);
489         ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
490 });
491
492 test("synchronous request with callbacks", function() {
493         expect(2);
494         var result;
495         jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
496         ok( /^{ "data"/.test( result ), "check returned text" );
497 });
498
499 test("pass-through request object", function() {
500         expect(8);
501         stop();
502
503         var target = "data/name.html";
504         var successCount = 0;
505         var errorCount = 0;
506         var errorEx = "";
507         var success = function() {
508                 successCount++;
509         };
510         jQuery("#foo").ajaxError(function (e, xml, s, ex) {
511                 errorCount++;
512                 errorEx += ": " + xml.status;
513         });
514         jQuery("#foo").one('ajaxStop', function () {
515                 equals(successCount, 5, "Check all ajax calls successful");
516                 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
517                 jQuery("#foo").unbind('ajaxError');
518
519                 start();
520         });
521
522         ok( jQuery.get(url(target), success), "get" );
523         ok( jQuery.post(url(target), success), "post" );
524         ok( jQuery.getScript(url("data/test.js"), success), "script" );
525         ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
526         ok( jQuery.ajax({url: url(target), success: success}), "generic" );
527 });
528
529 test("ajax cache", function () {
530         expect(18);
531         
532         stop();
533
534         var count = 0;
535
536         jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
537                 var re = /_=(.*?)(&|$)/g;
538                 var oldOne = null;
539                 for (var i = 0; i < 6; i++) {
540                         var ret = re.exec(s.url);
541                         if (!ret) {
542                                 break;
543                         }
544                         oldOne = ret[1];
545                 }
546                 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
547                 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
548                 if(++count == 6)
549                         start();
550         });
551
552         ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
553         ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
554         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
555         ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
556         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
557         ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
558 });
559
560 /*
561  * Test disabled.
562  * The assertions expect that the passed-in object will be modified,
563  * which shouldn't be the case. Fixes #5439.
564 test("global ajaxSettings", function() {
565         expect(2);
566
567         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
568         var orig = { url: "data/with_fries.xml" };
569         var t;
570
571         jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
572
573         t = jQuery.extend({}, orig);
574         t.data = {};
575         jQuery.ajax(t);
576         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
577
578         t = jQuery.extend({}, orig);
579         t.data = { zoo: 'a', ping: 'b' };
580         jQuery.ajax(t);
581         ok( t.url.indexOf('ping') > -1 && t.url.indexOf('zoo') > -1 && t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending { zoo: 'a', ping: 'b' }" );
582
583         jQuery.ajaxSettings = tmp;
584 });
585 */
586
587 test("load(String)", function() {
588         expect(1);
589         stop(); // check if load can be called with only url
590         jQuery('#first').load("data/name.html", start);
591 });
592
593 test("load('url selector')", function() {
594         expect(1);
595         stop(); // check if load can be called with only url
596         jQuery('#first').load("data/test3.html div.user", function(){
597                 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
598                 start();
599         });
600 });
601
602 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
603         expect(1);
604         stop();
605         jQuery.ajaxSetup({ dataType: "json" });
606         jQuery("#first").ajaxComplete(function (e, xml, s) {
607                 equals( s.dataType, "html", "Verify the load() dataType was html" );
608                 jQuery("#first").unbind("ajaxComplete");
609                 jQuery.ajaxSetup({ dataType: "" });
610                 start();
611         });
612         jQuery('#first').load("data/test3.html");
613 });
614
615 test("load(String, Function) - simple: inject text into DOM", function() {
616         expect(2);
617         stop();
618         jQuery('#first').load(url("data/name.html"), function() {
619                 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
620                 start();
621         });
622 });
623
624 test("load(String, Function) - check scripts", function() {
625         expect(7);
626         stop();
627
628         var verifyEvaluation = function() {
629                 equals( foobar, "bar", 'Check if script src was evaluated after load' );
630                 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
631
632                 start();
633         };
634         jQuery('#first').load(url('data/test.html'), function() {
635                 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
636                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
637                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
638                 setTimeout(verifyEvaluation, 600);
639         });
640 });
641
642 test("load(String, Function) - check file with only a script tag", function() {
643         expect(3);
644         stop();
645
646         jQuery('#first').load(url('data/test2.html'), function() {
647                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
648                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
649
650                 start();
651         });
652 });
653
654 test("load(String, Object, Function)", function() {
655         expect(2);
656         stop();
657
658         jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
659                 var $post = jQuery(this).find('#post');
660                 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
661                 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
662                 start();
663         });
664 });
665
666 test("load(String, String, Function)", function() {
667         expect(2);
668         stop();
669
670         jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
671                 var $get = jQuery(this).find('#get');
672                 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
673                 equals( $get.find('#bar').text(), 'ok', 'Check if a      of data is passed correctly');
674                 start();
675         });
676 });
677
678 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
679         expect(2);
680         stop();
681         jQuery.get(url('data/dashboard.xml'), function(xml) {
682                 var content = [];
683                 jQuery('tab', xml).each(function() {
684                         content.push(jQuery(this).text());
685                 });
686                 equals( content[0], 'blabla', 'Check first tab');
687                 equals( content[1], 'blublu', 'Check second tab');
688                 start();
689         });
690 });
691
692 test("jQuery.getScript(String, Function) - with callback", function() {
693         expect(2);
694         stop();
695         jQuery.getScript(url("data/test.js"), function() {
696                 equals( foobar, "bar", 'Check if script was evaluated' );
697                 setTimeout(start, 100);
698         });
699 });
700
701 test("jQuery.getScript(String, Function) - no callback", function() {
702         expect(1);
703         stop();
704         jQuery.getScript(url("data/test.js"), function(){
705                 start();
706         });
707 });
708
709 test("jQuery.ajax() - JSONP, Local", function() {
710         expect(8);
711
712         var count = 0;
713         function plus(){ if ( ++count == 8 ) start(); }
714
715         stop();
716
717         jQuery.ajax({
718                 url: "data/jsonp.php",
719                 dataType: "jsonp",
720                 success: function(data){
721                         ok( data.data, "JSON results returned (GET, no callback)" );
722                         plus();
723                 },
724                 error: function(data){
725                         ok( false, "Ajax error JSON (GET, no callback)" );
726                         plus();
727                 }
728         });
729
730         jQuery.ajax({
731                 url: "data/jsonp.php?callback=?",
732                 dataType: "jsonp",
733                 success: function(data){
734                         ok( data.data, "JSON results returned (GET, url callback)" );
735                         plus();
736                 },
737                 error: function(data){
738                         ok( false, "Ajax error JSON (GET, url callback)" );
739                         plus();
740                 }
741         });
742
743         jQuery.ajax({
744                 url: "data/jsonp.php",
745                 dataType: "jsonp",
746                 data: "callback=?",
747                 success: function(data){
748                         ok( data.data, "JSON results returned (GET, data callback)" );
749                         plus();
750                 },
751                 error: function(data){
752                         ok( false, "Ajax error JSON (GET, data callback)" );
753                         plus();
754                 }
755         });
756
757         jQuery.ajax({
758                 url: "data/jsonp.php",
759                 dataType: "jsonp",
760                 jsonp: "callback",
761                 success: function(data){
762                         ok( data.data, "JSON results returned (GET, data obj callback)" );
763                         plus();
764                 },
765                 error: function(data){
766                         ok( false, "Ajax error JSON (GET, data obj callback)" );
767                         plus();
768                 }
769         });
770
771         jQuery.ajax({
772                 url: "data/jsonp.php",
773                 dataType: "jsonp",
774                 jsonpCallback: "jsonpResults",
775                 success: function(data){
776                         ok( data.data, "JSON results returned (GET, custom callback name)" );
777                         plus();
778                 },
779                 error: function(data){
780                         ok( false, "Ajax error JSON (GET, custom callback name)" );
781                         plus();
782                 }
783         });
784
785         jQuery.ajax({
786                 type: "POST",
787                 url: "data/jsonp.php",
788                 dataType: "jsonp",
789                 success: function(data){
790                         ok( data.data, "JSON results returned (POST, no callback)" );
791                         plus();
792                 },
793                 error: function(data){
794                         ok( false, "Ajax error JSON (GET, data obj callback)" );
795                         plus();
796                 }
797         });
798
799         jQuery.ajax({
800                 type: "POST",
801                 url: "data/jsonp.php",
802                 data: "callback=?",
803                 dataType: "jsonp",
804                 success: function(data){
805                         ok( data.data, "JSON results returned (POST, data callback)" );
806                         plus();
807                 },
808                 error: function(data){
809                         ok( false, "Ajax error JSON (POST, data callback)" );
810                         plus();
811                 }
812         });
813
814         jQuery.ajax({
815                 type: "POST",
816                 url: "data/jsonp.php",
817                 jsonp: "callback",
818                 dataType: "jsonp",
819                 success: function(data){
820                         ok( data.data, "JSON results returned (POST, data obj callback)" );
821                         plus();
822                 },
823                 error: function(data){
824                         ok( false, "Ajax error JSON (POST, data obj callback)" );
825                         plus();
826                 }
827         });
828         
829   // Supports Ticket #5803      
830         jQuery.ajax({
831                 url: "data/jsonp.php",
832                 jsonpCallback: "jsonpResults",
833                 success: function(data){
834                         ok( data.data, "JSON results returned without dataType:jsonp when jsonpCallback is defined" );
835                         plus();
836                 },
837                 error: function(data){
838                         ok( false, "Ajax error JSON (GET, custom callback name)" );
839                         plus();
840                 }
841         });             
842         
843 });
844
845 test("JSONP - Custom JSONP Callback", function() {
846         expect(1);
847         stop();
848
849         window.jsonpResults = function(data) {
850                 ok( data.data, "JSON results returned (GET, custom callback function)" );
851                 start();
852         };
853
854         jQuery.ajax({
855                 url: "data/jsonp.php",
856                 dataType: "jsonp",
857                 jsonpCallback: "jsonpResults"
858         });
859 });
860
861 test("jQuery.ajax() - JSONP, Remote", function() {
862         expect(4);
863
864         var count = 0;
865         function plus(){ if ( ++count == 4 ) start(); }
866
867         var base = window.location.href.replace(/[^\/]*$/, "");
868
869         stop();
870
871         jQuery.ajax({
872                 url: base + "data/jsonp.php",
873                 dataType: "jsonp",
874                 success: function(data){
875                         ok( data.data, "JSON results returned (GET, no callback)" );
876                         plus();
877                 },
878                 error: function(data){
879                         ok( false, "Ajax error JSON (GET, no callback)" );
880                         plus();
881                 }
882         });
883
884         jQuery.ajax({
885                 url: base + "data/jsonp.php?callback=?",
886                 dataType: "jsonp",
887                 success: function(data){
888                         ok( data.data, "JSON results returned (GET, url callback)" );
889                         plus();
890                 },
891                 error: function(data){
892                         ok( false, "Ajax error JSON (GET, url callback)" );
893                         plus();
894                 }
895         });
896
897         jQuery.ajax({
898                 url: base + "data/jsonp.php",
899                 dataType: "jsonp",
900                 data: "callback=?",
901                 success: function(data){
902                         ok( data.data, "JSON results returned (GET, data callback)" );
903                         plus();
904                 },
905                 error: function(data){
906                         ok( false, "Ajax error JSON (GET, data callback)" );
907                         plus();
908                 }
909         });
910
911         jQuery.ajax({
912                 url: base + "data/jsonp.php",
913                 dataType: "jsonp",
914                 jsonp: "callback",
915                 success: function(data){
916                         ok( data.data, "JSON results returned (GET, data obj callback)" );
917                         plus();
918                 },
919                 error: function(data){
920                         ok( false, "Ajax error JSON (GET, data obj callback)" );
921                         plus();
922                 }
923         });
924 });
925
926 test("jQuery.ajax() - script, Remote", function() {
927         expect(2);
928
929         var base = window.location.href.replace(/[^\/]*$/, "");
930
931         stop();
932
933         jQuery.ajax({
934                 url: base + "data/test.js",
935                 dataType: "script",
936                 success: function(data){
937                         ok( foobar, "Script results returned (GET, no callback)" );
938                         start();
939                 }
940         });
941 });
942
943 test("jQuery.ajax() - script, Remote with POST", function() {
944         expect(3);
945
946         var base = window.location.href.replace(/[^\/]*$/, "");
947
948         stop();
949
950         jQuery.ajax({
951                 url: base + "data/test.js",
952                 type: "POST",
953                 dataType: "script",
954                 success: function(data, status){
955                         ok( foobar, "Script results returned (POST, no callback)" );
956                         equals( status, "success", "Script results returned (POST, no callback)" );
957                         start();
958                 },
959                 error: function(xhr) {
960                         ok( false, "ajax error, status code: " + xhr.status );
961                         start();
962                 }
963         });
964 });
965
966 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
967         expect(2);
968
969         var base = window.location.href.replace(/[^\/]*$/, "");
970         base = base.replace(/^.*?\/\//, "//");
971
972         stop();
973
974         jQuery.ajax({
975                 url: base + "data/test.js",
976                 dataType: "script",
977                 success: function(data){
978                         ok( foobar, "Script results returned (GET, no callback)" );
979                         start();
980                 }
981         });
982 });
983
984 test("jQuery.ajax() - malformed JSON", function() {
985         expect(2);
986
987         stop();
988
989         jQuery.ajax({
990                 url: "data/badjson.js",
991                 dataType: "json",
992                 success: function(){
993                         ok( false, "Success." );
994                         start();
995                 },
996                 error: function(xhr, msg, detailedMsg) {
997                         equals( "parsererror", msg, "A parse error occurred." );
998                         ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
999                         start();
1000                 }
1001         });
1002 });
1003
1004 test("jQuery.ajax() - script by content-type", function() {
1005         expect(1);
1006
1007         stop();
1008
1009         jQuery.ajax({
1010                 url: "data/script.php",
1011                 data: { header: "script" },
1012                 success: function() {
1013                         start();
1014                 }
1015         });
1016 });
1017
1018 test("jQuery.ajax() - json by content-type", function() {
1019         expect(5);
1020
1021         stop();
1022
1023         jQuery.ajax({
1024                 url: "data/json.php",
1025                 data: { header: "json", json: "array" },
1026                 success: function( json ) {
1027                         ok( json.length >= 2, "Check length");
1028                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1029                         equals( json[0].age, 21, 'Check JSON: first, age' );
1030                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1031                         equals( json[1].age, 25, 'Check JSON: second, age' );
1032                         start();
1033                 }
1034         });
1035 });
1036
1037 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1038         expect(5);
1039         stop();
1040         jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1041           ok( json.length >= 2, "Check length");
1042           equals( json[0].name, 'John', 'Check JSON: first, name' );
1043           equals( json[0].age, 21, 'Check JSON: first, age' );
1044           equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1045           equals( json[1].age, 25, 'Check JSON: second, age' );
1046           start();
1047         });
1048 });
1049
1050 test("jQuery.getJSON(String, Function) - JSON object", function() {
1051         expect(2);
1052         stop();
1053         jQuery.getJSON(url("data/json.php"), function(json) {
1054           if (json && json.data) {
1055                   equals( json.data.lang, 'en', 'Check JSON: lang' );
1056                   equals( json.data.length, 25, 'Check JSON: length' );
1057           }
1058           start();
1059         });
1060 });
1061
1062 test("jQuery.getJSON - Using Native JSON", function() {
1063         expect(2);
1064         
1065         var old = window.JSON;
1066         JSON = {
1067                 parse: function(str){
1068                         ok( true, "Verifying that parse method was run" );
1069                         return true;
1070                 }
1071         };
1072
1073         stop();
1074         jQuery.getJSON(url("data/json.php"), function(json) {
1075                 window.JSON = old;
1076                 equals( json, true, "Verifying return value" );
1077                 start();
1078         });
1079 });
1080
1081 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1082         expect(2);
1083
1084         var base = window.location.href.replace(/[^\/]*$/, "");
1085
1086         stop();
1087         jQuery.getJSON(url(base + "data/json.php"), function(json) {
1088           equals( json.data.lang, 'en', 'Check JSON: lang' );
1089           equals( json.data.length, 25, 'Check JSON: length' );
1090           start();
1091         });
1092 });
1093
1094 test("jQuery.post - data", function() {
1095         expect(2);
1096         stop();
1097
1098         jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1099                 jQuery('math', xml).each(function() {
1100                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1101                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1102                 });
1103                 start();
1104         });
1105 });
1106
1107 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1108         expect(4);
1109         stop();
1110         var done = 0;
1111
1112         jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1113           jQuery('math', xml).each(function() {
1114                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1115                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1116                  });
1117           if ( ++done === 2 ) start();
1118         });
1119
1120         jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1121           jQuery('math', xml).each(function() {
1122                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1123                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1124                  });
1125           if ( ++done === 2 ) start();
1126         });
1127 });
1128
1129 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1130         stop();
1131
1132         var passed = 0;
1133
1134         jQuery.ajaxSetup({timeout: 1000});
1135
1136         var pass = function() {
1137                 passed++;
1138                 if ( passed == 2 ) {
1139                         ok( true, 'Check local and global callbacks after timeout' );
1140                         jQuery('#main').unbind("ajaxError");
1141                         start();
1142                 }
1143         };
1144
1145         var fail = function(a,b,c) {
1146                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1147                 start();
1148         };
1149
1150         jQuery('#main').ajaxError(pass);
1151
1152         jQuery.ajax({
1153           type: "GET",
1154           url: url("data/name.php?wait=5"),
1155           error: pass,
1156           success: fail
1157         });
1158
1159         // reset timeout
1160         jQuery.ajaxSetup({timeout: 0});
1161 });
1162
1163 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1164         stop();
1165         jQuery.ajaxSetup({timeout: 50});
1166
1167         jQuery.ajax({
1168           type: "GET",
1169           timeout: 15000,
1170           url: url("data/name.php?wait=1"),
1171           error: function() {
1172                    ok( false, 'Check for local timeout failed' );
1173                    start();
1174           },
1175           success: function() {
1176                 ok( true, 'Check for local timeout' );
1177                 start();
1178           }
1179         });
1180
1181         // reset timeout
1182         jQuery.ajaxSetup({timeout: 0});
1183 });
1184
1185 test("jQuery.ajax - simple get", function() {
1186         expect(1);
1187         stop();
1188         jQuery.ajax({
1189           type: "GET",
1190           url: url("data/name.php?name=foo"),
1191           success: function(msg){
1192                 equals( msg, 'bar', 'Check for GET' );
1193                 start();
1194           }
1195         });
1196 });
1197
1198 test("jQuery.ajax - simple post", function() {
1199         expect(1);
1200         stop();
1201         jQuery.ajax({
1202           type: "POST",
1203           url: url("data/name.php"),
1204           data: "name=peter",
1205           success: function(msg){
1206                 equals( msg, 'pan', 'Check for POST' );
1207                 start();
1208           }
1209         });
1210 });
1211
1212 test("ajaxSetup()", function() {
1213         expect(1);
1214         stop();
1215         jQuery.ajaxSetup({
1216                 url: url("data/name.php?name=foo"),
1217                 success: function(msg){
1218                         equals( msg, 'bar', 'Check for GET' );
1219                         start();
1220                 }
1221         });
1222         jQuery.ajax();
1223 });
1224
1225 /*
1226 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1227         stop();
1228         jQuery.ajax({
1229                 url: "data/name.php?wait=1",
1230                 timeout: 500,
1231                 error: function(request, status) {
1232                         ok( status != null, "status shouldn't be null in error handler" );
1233                         equals( "timeout", status );
1234                         start();
1235                 }
1236         });
1237 });
1238 */
1239
1240 test("data option: evaluate function values (#2806)", function() {
1241         stop();
1242         jQuery.ajax({
1243                 url: "data/echoQuery.php",
1244                 data: {
1245                         key: function() {
1246                                 return "value";
1247                         }
1248                 },
1249                 success: function(result) {
1250                         equals( result, "key=value" );
1251                         start();
1252                 }
1253         })
1254 });
1255
1256 test("data option: empty bodies for non-GET requests", function() {
1257         stop();
1258         jQuery.ajax({
1259                 url: "data/echoData.php",
1260                 data: undefined,
1261                 type: "post",
1262                 success: function(result) {
1263                         equals( result, "" );
1264                         start();
1265                 }
1266         })
1267 });
1268
1269 test("jQuery.ajax - If-Modified-Since support", function() {
1270         expect( 3 );
1271
1272         stop();
1273
1274         var url = "data/if_modified_since.php?ts=" + new Date();
1275
1276         jQuery.ajax({
1277                 url: url,
1278                 ifModified: true,
1279                 success: function(data, status) { 
1280                         equals(status, "success");
1281                         
1282                         jQuery.ajax({
1283                                 url: url,
1284                                 ifModified: true,
1285                                 success: function(data, status) { 
1286                                         if ( data === "FAIL" ) {
1287                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1288                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1289                                         } else {
1290                                                 equals(status, "notmodified");
1291                                                 ok(data == null, "response body should be empty")
1292                                         }
1293                                         start();
1294                                 },
1295                                 error: function() {
1296                                         // Do this because opera simply refuses to implement 304 handling :(
1297                                         // A feature-driven way of detecting this would be appreciated
1298                                         // See: http://gist.github.com/599419
1299                                         ok(jQuery.browser.opera, "error");
1300                                         ok(jQuery.browser.opera, "error");
1301                                         start();
1302                                 }
1303                         });
1304                 },
1305                 error: function() {
1306                         // Do this because opera simply refuses to implement 304 handling :(
1307                         // A feature-driven way of detecting this would be appreciated
1308                         // See: http://gist.github.com/599419
1309                         ok(jQuery.browser.opera, "error");
1310                         start();
1311                 }
1312         });
1313 });
1314
1315 test("jQuery.ajax - Etag support", function() {
1316         expect( 3 );
1317
1318         stop();
1319
1320         var url = "data/etag.php?ts=" + new Date();
1321
1322         jQuery.ajax({
1323                 url: url,
1324                 ifModified: true,
1325                 success: function(data, status) { 
1326                         equals(status, "success");
1327                         
1328                         jQuery.ajax({
1329                                 url: url,
1330                                 ifModified: true,
1331                                 success: function(data, status) { 
1332                                         if ( data === "FAIL" ) {
1333                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1334                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1335                                         } else {
1336                                                 equals(status, "notmodified");
1337                                                 ok(data == null, "response body should be empty")
1338                                         }
1339                                         start();
1340                                 },
1341                                 error: function() {
1342                                         // Do this because opera simply refuses to implement 304 handling :(
1343                                         // A feature-driven way of detecting this would be appreciated
1344                                         // See: http://gist.github.com/599419
1345                                         ok(jQuery.browser.opera, "error");
1346                                         ok(jQuery.browser.opera, "error");
1347                                         start();
1348                                 }
1349                         });
1350                 },
1351                 error: function() {
1352                         // Do this because opera simply refuses to implement 304 handling :(
1353                         // A feature-driven way of detecting this would be appreciated
1354                         // See: http://gist.github.com/599419
1355                         ok(jQuery.browser.opera, "error");
1356                         start();
1357                 }
1358         });
1359 });
1360
1361 test("jQuery.ajax - active counter", function() {
1362     ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
1363 });
1364
1365
1366 }
1367
1368 //}