Make sure that .find() with multiple direct child selectors is handled correctly...
[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 - beforeSend", function() {
293         expect(1);
294         stop();
295
296         var check = false;
297
298         jQuery.ajaxSetup({ timeout: 0 });
299
300         jQuery.ajax({
301                 url: url("data/name.html"),
302                 beforeSend: function(xml) {
303                         check = true;
304                 },
305                 success: function(data) {
306                         ok( check, "check beforeSend was executed" );
307                         start();
308                 }
309         });
310 });
311
312 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
313         expect(2);
314         var request = jQuery.ajax({
315                 url: url("data/name.html"),
316                 beforeSend: function() {
317                         ok( true, "beforeSend got called, canceling" );
318                         return false;
319                 },
320                 success: function() {
321                         ok( false, "request didn't get canceled" );
322                 },
323                 complete: function() {
324                         ok( false, "request didn't get canceled" );
325                 },
326                 error: function() {
327                         ok( false, "request didn't get canceled" );
328                 }
329         });
330         ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
331 });
332
333 window.foobar = null;
334 window.testFoo = undefined;
335
336 test("jQuery.ajax - dataType html", function() {
337         expect(5);
338         stop();
339
340         var verifyEvaluation = function() {
341                 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
342                 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
343
344                 start();
345         };
346
347         jQuery.ajax({
348           dataType: "html",
349           url: url("data/test.html"),
350           success: function(data) {
351                 jQuery("#ap").html(data);
352                 ok( data.match(/^html text/), 'Check content for datatype html' );
353                 setTimeout(verifyEvaluation, 600);
354           }
355         });
356 });
357
358 test("serialize()", function() {
359         expect(5);
360
361         // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
362         jQuery("#search").after(
363                 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
364                 '<input type="number" id="html5number" name="number" value="43" />'
365         );
366
367         equals( jQuery('#form').serialize(),
368                 "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",
369                 'Check form serialization as query string');
370
371         equals( jQuery('#form :input').serialize(),
372                 "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",
373                 'Check input serialization as query string');
374
375         equals( jQuery('#testForm').serialize(),
376                 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
377                 'Check form serialization as query string');
378
379         equals( jQuery('#testForm :input').serialize(),
380                 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
381                 'Check input serialization as query string');
382
383         equals( jQuery('#form, #testForm').serialize(),
384                 "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=",
385                 'Multiple form serialization as query string');
386
387   /* Temporarily disabled. Opera 10 has problems with form serialization.
388         equals( jQuery('#form, #testForm :input').serialize(),
389                 "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=",
390                 'Mixed form/input serialization as query string');
391         */
392         jQuery("#html5email, #html5number").remove();
393 });
394
395 test("jQuery.param()", function() {
396         expect(22);
397         
398         equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
399   
400         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
401         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
402
403         params = {someName: [1, 2, 3], regularThing: "blah" };
404         equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3&regularThing=blah", "with array" );
405
406         params = {foo: ['a', 'b', 'c']};
407         equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
408
409         params = {foo: ["baz", 42, "All your base are belong to us"] };
410         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
411
412         params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
413         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" );
414         
415         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?" };
416         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" );
417         
418         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 ] };
419         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" );
420         
421         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?" };
422         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" );
423
424         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." );
425
426         // Make sure empty arrays and objects are handled #6481
427         equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
428         equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
429         equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
430         
431         jQuery.ajaxSetup({ traditional: true });
432         
433         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
434         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
435
436         params = {someName: [1, 2, 3], regularThing: "blah" };
437         equals( jQuery.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
438
439         params = {foo: ['a', 'b', 'c']};
440         equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
441
442         params = {"foo[]":["baz", 42, "All your base are belong to us"]};
443         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
444
445         params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
446         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" );
447         
448         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?" };
449         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" );
450         
451         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 ] };
452         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)" );
453         
454         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?" };
455         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" );
456         
457         params = { param1: null };
458         equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
459 });
460
461 test("synchronous request", function() {
462         expect(1);
463         ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
464 });
465
466 test("synchronous request with callbacks", function() {
467         expect(2);
468         var result;
469         jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
470         ok( /^{ "data"/.test( result ), "check returned text" );
471 });
472
473 test("pass-through request object", function() {
474         expect(8);
475         stop();
476
477         var target = "data/name.html";
478         var successCount = 0;
479         var errorCount = 0;
480         var errorEx = "";
481         var success = function() {
482                 successCount++;
483         };
484         jQuery("#foo").ajaxError(function (e, xml, s, ex) {
485                 errorCount++;
486                 errorEx += ": " + xml.status;
487         });
488         jQuery("#foo").one('ajaxStop', function () {
489                 equals(successCount, 5, "Check all ajax calls successful");
490                 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
491                 jQuery("#foo").unbind('ajaxError');
492
493                 start();
494         });
495
496         ok( jQuery.get(url(target), success), "get" );
497         ok( jQuery.post(url(target), success), "post" );
498         ok( jQuery.getScript(url("data/test.js"), success), "script" );
499         ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
500         ok( jQuery.ajax({url: url(target), success: success}), "generic" );
501 });
502
503 test("ajax cache", function () {
504         expect(18);
505         
506         stop();
507
508         var count = 0;
509
510         jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
511                 var re = /_=(.*?)(&|$)/g;
512                 var oldOne = null;
513                 for (var i = 0; i < 6; i++) {
514                         var ret = re.exec(s.url);
515                         if (!ret) {
516                                 break;
517                         }
518                         oldOne = ret[1];
519                 }
520                 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
521                 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
522                 if(++count == 6)
523                         start();
524         });
525
526         ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
527         ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
528         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
529         ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
530         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
531         ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
532 });
533
534 /*
535  * Test disabled.
536  * The assertions expect that the passed-in object will be modified,
537  * which shouldn't be the case. Fixes #5439.
538 test("global ajaxSettings", function() {
539         expect(2);
540
541         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
542         var orig = { url: "data/with_fries.xml" };
543         var t;
544
545         jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
546
547         t = jQuery.extend({}, orig);
548         t.data = {};
549         jQuery.ajax(t);
550         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
551
552         t = jQuery.extend({}, orig);
553         t.data = { zoo: 'a', ping: 'b' };
554         jQuery.ajax(t);
555         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' }" );
556
557         jQuery.ajaxSettings = tmp;
558 });
559 */
560
561 test("load(String)", function() {
562         expect(1);
563         stop(); // check if load can be called with only url
564         jQuery('#first').load("data/name.html", start);
565 });
566
567 test("load('url selector')", function() {
568         expect(1);
569         stop(); // check if load can be called with only url
570         jQuery('#first').load("data/test3.html div.user", function(){
571                 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
572                 start();
573         });
574 });
575
576 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
577         expect(1);
578         stop();
579         jQuery.ajaxSetup({ dataType: "json" });
580         jQuery("#first").ajaxComplete(function (e, xml, s) {
581                 equals( s.dataType, "html", "Verify the load() dataType was html" );
582                 jQuery("#first").unbind("ajaxComplete");
583                 jQuery.ajaxSetup({ dataType: "" });
584                 start();
585         });
586         jQuery('#first').load("data/test3.html");
587 });
588
589 test("load(String, Function) - simple: inject text into DOM", function() {
590         expect(2);
591         stop();
592         jQuery('#first').load(url("data/name.html"), function() {
593                 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
594                 start();
595         });
596 });
597
598 test("load(String, Function) - check scripts", function() {
599         expect(7);
600         stop();
601
602         var verifyEvaluation = function() {
603                 equals( foobar, "bar", 'Check if script src was evaluated after load' );
604                 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
605
606                 start();
607         };
608         jQuery('#first').load(url('data/test.html'), function() {
609                 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
610                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
611                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
612                 setTimeout(verifyEvaluation, 600);
613         });
614 });
615
616 test("load(String, Function) - check file with only a script tag", function() {
617         expect(3);
618         stop();
619
620         jQuery('#first').load(url('data/test2.html'), function() {
621                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
622                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
623
624                 start();
625         });
626 });
627
628 test("load(String, Object, Function)", function() {
629         expect(2);
630         stop();
631
632         jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
633                 var $post = jQuery(this).find('#post');
634                 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
635                 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
636                 start();
637         });
638 });
639
640 test("load(String, String, Function)", function() {
641         expect(2);
642         stop();
643
644         jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
645                 var $get = jQuery(this).find('#get');
646                 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
647                 equals( $get.find('#bar').text(), 'ok', 'Check if a      of data is passed correctly');
648                 start();
649         });
650 });
651
652 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
653         expect(2);
654         stop();
655         jQuery.get(url('data/dashboard.xml'), function(xml) {
656                 var content = [];
657                 jQuery('tab', xml).each(function() {
658                         content.push(jQuery(this).text());
659                 });
660                 equals( content[0], 'blabla', 'Check first tab');
661                 equals( content[1], 'blublu', 'Check second tab');
662                 start();
663         });
664 });
665
666 test("jQuery.getScript(String, Function) - with callback", function() {
667         expect(2);
668         stop();
669         jQuery.getScript(url("data/test.js"), function() {
670                 equals( foobar, "bar", 'Check if script was evaluated' );
671                 setTimeout(start, 100);
672         });
673 });
674
675 test("jQuery.getScript(String, Function) - no callback", function() {
676         expect(1);
677         stop();
678         jQuery.getScript(url("data/test.js"), function(){
679                 start();
680         });
681 });
682
683 test("jQuery.ajax() - JSONP, Local", function() {
684         expect(8);
685
686         var count = 0;
687         function plus(){ if ( ++count == 8 ) start(); }
688
689         stop();
690
691         jQuery.ajax({
692                 url: "data/jsonp.php",
693                 dataType: "jsonp",
694                 success: function(data){
695                         ok( data.data, "JSON results returned (GET, no callback)" );
696                         plus();
697                 },
698                 error: function(data){
699                         ok( false, "Ajax error JSON (GET, no callback)" );
700                         plus();
701                 }
702         });
703
704         jQuery.ajax({
705                 url: "data/jsonp.php?callback=?",
706                 dataType: "jsonp",
707                 success: function(data){
708                         ok( data.data, "JSON results returned (GET, url callback)" );
709                         plus();
710                 },
711                 error: function(data){
712                         ok( false, "Ajax error JSON (GET, url callback)" );
713                         plus();
714                 }
715         });
716
717         jQuery.ajax({
718                 url: "data/jsonp.php",
719                 dataType: "jsonp",
720                 data: "callback=?",
721                 success: function(data){
722                         ok( data.data, "JSON results returned (GET, data callback)" );
723                         plus();
724                 },
725                 error: function(data){
726                         ok( false, "Ajax error JSON (GET, data callback)" );
727                         plus();
728                 }
729         });
730
731         jQuery.ajax({
732                 url: "data/jsonp.php",
733                 dataType: "jsonp",
734                 jsonp: "callback",
735                 success: function(data){
736                         ok( data.data, "JSON results returned (GET, data obj callback)" );
737                         plus();
738                 },
739                 error: function(data){
740                         ok( false, "Ajax error JSON (GET, data obj callback)" );
741                         plus();
742                 }
743         });
744
745         jQuery.ajax({
746                 url: "data/jsonp.php",
747                 dataType: "jsonp",
748                 jsonpCallback: "jsonpResults",
749                 success: function(data){
750                         ok( data.data, "JSON results returned (GET, custom callback name)" );
751                         plus();
752                 },
753                 error: function(data){
754                         ok( false, "Ajax error JSON (GET, custom callback name)" );
755                         plus();
756                 }
757         });
758
759         jQuery.ajax({
760                 type: "POST",
761                 url: "data/jsonp.php",
762                 dataType: "jsonp",
763                 success: function(data){
764                         ok( data.data, "JSON results returned (POST, no callback)" );
765                         plus();
766                 },
767                 error: function(data){
768                         ok( false, "Ajax error JSON (GET, data obj callback)" );
769                         plus();
770                 }
771         });
772
773         jQuery.ajax({
774                 type: "POST",
775                 url: "data/jsonp.php",
776                 data: "callback=?",
777                 dataType: "jsonp",
778                 success: function(data){
779                         ok( data.data, "JSON results returned (POST, data callback)" );
780                         plus();
781                 },
782                 error: function(data){
783                         ok( false, "Ajax error JSON (POST, data callback)" );
784                         plus();
785                 }
786         });
787
788         jQuery.ajax({
789                 type: "POST",
790                 url: "data/jsonp.php",
791                 jsonp: "callback",
792                 dataType: "jsonp",
793                 success: function(data){
794                         ok( data.data, "JSON results returned (POST, data obj callback)" );
795                         plus();
796                 },
797                 error: function(data){
798                         ok( false, "Ajax error JSON (POST, data obj callback)" );
799                         plus();
800                 }
801         });
802 });
803
804 test("JSONP - Custom JSONP Callback", function() {
805         expect(1);
806         stop();
807
808         window.jsonpResults = function(data) {
809                 ok( data.data, "JSON results returned (GET, custom callback function)" );
810                 start();
811         };
812
813         jQuery.ajax({
814                 url: "data/jsonp.php",
815                 dataType: "jsonp",
816                 jsonpCallback: "jsonpResults"
817         });
818 });
819
820 test("jQuery.ajax() - JSONP, Remote", function() {
821         expect(4);
822
823         var count = 0;
824         function plus(){ if ( ++count == 4 ) start(); }
825
826         var base = window.location.href.replace(/[^\/]*$/, "");
827
828         stop();
829
830         jQuery.ajax({
831                 url: base + "data/jsonp.php",
832                 dataType: "jsonp",
833                 success: function(data){
834                         ok( data.data, "JSON results returned (GET, no callback)" );
835                         plus();
836                 },
837                 error: function(data){
838                         ok( false, "Ajax error JSON (GET, no callback)" );
839                         plus();
840                 }
841         });
842
843         jQuery.ajax({
844                 url: base + "data/jsonp.php?callback=?",
845                 dataType: "jsonp",
846                 success: function(data){
847                         ok( data.data, "JSON results returned (GET, url callback)" );
848                         plus();
849                 },
850                 error: function(data){
851                         ok( false, "Ajax error JSON (GET, url callback)" );
852                         plus();
853                 }
854         });
855
856         jQuery.ajax({
857                 url: base + "data/jsonp.php",
858                 dataType: "jsonp",
859                 data: "callback=?",
860                 success: function(data){
861                         ok( data.data, "JSON results returned (GET, data callback)" );
862                         plus();
863                 },
864                 error: function(data){
865                         ok( false, "Ajax error JSON (GET, data callback)" );
866                         plus();
867                 }
868         });
869
870         jQuery.ajax({
871                 url: base + "data/jsonp.php",
872                 dataType: "jsonp",
873                 jsonp: "callback",
874                 success: function(data){
875                         ok( data.data, "JSON results returned (GET, data obj callback)" );
876                         plus();
877                 },
878                 error: function(data){
879                         ok( false, "Ajax error JSON (GET, data obj callback)" );
880                         plus();
881                 }
882         });
883 });
884
885 test("jQuery.ajax() - script, Remote", function() {
886         expect(2);
887
888         var base = window.location.href.replace(/[^\/]*$/, "");
889
890         stop();
891
892         jQuery.ajax({
893                 url: base + "data/test.js",
894                 dataType: "script",
895                 success: function(data){
896                         ok( foobar, "Script results returned (GET, no callback)" );
897                         start();
898                 }
899         });
900 });
901
902 test("jQuery.ajax() - script, Remote with POST", function() {
903         expect(3);
904
905         var base = window.location.href.replace(/[^\/]*$/, "");
906
907         stop();
908
909         jQuery.ajax({
910                 url: base + "data/test.js",
911                 type: "POST",
912                 dataType: "script",
913                 success: function(data, status){
914                         ok( foobar, "Script results returned (POST, no callback)" );
915                         equals( status, "success", "Script results returned (POST, no callback)" );
916                         start();
917                 },
918                 error: function(xhr) {
919                         ok( false, "ajax error, status code: " + xhr.status );
920                         start();
921                 }
922         });
923 });
924
925 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
926         expect(2);
927
928         var base = window.location.href.replace(/[^\/]*$/, "");
929         base = base.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() - malformed JSON", function() {
944         expect(2);
945
946         stop();
947
948         jQuery.ajax({
949                 url: "data/badjson.js",
950                 dataType: "json",
951                 success: function(){
952                         ok( false, "Success." );
953                         start();
954                 },
955                 error: function(xhr, msg, detailedMsg) {
956                         equals( "parsererror", msg, "A parse error occurred." );
957                         ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
958                         start();
959                 }
960         });
961 });
962
963 test("jQuery.ajax() - script by content-type", function() {
964         expect(1);
965
966         stop();
967
968         jQuery.ajax({
969                 url: "data/script.php",
970                 data: { header: "script" },
971                 success: function() {
972                         start();
973                 }
974         });
975 });
976
977 test("jQuery.ajax() - json by content-type", function() {
978         expect(5);
979
980         stop();
981
982         jQuery.ajax({
983                 url: "data/json.php",
984                 data: { header: "json", json: "array" },
985                 success: function( json ) {
986                         ok( json.length >= 2, "Check length");
987                         equals( json[0].name, 'John', 'Check JSON: first, name' );
988                         equals( json[0].age, 21, 'Check JSON: first, age' );
989                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
990                         equals( json[1].age, 25, 'Check JSON: second, age' );
991                         start();
992                 }
993         });
994 });
995
996 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
997         expect(5);
998         stop();
999         jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1000           ok( json.length >= 2, "Check length");
1001           equals( json[0].name, 'John', 'Check JSON: first, name' );
1002           equals( json[0].age, 21, 'Check JSON: first, age' );
1003           equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1004           equals( json[1].age, 25, 'Check JSON: second, age' );
1005           start();
1006         });
1007 });
1008
1009 test("jQuery.getJSON(String, Function) - JSON object", function() {
1010         expect(2);
1011         stop();
1012         jQuery.getJSON(url("data/json.php"), function(json) {
1013           if (json && json.data) {
1014                   equals( json.data.lang, 'en', 'Check JSON: lang' );
1015                   equals( json.data.length, 25, 'Check JSON: length' );
1016           }
1017           start();
1018         });
1019 });
1020
1021 test("jQuery.getJSON - Using Native JSON", function() {
1022         expect(2);
1023         
1024         var old = window.JSON;
1025         JSON = {
1026                 parse: function(str){
1027                         ok( true, "Verifying that parse method was run" );
1028                         return true;
1029                 }
1030         };
1031
1032         stop();
1033         jQuery.getJSON(url("data/json.php"), function(json) {
1034                 window.JSON = old;
1035                 equals( json, true, "Verifying return value" );
1036                 start();
1037         });
1038 });
1039
1040 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1041         expect(2);
1042
1043         var base = window.location.href.replace(/[^\/]*$/, "");
1044
1045         stop();
1046         jQuery.getJSON(url(base + "data/json.php"), function(json) {
1047           equals( json.data.lang, 'en', 'Check JSON: lang' );
1048           equals( json.data.length, 25, 'Check JSON: length' );
1049           start();
1050         });
1051 });
1052
1053 test("jQuery.post - data", function() {
1054         expect(2);
1055         stop();
1056
1057         jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1058                 jQuery('math', xml).each(function() {
1059                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1060                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1061                 });
1062                 start();
1063         });
1064 });
1065
1066 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1067         expect(4);
1068         stop();
1069         var done = 0;
1070
1071         jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1072           jQuery('math', xml).each(function() {
1073                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1074                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1075                  });
1076           if ( ++done === 2 ) start();
1077         });
1078
1079         jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1080           jQuery('math', xml).each(function() {
1081                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1082                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1083                  });
1084           if ( ++done === 2 ) start();
1085         });
1086 });
1087
1088 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1089         stop();
1090
1091         var passed = 0;
1092
1093         jQuery.ajaxSetup({timeout: 1000});
1094
1095         var pass = function() {
1096                 passed++;
1097                 if ( passed == 2 ) {
1098                         ok( true, 'Check local and global callbacks after timeout' );
1099                         jQuery('#main').unbind("ajaxError");
1100                         start();
1101                 }
1102         };
1103
1104         var fail = function(a,b,c) {
1105                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1106                 start();
1107         };
1108
1109         jQuery('#main').ajaxError(pass);
1110
1111         jQuery.ajax({
1112           type: "GET",
1113           url: url("data/name.php?wait=5"),
1114           error: pass,
1115           success: fail
1116         });
1117
1118         // reset timeout
1119         jQuery.ajaxSetup({timeout: 0});
1120 });
1121
1122 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1123         stop();
1124         jQuery.ajaxSetup({timeout: 50});
1125
1126         jQuery.ajax({
1127           type: "GET",
1128           timeout: 15000,
1129           url: url("data/name.php?wait=1"),
1130           error: function() {
1131                    ok( false, 'Check for local timeout failed' );
1132                    start();
1133           },
1134           success: function() {
1135                 ok( true, 'Check for local timeout' );
1136                 start();
1137           }
1138         });
1139
1140         // reset timeout
1141         jQuery.ajaxSetup({timeout: 0});
1142 });
1143
1144 test("jQuery.ajax - simple get", function() {
1145         expect(1);
1146         stop();
1147         jQuery.ajax({
1148           type: "GET",
1149           url: url("data/name.php?name=foo"),
1150           success: function(msg){
1151                 equals( msg, 'bar', 'Check for GET' );
1152                 start();
1153           }
1154         });
1155 });
1156
1157 test("jQuery.ajax - simple post", function() {
1158         expect(1);
1159         stop();
1160         jQuery.ajax({
1161           type: "POST",
1162           url: url("data/name.php"),
1163           data: "name=peter",
1164           success: function(msg){
1165                 equals( msg, 'pan', 'Check for POST' );
1166                 start();
1167           }
1168         });
1169 });
1170
1171 test("ajaxSetup()", function() {
1172         expect(1);
1173         stop();
1174         jQuery.ajaxSetup({
1175                 url: url("data/name.php?name=foo"),
1176                 success: function(msg){
1177                         equals( msg, 'bar', 'Check for GET' );
1178                         start();
1179                 }
1180         });
1181         jQuery.ajax();
1182 });
1183
1184 /*
1185 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1186         stop();
1187         jQuery.ajax({
1188                 url: "data/name.php?wait=1",
1189                 timeout: 500,
1190                 error: function(request, status) {
1191                         ok( status != null, "status shouldn't be null in error handler" );
1192                         equals( "timeout", status );
1193                         start();
1194                 }
1195         });
1196 });
1197 */
1198
1199 test("data option: evaluate function values (#2806)", function() {
1200         stop();
1201         jQuery.ajax({
1202                 url: "data/echoQuery.php",
1203                 data: {
1204                         key: function() {
1205                                 return "value";
1206                         }
1207                 },
1208                 success: function(result) {
1209                         equals( result, "key=value" );
1210                         start();
1211                 }
1212         })
1213 });
1214
1215 test("data option: empty bodies for non-GET requests", function() {
1216         stop();
1217         jQuery.ajax({
1218                 url: "data/echoData.php",
1219                 data: undefined,
1220                 type: "post",
1221                 success: function(result) {
1222                         equals( result, "" );
1223                         start();
1224                 }
1225         })
1226 });
1227
1228 test("jQuery.ajax - If-Modified-Since support", function() {
1229         expect( 3 );
1230
1231         stop();
1232
1233         var url = "data/if_modified_since.php?ts=" + new Date();
1234
1235         jQuery.ajax({
1236                 url: url,
1237                 ifModified: true,
1238                 success: function(data, status) { 
1239                         equals(status, "success");
1240                         
1241                         jQuery.ajax({
1242                                 url: url,
1243                                 ifModified: true,
1244                                 success: function(data, status) { 
1245                                         if ( data === "FAIL" ) {
1246                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1247                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1248                                         } else {
1249                                                 equals(status, "notmodified");
1250                                                 ok(data == null, "response body should be empty")
1251                                         }
1252                                         start();
1253                                 },
1254                                 error: function() {
1255                                         // Do this because opera simply refuses to implement 304 handling :(
1256                                         // A feature-driven way of detecting this would be appreciated
1257                                         // See: http://gist.github.com/599419
1258                                         ok(jQuery.browser.opera, "error");
1259                                         ok(jQuery.browser.opera, "error");
1260                                         start();
1261                                 }
1262                         });
1263                 },
1264                 error: function() {
1265                         // Do this because opera simply refuses to implement 304 handling :(
1266                         // A feature-driven way of detecting this would be appreciated
1267                         // See: http://gist.github.com/599419
1268                         ok(jQuery.browser.opera, "error");
1269                         start();
1270                 }
1271         });
1272 });
1273
1274 test("jQuery.ajax - Etag support", function() {
1275         expect( 3 );
1276
1277         stop();
1278
1279         var url = "data/etag.php?ts=" + new Date();
1280
1281         jQuery.ajax({
1282                 url: url,
1283                 ifModified: true,
1284                 success: function(data, status) { 
1285                         equals(status, "success");
1286                         
1287                         jQuery.ajax({
1288                                 url: url,
1289                                 ifModified: true,
1290                                 success: function(data, status) { 
1291                                         if ( data === "FAIL" ) {
1292                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1293                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1294                                         } else {
1295                                                 equals(status, "notmodified");
1296                                                 ok(data == null, "response body should be empty")
1297                                         }
1298                                         start();
1299                                 },
1300                                 error: function() {
1301                                         // Do this because opera simply refuses to implement 304 handling :(
1302                                         // A feature-driven way of detecting this would be appreciated
1303                                         // See: http://gist.github.com/599419
1304                                         ok(jQuery.browser.opera, "error");
1305                                         ok(jQuery.browser.opera, "error");
1306                                         start();
1307                                 }
1308                         });
1309                 },
1310                 error: function() {
1311                         // Do this because opera simply refuses to implement 304 handling :(
1312                         // A feature-driven way of detecting this would be appreciated
1313                         // See: http://gist.github.com/599419
1314                         ok(jQuery.browser.opera, "error");
1315                         start();
1316                 }
1317         });
1318 });
1319
1320 test("jQuery.ajax - active counter", function() {
1321     ok( jQuery.ajax.active == 0, "ajax active counter should be zero: " + jQuery.ajax.active );
1322 });
1323
1324
1325 }
1326
1327 //}