Merge branch 'master' of http://github.com/rwldrn/jquery
[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   // Supports Ticket #5803      
804         jQuery.ajax({
805                 url: "data/jsonp.php",
806                 jsonpCallback: "jsonpResults",
807                 success: function(data){
808                         ok( data.data, "JSON results returned without dataType:jsonp when jsonpCallback is defined" );
809                         plus();
810                 },
811                 error: function(data){
812                         ok( false, "Ajax error JSON (GET, custom callback name)" );
813                         plus();
814                 }
815         });             
816         
817 });
818
819 test("JSONP - Custom JSONP Callback", function() {
820         expect(1);
821         stop();
822
823         window.jsonpResults = function(data) {
824                 ok( data.data, "JSON results returned (GET, custom callback function)" );
825                 start();
826         };
827
828         jQuery.ajax({
829                 url: "data/jsonp.php",
830                 dataType: "jsonp",
831                 jsonpCallback: "jsonpResults"
832         });
833 });
834
835 test("jQuery.ajax() - JSONP, Remote", function() {
836         expect(4);
837
838         var count = 0;
839         function plus(){ if ( ++count == 4 ) start(); }
840
841         var base = window.location.href.replace(/[^\/]*$/, "");
842
843         stop();
844
845         jQuery.ajax({
846                 url: base + "data/jsonp.php",
847                 dataType: "jsonp",
848                 success: function(data){
849                         ok( data.data, "JSON results returned (GET, no callback)" );
850                         plus();
851                 },
852                 error: function(data){
853                         ok( false, "Ajax error JSON (GET, no callback)" );
854                         plus();
855                 }
856         });
857
858         jQuery.ajax({
859                 url: base + "data/jsonp.php?callback=?",
860                 dataType: "jsonp",
861                 success: function(data){
862                         ok( data.data, "JSON results returned (GET, url callback)" );
863                         plus();
864                 },
865                 error: function(data){
866                         ok( false, "Ajax error JSON (GET, url callback)" );
867                         plus();
868                 }
869         });
870
871         jQuery.ajax({
872                 url: base + "data/jsonp.php",
873                 dataType: "jsonp",
874                 data: "callback=?",
875                 success: function(data){
876                         ok( data.data, "JSON results returned (GET, data callback)" );
877                         plus();
878                 },
879                 error: function(data){
880                         ok( false, "Ajax error JSON (GET, data callback)" );
881                         plus();
882                 }
883         });
884
885         jQuery.ajax({
886                 url: base + "data/jsonp.php",
887                 dataType: "jsonp",
888                 jsonp: "callback",
889                 success: function(data){
890                         ok( data.data, "JSON results returned (GET, data obj callback)" );
891                         plus();
892                 },
893                 error: function(data){
894                         ok( false, "Ajax error JSON (GET, data obj callback)" );
895                         plus();
896                 }
897         });
898 });
899
900 test("jQuery.ajax() - script, Remote", function() {
901         expect(2);
902
903         var base = window.location.href.replace(/[^\/]*$/, "");
904
905         stop();
906
907         jQuery.ajax({
908                 url: base + "data/test.js",
909                 dataType: "script",
910                 success: function(data){
911                         ok( foobar, "Script results returned (GET, no callback)" );
912                         start();
913                 }
914         });
915 });
916
917 test("jQuery.ajax() - script, Remote with POST", function() {
918         expect(3);
919
920         var base = window.location.href.replace(/[^\/]*$/, "");
921
922         stop();
923
924         jQuery.ajax({
925                 url: base + "data/test.js",
926                 type: "POST",
927                 dataType: "script",
928                 success: function(data, status){
929                         ok( foobar, "Script results returned (POST, no callback)" );
930                         equals( status, "success", "Script results returned (POST, no callback)" );
931                         start();
932                 },
933                 error: function(xhr) {
934                         ok( false, "ajax error, status code: " + xhr.status );
935                         start();
936                 }
937         });
938 });
939
940 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
941         expect(2);
942
943         var base = window.location.href.replace(/[^\/]*$/, "");
944         base = base.replace(/^.*?\/\//, "//");
945
946         stop();
947
948         jQuery.ajax({
949                 url: base + "data/test.js",
950                 dataType: "script",
951                 success: function(data){
952                         ok( foobar, "Script results returned (GET, no callback)" );
953                         start();
954                 }
955         });
956 });
957
958 test("jQuery.ajax() - malformed JSON", function() {
959         expect(2);
960
961         stop();
962
963         jQuery.ajax({
964                 url: "data/badjson.js",
965                 dataType: "json",
966                 success: function(){
967                         ok( false, "Success." );
968                         start();
969                 },
970                 error: function(xhr, msg, detailedMsg) {
971                         equals( "parsererror", msg, "A parse error occurred." );
972                         ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
973                         start();
974                 }
975         });
976 });
977
978 test("jQuery.ajax() - script by content-type", function() {
979         expect(1);
980
981         stop();
982
983         jQuery.ajax({
984                 url: "data/script.php",
985                 data: { header: "script" },
986                 success: function() {
987                         start();
988                 }
989         });
990 });
991
992 test("jQuery.ajax() - json by content-type", function() {
993         expect(5);
994
995         stop();
996
997         jQuery.ajax({
998                 url: "data/json.php",
999                 data: { header: "json", json: "array" },
1000                 success: function( json ) {
1001                         ok( json.length >= 2, "Check length");
1002                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1003                         equals( json[0].age, 21, 'Check JSON: first, age' );
1004                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1005                         equals( json[1].age, 25, 'Check JSON: second, age' );
1006                         start();
1007                 }
1008         });
1009 });
1010
1011 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1012         expect(5);
1013         stop();
1014         jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1015           ok( json.length >= 2, "Check length");
1016           equals( json[0].name, 'John', 'Check JSON: first, name' );
1017           equals( json[0].age, 21, 'Check JSON: first, age' );
1018           equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1019           equals( json[1].age, 25, 'Check JSON: second, age' );
1020           start();
1021         });
1022 });
1023
1024 test("jQuery.getJSON(String, Function) - JSON object", function() {
1025         expect(2);
1026         stop();
1027         jQuery.getJSON(url("data/json.php"), function(json) {
1028           if (json && json.data) {
1029                   equals( json.data.lang, 'en', 'Check JSON: lang' );
1030                   equals( json.data.length, 25, 'Check JSON: length' );
1031           }
1032           start();
1033         });
1034 });
1035
1036 test("jQuery.getJSON - Using Native JSON", function() {
1037         expect(2);
1038         
1039         var old = window.JSON;
1040         JSON = {
1041                 parse: function(str){
1042                         ok( true, "Verifying that parse method was run" );
1043                         return true;
1044                 }
1045         };
1046
1047         stop();
1048         jQuery.getJSON(url("data/json.php"), function(json) {
1049                 window.JSON = old;
1050                 equals( json, true, "Verifying return value" );
1051                 start();
1052         });
1053 });
1054
1055 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1056         expect(2);
1057
1058         var base = window.location.href.replace(/[^\/]*$/, "");
1059
1060         stop();
1061         jQuery.getJSON(url(base + "data/json.php"), function(json) {
1062           equals( json.data.lang, 'en', 'Check JSON: lang' );
1063           equals( json.data.length, 25, 'Check JSON: length' );
1064           start();
1065         });
1066 });
1067
1068 test("jQuery.post - data", function() {
1069         expect(2);
1070         stop();
1071
1072         jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1073                 jQuery('math', xml).each(function() {
1074                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1075                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1076                 });
1077                 start();
1078         });
1079 });
1080
1081 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1082         expect(4);
1083         stop();
1084         var done = 0;
1085
1086         jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1087           jQuery('math', xml).each(function() {
1088                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1089                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1090                  });
1091           if ( ++done === 2 ) start();
1092         });
1093
1094         jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1095           jQuery('math', xml).each(function() {
1096                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1097                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1098                  });
1099           if ( ++done === 2 ) start();
1100         });
1101 });
1102
1103 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1104         stop();
1105
1106         var passed = 0;
1107
1108         jQuery.ajaxSetup({timeout: 1000});
1109
1110         var pass = function() {
1111                 passed++;
1112                 if ( passed == 2 ) {
1113                         ok( true, 'Check local and global callbacks after timeout' );
1114                         jQuery('#main').unbind("ajaxError");
1115                         start();
1116                 }
1117         };
1118
1119         var fail = function(a,b,c) {
1120                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1121                 start();
1122         };
1123
1124         jQuery('#main').ajaxError(pass);
1125
1126         jQuery.ajax({
1127           type: "GET",
1128           url: url("data/name.php?wait=5"),
1129           error: pass,
1130           success: fail
1131         });
1132
1133         // reset timeout
1134         jQuery.ajaxSetup({timeout: 0});
1135 });
1136
1137 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1138         stop();
1139         jQuery.ajaxSetup({timeout: 50});
1140
1141         jQuery.ajax({
1142           type: "GET",
1143           timeout: 15000,
1144           url: url("data/name.php?wait=1"),
1145           error: function() {
1146                    ok( false, 'Check for local timeout failed' );
1147                    start();
1148           },
1149           success: function() {
1150                 ok( true, 'Check for local timeout' );
1151                 start();
1152           }
1153         });
1154
1155         // reset timeout
1156         jQuery.ajaxSetup({timeout: 0});
1157 });
1158
1159 test("jQuery.ajax - simple get", function() {
1160         expect(1);
1161         stop();
1162         jQuery.ajax({
1163           type: "GET",
1164           url: url("data/name.php?name=foo"),
1165           success: function(msg){
1166                 equals( msg, 'bar', 'Check for GET' );
1167                 start();
1168           }
1169         });
1170 });
1171
1172 test("jQuery.ajax - simple post", function() {
1173         expect(1);
1174         stop();
1175         jQuery.ajax({
1176           type: "POST",
1177           url: url("data/name.php"),
1178           data: "name=peter",
1179           success: function(msg){
1180                 equals( msg, 'pan', 'Check for POST' );
1181                 start();
1182           }
1183         });
1184 });
1185
1186 test("ajaxSetup()", function() {
1187         expect(1);
1188         stop();
1189         jQuery.ajaxSetup({
1190                 url: url("data/name.php?name=foo"),
1191                 success: function(msg){
1192                         equals( msg, 'bar', 'Check for GET' );
1193                         start();
1194                 }
1195         });
1196         jQuery.ajax();
1197 });
1198
1199 /*
1200 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1201         stop();
1202         jQuery.ajax({
1203                 url: "data/name.php?wait=1",
1204                 timeout: 500,
1205                 error: function(request, status) {
1206                         ok( status != null, "status shouldn't be null in error handler" );
1207                         equals( "timeout", status );
1208                         start();
1209                 }
1210         });
1211 });
1212 */
1213
1214 test("data option: evaluate function values (#2806)", function() {
1215         stop();
1216         jQuery.ajax({
1217                 url: "data/echoQuery.php",
1218                 data: {
1219                         key: function() {
1220                                 return "value";
1221                         }
1222                 },
1223                 success: function(result) {
1224                         equals( result, "key=value" );
1225                         start();
1226                 }
1227         })
1228 });
1229
1230 test("data option: empty bodies for non-GET requests", function() {
1231         stop();
1232         jQuery.ajax({
1233                 url: "data/echoData.php",
1234                 data: undefined,
1235                 type: "post",
1236                 success: function(result) {
1237                         equals( result, "" );
1238                         start();
1239                 }
1240         })
1241 });
1242
1243 test("jQuery.ajax - If-Modified-Since support", function() {
1244         expect( 3 );
1245
1246         stop();
1247
1248         var url = "data/if_modified_since.php?ts=" + new Date();
1249
1250         jQuery.ajax({
1251                 url: url,
1252                 ifModified: true,
1253                 success: function(data, status) { 
1254                         equals(status, "success");
1255                         
1256                         jQuery.ajax({
1257                                 url: url,
1258                                 ifModified: true,
1259                                 success: function(data, status) { 
1260                                         if ( data === "FAIL" ) {
1261                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1262                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1263                                         } else {
1264                                                 equals(status, "notmodified");
1265                                                 ok(data == null, "response body should be empty")
1266                                         }
1267                                         start();
1268                                 },
1269                                 error: function() {
1270                                         // Do this because opera simply refuses to implement 304 handling :(
1271                                         // A feature-driven way of detecting this would be appreciated
1272                                         // See: http://gist.github.com/599419
1273                                         ok(jQuery.browser.opera, "error");
1274                                         ok(jQuery.browser.opera, "error");
1275                                         start();
1276                                 }
1277                         });
1278                 },
1279                 error: function() {
1280                         // Do this because opera simply refuses to implement 304 handling :(
1281                         // A feature-driven way of detecting this would be appreciated
1282                         // See: http://gist.github.com/599419
1283                         ok(jQuery.browser.opera, "error");
1284                         start();
1285                 }
1286         });
1287 });
1288
1289 test("jQuery.ajax - Etag support", function() {
1290         expect( 3 );
1291
1292         stop();
1293
1294         var url = "data/etag.php?ts=" + new Date();
1295
1296         jQuery.ajax({
1297                 url: url,
1298                 ifModified: true,
1299                 success: function(data, status) { 
1300                         equals(status, "success");
1301                         
1302                         jQuery.ajax({
1303                                 url: url,
1304                                 ifModified: true,
1305                                 success: function(data, status) { 
1306                                         if ( data === "FAIL" ) {
1307                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1308                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1309                                         } else {
1310                                                 equals(status, "notmodified");
1311                                                 ok(data == null, "response body should be empty")
1312                                         }
1313                                         start();
1314                                 },
1315                                 error: function() {
1316                                         // Do this because opera simply refuses to implement 304 handling :(
1317                                         // A feature-driven way of detecting this would be appreciated
1318                                         // See: http://gist.github.com/599419
1319                                         ok(jQuery.browser.opera, "error");
1320                                         ok(jQuery.browser.opera, "error");
1321                                         start();
1322                                 }
1323                         });
1324                 },
1325                 error: function() {
1326                         // Do this because opera simply refuses to implement 304 handling :(
1327                         // A feature-driven way of detecting this would be appreciated
1328                         // See: http://gist.github.com/599419
1329                         ok(jQuery.browser.opera, "error");
1330                         start();
1331                 }
1332         });
1333 });
1334
1335 test("jQuery.ajax - active counter", function() {
1336     ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
1337 });
1338
1339
1340 }
1341
1342 //}