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