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