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