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