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