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