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