Updating #7397 unit tests to correctly test for a persisted display value
[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(8);
714
715         var count = 0;
716         function plus(){ if ( ++count == 8 ) 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
833 test("JSONP - Custom JSONP Callback", function() {
834         expect(1);
835         stop();
836
837         window.jsonpResults = function(data) {
838                 ok( data.data, "JSON results returned (GET, custom callback function)" );
839                 start();
840         };
841
842         jQuery.ajax({
843                 url: "data/jsonp.php",
844                 dataType: "jsonp",
845                 jsonpCallback: "jsonpResults"
846         });
847 });
848
849 test("jQuery.ajax() - JSONP, Remote", function() {
850         expect(4);
851
852         var count = 0;
853         function plus(){ if ( ++count == 4 ) start(); }
854
855         var base = window.location.href.replace(/[^\/]*$/, "");
856
857         stop();
858
859         jQuery.ajax({
860                 url: base + "data/jsonp.php",
861                 dataType: "jsonp",
862                 success: function(data){
863                         ok( data.data, "JSON results returned (GET, no callback)" );
864                         plus();
865                 },
866                 error: function(data){
867                         ok( false, "Ajax error JSON (GET, no callback)" );
868                         plus();
869                 }
870         });
871
872         jQuery.ajax({
873                 url: base + "data/jsonp.php?callback=?",
874                 dataType: "jsonp",
875                 success: function(data){
876                         ok( data.data, "JSON results returned (GET, url callback)" );
877                         plus();
878                 },
879                 error: function(data){
880                         ok( false, "Ajax error JSON (GET, url callback)" );
881                         plus();
882                 }
883         });
884
885         jQuery.ajax({
886                 url: base + "data/jsonp.php",
887                 dataType: "jsonp",
888                 data: "callback=?",
889                 success: function(data){
890                         ok( data.data, "JSON results returned (GET, data callback)" );
891                         plus();
892                 },
893                 error: function(data){
894                         ok( false, "Ajax error JSON (GET, data callback)" );
895                         plus();
896                 }
897         });
898
899         jQuery.ajax({
900                 url: base + "data/jsonp.php",
901                 dataType: "jsonp",
902                 jsonp: "callback",
903                 success: function(data){
904                         ok( data.data, "JSON results returned (GET, data obj callback)" );
905                         plus();
906                 },
907                 error: function(data){
908                         ok( false, "Ajax error JSON (GET, data obj callback)" );
909                         plus();
910                 }
911         });
912 });
913
914 test("jQuery.ajax() - script, Remote", function() {
915         expect(2);
916
917         var base = window.location.href.replace(/[^\/]*$/, "");
918
919         stop();
920
921         jQuery.ajax({
922                 url: base + "data/test.js",
923                 dataType: "script",
924                 success: function(data){
925                         ok( foobar, "Script results returned (GET, no callback)" );
926                         start();
927                 }
928         });
929 });
930
931 test("jQuery.ajax() - script, Remote with POST", function() {
932         expect(3);
933
934         var base = window.location.href.replace(/[^\/]*$/, "");
935
936         stop();
937
938         jQuery.ajax({
939                 url: base + "data/test.js",
940                 type: "POST",
941                 dataType: "script",
942                 success: function(data, status){
943                         ok( foobar, "Script results returned (POST, no callback)" );
944                         equals( status, "success", "Script results returned (POST, no callback)" );
945                         start();
946                 },
947                 error: function(xhr) {
948                         ok( false, "ajax error, status code: " + xhr.status );
949                         start();
950                 }
951         });
952 });
953
954 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
955         expect(2);
956
957         var base = window.location.href.replace(/[^\/]*$/, "");
958         base = base.replace(/^.*?\/\//, "//");
959
960         stop();
961
962         jQuery.ajax({
963                 url: base + "data/test.js",
964                 dataType: "script",
965                 success: function(data){
966                         ok( foobar, "Script results returned (GET, no callback)" );
967                         start();
968                 }
969         });
970 });
971
972 test("jQuery.ajax() - malformed JSON", function() {
973         expect(2);
974
975         stop();
976
977         jQuery.ajax({
978                 url: "data/badjson.js",
979                 dataType: "json",
980                 success: function(){
981                         ok( false, "Success." );
982                         start();
983                 },
984                 error: function(xhr, msg, detailedMsg) {
985                         equals( "parsererror", msg, "A parse error occurred." );
986                         ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
987                         start();
988                 }
989         });
990 });
991
992 test("jQuery.ajax() - script by content-type", function() {
993         expect(1);
994
995         stop();
996
997         jQuery.ajax({
998                 url: "data/script.php",
999                 data: { header: "script" },
1000                 success: function() {
1001                         start();
1002                 }
1003         });
1004 });
1005
1006 test("jQuery.ajax() - json by content-type", function() {
1007         expect(5);
1008
1009         stop();
1010
1011         jQuery.ajax({
1012                 url: "data/json.php",
1013                 data: { header: "json", json: "array" },
1014                 success: function( json ) {
1015                         ok( json.length >= 2, "Check length");
1016                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1017                         equals( json[0].age, 21, 'Check JSON: first, age' );
1018                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1019                         equals( json[1].age, 25, 'Check JSON: second, age' );
1020                         start();
1021                 }
1022         });
1023 });
1024
1025 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1026         expect(5);
1027         stop();
1028         jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1029           ok( json.length >= 2, "Check length");
1030           equals( json[0].name, 'John', 'Check JSON: first, name' );
1031           equals( json[0].age, 21, 'Check JSON: first, age' );
1032           equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1033           equals( json[1].age, 25, 'Check JSON: second, age' );
1034           start();
1035         });
1036 });
1037
1038 test("jQuery.getJSON(String, Function) - JSON object", function() {
1039         expect(2);
1040         stop();
1041         jQuery.getJSON(url("data/json.php"), function(json) {
1042           if (json && json.data) {
1043                   equals( json.data.lang, 'en', 'Check JSON: lang' );
1044                   equals( json.data.length, 25, 'Check JSON: length' );
1045           }
1046           start();
1047         });
1048 });
1049
1050 test("jQuery.getJSON - Using Native JSON", function() {
1051         expect(2);
1052         
1053         var old = window.JSON;
1054         JSON = {
1055                 parse: function(str){
1056                         ok( true, "Verifying that parse method was run" );
1057                         return true;
1058                 }
1059         };
1060
1061         stop();
1062         jQuery.getJSON(url("data/json.php"), function(json) {
1063                 window.JSON = old;
1064                 equals( json, true, "Verifying return value" );
1065                 start();
1066         });
1067 });
1068
1069 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1070         expect(2);
1071
1072         var base = window.location.href.replace(/[^\/]*$/, "");
1073
1074         stop();
1075         jQuery.getJSON(url(base + "data/json.php"), function(json) {
1076           equals( json.data.lang, 'en', 'Check JSON: lang' );
1077           equals( json.data.length, 25, 'Check JSON: length' );
1078           start();
1079         });
1080 });
1081
1082 test("jQuery.post - data", function() {
1083         expect(2);
1084         stop();
1085
1086         jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1087                 jQuery('math', xml).each(function() {
1088                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1089                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1090                 });
1091                 start();
1092         });
1093 });
1094
1095 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1096         expect(4);
1097         stop();
1098         var done = 0;
1099
1100         jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1101           jQuery('math', xml).each(function() {
1102                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1103                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1104                  });
1105           if ( ++done === 2 ) start();
1106         });
1107
1108         jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1109           jQuery('math', xml).each(function() {
1110                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1111                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1112                  });
1113           if ( ++done === 2 ) start();
1114         });
1115 });
1116
1117 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1118         stop();
1119
1120         var passed = 0;
1121
1122         jQuery.ajaxSetup({timeout: 1000});
1123
1124         var pass = function() {
1125                 passed++;
1126                 if ( passed == 2 ) {
1127                         ok( true, 'Check local and global callbacks after timeout' );
1128                         jQuery('#main').unbind("ajaxError");
1129                         start();
1130                 }
1131         };
1132
1133         var fail = function(a,b,c) {
1134                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1135                 start();
1136         };
1137
1138         jQuery('#main').ajaxError(pass);
1139
1140         jQuery.ajax({
1141           type: "GET",
1142           url: url("data/name.php?wait=5"),
1143           error: pass,
1144           success: fail
1145         });
1146
1147         // reset timeout
1148         jQuery.ajaxSetup({timeout: 0});
1149 });
1150
1151 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1152         stop();
1153         jQuery.ajaxSetup({timeout: 50});
1154
1155         jQuery.ajax({
1156           type: "GET",
1157           timeout: 15000,
1158           url: url("data/name.php?wait=1"),
1159           error: function() {
1160                    ok( false, 'Check for local timeout failed' );
1161                    start();
1162           },
1163           success: function() {
1164                 ok( true, 'Check for local timeout' );
1165                 start();
1166           }
1167         });
1168
1169         // reset timeout
1170         jQuery.ajaxSetup({timeout: 0});
1171 });
1172
1173 test("jQuery.ajax - simple get", function() {
1174         expect(1);
1175         stop();
1176         jQuery.ajax({
1177           type: "GET",
1178           url: url("data/name.php?name=foo"),
1179           success: function(msg){
1180                 equals( msg, 'bar', 'Check for GET' );
1181                 start();
1182           }
1183         });
1184 });
1185
1186 test("jQuery.ajax - simple post", function() {
1187         expect(1);
1188         stop();
1189         jQuery.ajax({
1190           type: "POST",
1191           url: url("data/name.php"),
1192           data: "name=peter",
1193           success: function(msg){
1194                 equals( msg, 'pan', 'Check for POST' );
1195                 start();
1196           }
1197         });
1198 });
1199
1200 test("ajaxSetup()", function() {
1201         expect(1);
1202         stop();
1203         jQuery.ajaxSetup({
1204                 url: url("data/name.php?name=foo"),
1205                 success: function(msg){
1206                         equals( msg, 'bar', 'Check for GET' );
1207                         start();
1208                 }
1209         });
1210         jQuery.ajax();
1211 });
1212
1213 /*
1214 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1215         stop();
1216         jQuery.ajax({
1217                 url: "data/name.php?wait=1",
1218                 timeout: 500,
1219                 error: function(request, status) {
1220                         ok( status != null, "status shouldn't be null in error handler" );
1221                         equals( "timeout", status );
1222                         start();
1223                 }
1224         });
1225 });
1226 */
1227
1228 test("data option: evaluate function values (#2806)", function() {
1229         stop();
1230         jQuery.ajax({
1231                 url: "data/echoQuery.php",
1232                 data: {
1233                         key: function() {
1234                                 return "value";
1235                         }
1236                 },
1237                 success: function(result) {
1238                         equals( result, "key=value" );
1239                         start();
1240                 }
1241         })
1242 });
1243
1244 test("data option: empty bodies for non-GET requests", function() {
1245         stop();
1246         jQuery.ajax({
1247                 url: "data/echoData.php",
1248                 data: undefined,
1249                 type: "post",
1250                 success: function(result) {
1251                         equals( result, "" );
1252                         start();
1253                 }
1254         })
1255 });
1256
1257 test("jQuery.ajax - If-Modified-Since support", function() {
1258         expect( 3 );
1259
1260         stop();
1261
1262         var url = "data/if_modified_since.php?ts=" + new Date();
1263
1264         jQuery.ajax({
1265                 url: url,
1266                 ifModified: true,
1267                 success: function(data, status) { 
1268                         equals(status, "success");
1269                         
1270                         jQuery.ajax({
1271                                 url: url,
1272                                 ifModified: true,
1273                                 success: function(data, status) { 
1274                                         if ( data === "FAIL" ) {
1275                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1276                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1277                                         } else {
1278                                                 equals(status, "notmodified");
1279                                                 ok(data == null, "response body should be empty")
1280                                         }
1281                                         start();
1282                                 },
1283                                 error: function() {
1284                                         // Do this because opera simply refuses to implement 304 handling :(
1285                                         // A feature-driven way of detecting this would be appreciated
1286                                         // See: http://gist.github.com/599419
1287                                         ok(jQuery.browser.opera, "error");
1288                                         ok(jQuery.browser.opera, "error");
1289                                         start();
1290                                 }
1291                         });
1292                 },
1293                 error: function() {
1294                         // Do this because opera simply refuses to implement 304 handling :(
1295                         // A feature-driven way of detecting this would be appreciated
1296                         // See: http://gist.github.com/599419
1297                         ok(jQuery.browser.opera, "error");
1298                         start();
1299                 }
1300         });
1301 });
1302
1303 test("jQuery.ajax - Etag support", function() {
1304         expect( 3 );
1305
1306         stop();
1307
1308         var url = "data/etag.php?ts=" + new Date();
1309
1310         jQuery.ajax({
1311                 url: url,
1312                 ifModified: true,
1313                 success: function(data, status) { 
1314                         equals(status, "success");
1315                         
1316                         jQuery.ajax({
1317                                 url: url,
1318                                 ifModified: true,
1319                                 success: function(data, status) { 
1320                                         if ( data === "FAIL" ) {
1321                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1322                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1323                                         } else {
1324                                                 equals(status, "notmodified");
1325                                                 ok(data == null, "response body should be empty")
1326                                         }
1327                                         start();
1328                                 },
1329                                 error: function() {
1330                                         // Do this because opera simply refuses to implement 304 handling :(
1331                                         // A feature-driven way of detecting this would be appreciated
1332                                         // See: http://gist.github.com/599419
1333                                         ok(jQuery.browser.opera, "error");
1334                                         ok(jQuery.browser.opera, "error");
1335                                         start();
1336                                 }
1337                         });
1338                 },
1339                 error: function() {
1340                         // Do this because opera simply refuses to implement 304 handling :(
1341                         // A feature-driven way of detecting this would be appreciated
1342                         // See: http://gist.github.com/599419
1343                         ok(jQuery.browser.opera, "error");
1344                         start();
1345                 }
1346         });
1347 });
1348
1349 test("jQuery.ajax - active counter", function() {
1350     ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
1351 });
1352
1353
1354 }
1355
1356 //}