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