Fixes #8054 by reverting feature enhancement 5812 (4920). Regexps no longer searches...
[jquery.git] / test / unit / ajax.js
1 module("ajax", { teardown: moduleTeardown });
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() - success callbacks - (url, options) syntax", function() {
42         expect( 8 );
43
44         jQuery.ajaxSetup({ timeout: 0 });
45
46         stop();
47
48         setTimeout(function(){
49                 jQuery('#foo').ajaxStart(function(){
50                         ok( true, "ajaxStart" );
51                 }).ajaxStop(function(){
52                         ok( true, "ajaxStop" );
53                         start();
54                 }).ajaxSend(function(){
55                         ok( true, "ajaxSend" );
56                 }).ajaxComplete(function(){
57                         ok( true, "ajaxComplete" );
58                 }).ajaxError(function(){
59                         ok( false, "ajaxError" );
60                 }).ajaxSuccess(function(){
61                         ok( true, "ajaxSuccess" );
62                 });
63
64                 jQuery.ajax( url("data/name.html") , {
65                         beforeSend: function(){ ok(true, "beforeSend"); },
66                         success: function(){ ok(true, "success"); },
67                         error: function(){ ok(false, "error"); },
68                         complete: function(){ ok(true, "complete"); }
69                 });
70         }, 13);
71 });
72
73 test("jQuery.ajax() - success callbacks (late binding)", function() {
74         expect( 8 );
75
76         jQuery.ajaxSetup({ timeout: 0 });
77
78         stop();
79
80         setTimeout(function(){
81                 jQuery('#foo').ajaxStart(function(){
82                         ok( true, "ajaxStart" );
83                 }).ajaxStop(function(){
84                         ok( true, "ajaxStop" );
85                         start();
86                 }).ajaxSend(function(){
87                         ok( true, "ajaxSend" );
88                 }).ajaxComplete(function(){
89                         ok( true, "ajaxComplete" );
90                 }).ajaxError(function(){
91                         ok( false, "ajaxError" );
92                 }).ajaxSuccess(function(){
93                         ok( true, "ajaxSuccess" );
94                 });
95
96                 jQuery.ajax({
97                         url: url("data/name.html"),
98                         beforeSend: function(){ ok(true, "beforeSend"); }
99                 })
100                         .complete(function(){ ok(true, "complete"); })
101                         .success(function(){ ok(true, "success"); })
102                         .error(function(){ ok(false, "error"); });
103         }, 13);
104 });
105
106 test("jQuery.ajax() - success callbacks (oncomplete binding)", function() {
107         expect( 8 );
108
109         jQuery.ajaxSetup({ timeout: 0 });
110
111         stop();
112
113         setTimeout(function(){
114                 jQuery('#foo').ajaxStart(function(){
115                         ok( true, "ajaxStart" );
116                 }).ajaxStop(function(){
117                         ok( true, "ajaxStop" );
118                 }).ajaxSend(function(){
119                         ok( true, "ajaxSend" );
120                 }).ajaxComplete(function(){
121                         ok( true, "ajaxComplete" );
122                 }).ajaxError(function(){
123                         ok( false, "ajaxError" );
124                 }).ajaxSuccess(function(){
125                         ok( true, "ajaxSuccess" );
126                 });
127
128                 jQuery.ajax({
129                         url: url("data/name.html"),
130                         beforeSend: function(){ ok(true, "beforeSend"); },
131                         complete: function(xhr) {
132                                 xhr
133                                 .complete(function(){ ok(true, "complete"); })
134                                 .success(function(){ ok(true, "success"); })
135                                 .error(function(){ ok(false, "error"); })
136                                 .complete(function(){ start(); });
137                         }
138                 });
139         }, 13);
140 });
141
142 test("jQuery.ajax() - success callbacks (very late binding)", function() {
143         expect( 8 );
144
145         jQuery.ajaxSetup({ timeout: 0 });
146
147         stop();
148
149         setTimeout(function(){
150                 jQuery('#foo').ajaxStart(function(){
151                         ok( true, "ajaxStart" );
152                 }).ajaxStop(function(){
153                         ok( true, "ajaxStop" );
154                 }).ajaxSend(function(){
155                         ok( true, "ajaxSend" );
156                 }).ajaxComplete(function(){
157                         ok( true, "ajaxComplete" );
158                 }).ajaxError(function(){
159                         ok( false, "ajaxError" );
160                 }).ajaxSuccess(function(){
161                         ok( true, "ajaxSuccess" );
162                 });
163
164                 jQuery.ajax({
165                         url: url("data/name.html"),
166                         beforeSend: function(){ ok(true, "beforeSend"); },
167                         complete: function(xhr) {
168                                 setTimeout (function() {
169                                         xhr
170                                         .complete(function(){ ok(true, "complete"); })
171                                         .success(function(){ ok(true, "success"); })
172                                         .error(function(){ ok(false, "error"); })
173                                         .complete(function(){ start(); });
174                                 },100);
175                         }
176                 });
177         }, 13);
178 });
179
180 test("jQuery.ajax() - success callbacks (order)", function() {
181         expect( 1 );
182
183         jQuery.ajaxSetup({ timeout: 0 });
184
185         stop();
186
187         var testString = "";
188
189         setTimeout(function(){
190                 jQuery.ajax({
191                         url: url("data/name.html"),
192                         success: function( _1 , _2 , xhr ) {
193                                 xhr.success(function() {
194                                         xhr.success(function() {
195                                                 testString += "E";
196                                         });
197                                         testString += "D";
198                                 });
199                                 testString += "A";
200                         },
201                         complete: function() {
202                                 strictEqual(testString, "ABCDE", "Proper order");
203                                 start();
204                         }
205                 }).success(function() {
206                         testString += "B";
207                 }).success(function() {
208                         testString += "C";
209                 });
210         }, 13);
211 });
212
213 test("jQuery.ajax() - error callbacks", function() {
214         expect( 8 );
215         stop();
216
217         jQuery('#foo').ajaxStart(function(){
218                 ok( true, "ajaxStart" );
219         }).ajaxStop(function(){
220                 ok( true, "ajaxStop" );
221                 start();
222         }).ajaxSend(function(){
223                 ok( true, "ajaxSend" );
224         }).ajaxComplete(function(){
225                 ok( true, "ajaxComplete" );
226         }).ajaxError(function(){
227                 ok( true, "ajaxError" );
228         }).ajaxSuccess(function(){
229                 ok( false, "ajaxSuccess" );
230         });
231
232         jQuery.ajaxSetup({ timeout: 500 });
233
234         jQuery.ajax({
235                 url: url("data/name.php?wait=5"),
236                 beforeSend: function(){ ok(true, "beforeSend"); },
237                 success: function(){ ok(false, "success"); },
238                 error: function(){ ok(true, "error"); },
239                 complete: function(){ ok(true, "complete"); }
240         });
241 });
242
243 test("jQuery.ajax() - textStatus and errorThrown values", function() {
244
245         var nb = 3;
246
247         expect( 2 * nb );
248         stop();
249
250         function startN() {
251                 if ( !( --nb ) ) {
252                         start();
253                 }
254         }
255
256         jQuery.ajax({
257                 url: url("data/nonExistingURL"),
258                 error: function( _ , textStatus , errorThrown ){
259                         strictEqual( textStatus, "error", "textStatus is 'error' for 404" );
260                         strictEqual( errorThrown, "Not Found", "errorThrown is 'Not Found' for 404");
261                         startN();
262                 }
263         });
264
265         jQuery.ajax({
266                 url: url("data/name.php?wait=5"),
267                 error: function( _ , textStatus , errorThrown ){
268                         strictEqual( textStatus, "abort", "textStatus is 'abort' for abort" );
269                         strictEqual( errorThrown, "abort", "errorThrown is 'abort' for abort");
270                         startN();
271                 }
272         }).abort();
273
274         jQuery.ajax({
275                 url: url("data/name.php?wait=5"),
276                 error: function( _ , textStatus , errorThrown ){
277                         strictEqual( textStatus, "mystatus", "textStatus is 'mystatus' for abort('mystatus')" );
278                         strictEqual( errorThrown, "mystatus", "errorThrown is 'mystatus' for abort('mystatus')");
279                         startN();
280                 }
281         }).abort( "mystatus" );
282 });
283
284 test("jQuery.ajax() - responseText on error", function() {
285
286         expect( 1 );
287
288         stop();
289
290         jQuery.ajax({
291                 url: url("data/errorWithText.php"),
292                 error: function(xhr) {
293                         strictEqual( xhr.responseText , "plain text message" , "Test jXHR.responseText is filled for HTTP errors" );
294                 },
295                 complete: function() {
296                         start();
297                 }
298         });
299 });
300
301 test(".ajax() - retry with jQuery.ajax( this )", function() {
302
303         expect( 1 );
304
305         stop();
306
307         var firstTime = 1;
308
309         jQuery.ajax({
310                 url: url("data/errorWithText.php"),
311                 error: function() {
312                         if ( firstTime ) {
313                                 firstTime = 0;
314                                 jQuery.ajax( this );
315                         } else {
316                                 ok( true , "Test retrying with jQuery.ajax(this) works" );
317                                 start();
318                         }
319                 }
320         });
321
322 });
323
324 test(".ajax() - headers" , function() {
325
326         expect( 2 );
327
328         stop();
329
330         var requestHeaders = {
331                 siMPle: "value",
332                 "SometHing-elsE": "other value",
333                 OthEr: "something else"
334                 },
335                 list = [],
336                 i;
337
338         for( i in requestHeaders ) {
339                 list.push( i );
340         }
341
342         jQuery.ajax(url("data/headers.php?keys="+list.join( "_" ) ), {
343                 headers: requestHeaders,
344                 success: function( data , _ , xhr ) {
345                         var tmp = [];
346                         for ( i in requestHeaders ) {
347                                 tmp.push( i , ": " , requestHeaders[ i ] , "\n" );
348                         }
349                         tmp = tmp.join( "" );
350
351                         equals( data , tmp , "Headers were sent" );
352                         equals( xhr.getResponseHeader( "Sample-Header" ) , "Hello World" , "Sample header received" );
353                         start();
354                 },
355                 error: function(){ ok(false, "error"); }
356         });
357
358 });
359
360 test(".ajax() - Accept header" , function() {
361
362         expect( 1 );
363
364         stop();
365
366         jQuery.ajax(url("data/headers.php?keys=accept"), {
367                 headers: {
368                         Accept: "very wrong accept value"
369                 },
370                 beforeSend: function( xhr ) {
371                         xhr.setRequestHeader( "Accept", "*/*" );
372                 },
373                 success: function( data ) {
374                         strictEqual( data , "accept: */*\n" , "Test Accept header is set to last value provided" );
375                         start();
376                 },
377                 error: function(){ ok(false, "error"); }
378         });
379
380 });
381
382 test(".ajax() - contentType" , function() {
383
384         expect( 2 );
385
386         stop();
387
388         var count = 2;
389
390         function restart() {
391                 if ( ! --count ) {
392                         start();
393                 }
394         }
395
396         jQuery.ajax(url("data/headers.php?keys=content-type" ), {
397                 contentType: "test",
398                 success: function( data ) {
399                         strictEqual( data , "content-type: test\n" , "Test content-type is sent when options.contentType is set" );
400                 },
401                 complete: function() {
402                         restart();
403                 }
404         });
405
406         jQuery.ajax(url("data/headers.php?keys=content-type" ), {
407                 contentType: false,
408                 success: function( data ) {
409                         strictEqual( data , "content-type: \n" , "Test content-type is not sent when options.contentType===false" );
410                 },
411                 complete: function() {
412                         restart();
413                 }
414         });
415
416 });
417
418 test(".ajax() - hash", function() {
419         expect(3);
420
421         jQuery.ajax({
422                 url: "data/name.html#foo",
423                 beforeSend: function( xhr, settings ) {
424                         equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
425                         return false;
426                 }
427         });
428
429         jQuery.ajax({
430                 url: "data/name.html?abc#foo",
431                 beforeSend: function( xhr, settings ) {
432                 equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
433                         return false;
434                 }
435         });
436
437         jQuery.ajax({
438                 url: "data/name.html?abc#foo",
439                 data: { "test": 123 },
440                 beforeSend: function( xhr, settings ) {
441                         equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
442                         return false;
443                 }
444         });
445 });
446
447 test("jQuery ajax - cross-domain detection", function() {
448
449         expect( 4 );
450
451         var loc = document.location,
452                 otherPort = loc.port === 666 ? 667 : 666,
453                 otherProtocol = loc.protocol === "http:" ? "https:" : "http:";
454
455         jQuery.ajax({
456                 dataType: "jsonp",
457                 url: otherProtocol + "//" + loc.host,
458                 beforeSend: function( _ , s ) {
459                         ok( s.crossDomain , "Test different protocols are detected as cross-domain" );
460                         return false;
461                 }
462         });
463
464         jQuery.ajax({
465                 dataType: "jsonp",
466                 url: loc.protocol + '//somewebsitethatdoesnotexist-656329477541.com:' + ( loc.port || 80 ),
467                 beforeSend: function( _ , s ) {
468                         ok( s.crossDomain , "Test different hostnames are detected as cross-domain" );
469                         return false;
470                 }
471         });
472
473         jQuery.ajax({
474                 dataType: "jsonp",
475                 url: loc.protocol + "//" + loc.hostname + ":" + otherPort,
476                 beforeSend: function( _ , s ) {
477                         ok( s.crossDomain , "Test different ports are detected as cross-domain" );
478                         return false;
479                 }
480         });
481
482         jQuery.ajax({
483                 dataType: "jsonp",
484                 url: loc.protocol + "//" + loc.host,
485                 crossDomain: true,
486                 beforeSend: function( _ , s ) {
487                         ok( s.crossDomain , "Test forced crossDomain is detected as cross-domain" );
488                         return false;
489                 }
490         });
491
492 });
493
494 test(".ajax() - 304", function() {
495         expect( 1 );
496         stop();
497
498         jQuery.ajax({
499                 url: url("data/notmodified.php"),
500                 success: function(){ ok(true, "304 ok"); },
501                 // Do this because opera simply refuses to implement 304 handling :(
502                 // A feature-driven way of detecting this would be appreciated
503                 // See: http://gist.github.com/599419
504                 error: function(){ ok(jQuery.browser.opera, "304 not ok "); },
505                 complete: function(xhr){ start(); }
506         });
507 });
508
509 test(".load()) - 404 error callbacks", function() {
510         expect( 6 );
511         stop();
512
513         jQuery('#foo').ajaxStart(function(){
514                 ok( true, "ajaxStart" );
515         }).ajaxStop(function(){
516                 ok( true, "ajaxStop" );
517                 start();
518         }).ajaxSend(function(){
519                 ok( true, "ajaxSend" );
520         }).ajaxComplete(function(){
521                 ok( true, "ajaxComplete" );
522         }).ajaxError(function(){
523                 ok( true, "ajaxError" );
524         }).ajaxSuccess(function(){
525                 ok( false, "ajaxSuccess" );
526         });
527
528         jQuery("<div/>").load("data/404.html", function(){
529                 ok(true, "complete");
530         });
531 });
532
533 test("jQuery.ajax() - abort", function() {
534         expect( 8 );
535         stop();
536
537         jQuery('#foo').ajaxStart(function(){
538                 ok( true, "ajaxStart" );
539         }).ajaxStop(function(){
540                 ok( true, "ajaxStop" );
541                 start();
542         }).ajaxSend(function(){
543                 ok( true, "ajaxSend" );
544         }).ajaxComplete(function(){
545                 ok( true, "ajaxComplete" );
546         });
547
548         var xhr = jQuery.ajax({
549                 url: url("data/name.php?wait=5"),
550                 beforeSend: function(){ ok(true, "beforeSend"); },
551                 complete: function(){ ok(true, "complete"); }
552         });
553
554         equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
555
556         xhr.abort();
557         equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
558 });
559
560 test("Ajax events with context", function() {
561         expect(14);
562
563         stop();
564         var context = document.createElement("div");
565
566         function event(e){
567                 equals( this, context, e.type );
568         }
569
570         function callback(msg){
571                 return function(){
572                         equals( this, context, "context is preserved on callback " + msg );
573                 };
574         }
575
576         function nocallback(msg){
577                 return function(){
578                         equals( typeof this.url, "string", "context is settings on callback " + msg );
579                 };
580         }
581
582         jQuery('#foo').add(context)
583                         .ajaxSend(event)
584                         .ajaxComplete(event)
585                         .ajaxError(event)
586                         .ajaxSuccess(event);
587
588         jQuery.ajax({
589                 url: url("data/name.html"),
590                 beforeSend: callback("beforeSend"),
591                 success: callback("success"),
592                 error: callback("error"),
593                 complete:function(){
594                         callback("complete").call(this);
595
596                         jQuery.ajax({
597                                 url: url("data/404.html"),
598                                 context: context,
599                                 beforeSend: callback("beforeSend"),
600                                 error: callback("error"),
601                                 complete: function(){
602                                         callback("complete").call(this);
603
604                                         jQuery('#foo').add(context).unbind();
605
606                                         jQuery.ajax({
607                                                 url: url("data/404.html"),
608                                                 beforeSend: nocallback("beforeSend"),
609                                                 error: nocallback("error"),
610                                                 complete: function(){
611                                                         nocallback("complete").call(this);
612                                                         start();
613                                                 }
614                                         });
615                                 }
616                         });
617                 },
618                 context:context
619         });
620 });
621
622 test("jQuery.ajax context modification", function() {
623         expect(1);
624
625         stop();
626
627         var obj = {};
628
629         jQuery.ajax({
630                 url: url("data/name.html"),
631                 context: obj,
632                 beforeSend: function(){
633                         this.test = "foo";
634                 },
635                 complete: function() {
636                         start();
637                 }
638         });
639
640         equals( obj.test, "foo", "Make sure the original object is maintained." );
641 });
642
643 test("jQuery.ajax context modification through ajaxSetup", function() {
644         expect(4);
645
646         stop();
647
648         var obj = {};
649
650         jQuery.ajaxSetup({
651                 context: obj
652         });
653
654         strictEqual( jQuery.ajaxSettings.context, obj, "Make sure the context is properly set in ajaxSettings." );
655
656         jQuery.ajax({
657                 url: url("data/name.html"),
658                 complete: function() {
659                         strictEqual( this, obj, "Make sure the original object is maintained." );
660                         jQuery.ajax({
661                                 url: url("data/name.html"),
662                                 context: {},
663                                 complete: function() {
664                                         ok( this !== obj, "Make sure overidding context is possible." );
665                                         jQuery.ajaxSetup({
666                                                 context: false
667                                         });
668                                         jQuery.ajax({
669                                                 url: url("data/name.html"),
670                                                 beforeSend: function(){
671                                                         this.test = "foo2";
672                                                 },
673                                                 complete: function() {
674                                                         ok( this !== obj, "Make sure unsetting context is possible." );
675                                                         start();
676                                                 }
677                                         });
678                                 }
679                         });
680                 }
681         });
682 });
683
684 test("jQuery.ajax() - disabled globals", function() {
685         expect( 3 );
686         stop();
687
688         jQuery('#foo').ajaxStart(function(){
689                 ok( false, "ajaxStart" );
690         }).ajaxStop(function(){
691                 ok( false, "ajaxStop" );
692         }).ajaxSend(function(){
693                 ok( false, "ajaxSend" );
694         }).ajaxComplete(function(){
695                 ok( false, "ajaxComplete" );
696         }).ajaxError(function(){
697                 ok( false, "ajaxError" );
698         }).ajaxSuccess(function(){
699                 ok( false, "ajaxSuccess" );
700         });
701
702         jQuery.ajax({
703                 global: false,
704                 url: url("data/name.html"),
705                 beforeSend: function(){ ok(true, "beforeSend"); },
706                 success: function(){ ok(true, "success"); },
707                 error: function(){ ok(false, "error"); },
708                 complete: function(){
709                   ok(true, "complete");
710                   setTimeout(function(){ start(); }, 13);
711                 }
712         });
713 });
714
715 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
716         expect(3);
717         stop();
718         jQuery.ajax({
719           url: url("data/with_fries.xml"),
720           dataType: "xml",
721           success: function(resp) {
722                 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
723                 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
724                 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
725                 start();
726           }
727         });
728 });
729
730 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
731         expect(3);
732         stop();
733         jQuery.ajax({
734           url: url("data/with_fries_over_jsonp.php"),
735           dataType: "jsonp xml",
736           success: function(resp) {
737                 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
738                 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
739                 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
740                 start();
741           },
742           error: function(_1,_2,error) {
743                 ok( false, error );
744                 start();
745           }
746         });
747 });
748
749 test("jQuery.ajax - HEAD requests", function() {
750         expect(2);
751
752         stop();
753         jQuery.ajax({
754                 url: url("data/name.html"),
755                 type: "HEAD",
756                 success: function(data, status, xhr){
757                         var h = xhr.getAllResponseHeaders();
758                         ok( /Date/i.test(h), 'No Date in HEAD response' );
759
760                         jQuery.ajax({
761                                 url: url("data/name.html"),
762                                 data: { whip_it: "good" },
763                                 type: "HEAD",
764                                 success: function(data, status, xhr){
765                                         var h = xhr.getAllResponseHeaders();
766                                         ok( /Date/i.test(h), 'No Date in HEAD response with data' );
767                                         start();
768                                 }
769                         });
770                 }
771         });
772
773 });
774
775 test("jQuery.ajax - beforeSend", function() {
776         expect(1);
777         stop();
778
779         var check = false;
780
781         jQuery.ajaxSetup({ timeout: 0 });
782
783         jQuery.ajax({
784                 url: url("data/name.html"),
785                 beforeSend: function(xml) {
786                         check = true;
787                 },
788                 success: function(data) {
789                         ok( check, "check beforeSend was executed" );
790                         start();
791                 }
792         });
793 });
794
795 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
796         expect(2);
797         var request = jQuery.ajax({
798                 url: url("data/name.html"),
799                 beforeSend: function() {
800                         ok( true, "beforeSend got called, canceling" );
801                         return false;
802                 },
803                 success: function() {
804                         ok( false, "request didn't get canceled" );
805                 },
806                 complete: function() {
807                         ok( false, "request didn't get canceled" );
808                 },
809                 error: function() {
810                         ok( false, "request didn't get canceled" );
811                 }
812         });
813         ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
814 });
815
816 test("jQuery.ajax - beforeSend, cancel request manually", function() {
817         expect(2);
818         var request = jQuery.ajax({
819                 url: url("data/name.html"),
820                 beforeSend: function(xhr) {
821                         ok( true, "beforeSend got called, canceling" );
822                         xhr.abort();
823                 },
824                 success: function() {
825                         ok( false, "request didn't get canceled" );
826                 },
827                 complete: function() {
828                         ok( false, "request didn't get canceled" );
829                 },
830                 error: function() {
831                         ok( false, "request didn't get canceled" );
832                 }
833         });
834         ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
835 });
836
837 window.foobar = null;
838 window.testFoo = undefined;
839
840 test("jQuery.ajax - dataType html", function() {
841         expect(5);
842         stop();
843
844         var verifyEvaluation = function() {
845                 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
846                 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
847
848                 start();
849         };
850
851         jQuery.ajax({
852           dataType: "html",
853           url: url("data/test.html"),
854           success: function(data) {
855                 jQuery("#ap").html(data);
856                 ok( data.match(/^html text/), 'Check content for datatype html' );
857                 setTimeout(verifyEvaluation, 600);
858           }
859         });
860 });
861
862 test("serialize()", function() {
863         expect(5);
864
865         // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
866         jQuery("#search").after(
867                 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
868                 '<input type="number" id="html5number" name="number" value="43" />'
869         );
870
871         equals( jQuery('#form').serialize(),
872                 "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",
873                 'Check form serialization as query string');
874
875         equals( jQuery('#form :input').serialize(),
876                 "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",
877                 'Check input serialization as query string');
878
879         equals( jQuery('#testForm').serialize(),
880                 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
881                 'Check form serialization as query string');
882
883         equals( jQuery('#testForm :input').serialize(),
884                 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
885                 'Check input serialization as query string');
886
887         equals( jQuery('#form, #testForm').serialize(),
888                 "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%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
889                 'Multiple form serialization as query string');
890
891   /* Temporarily disabled. Opera 10 has problems with form serialization.
892         equals( jQuery('#form, #testForm :input').serialize(),
893                 "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%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
894                 'Mixed form/input serialization as query string');
895         */
896         jQuery("#html5email, #html5number").remove();
897 });
898
899 test("jQuery.param()", function() {
900         expect(22);
901
902         equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
903
904         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
905         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
906
907         params = {someName: [1, 2, 3], regularThing: "blah" };
908         equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3&regularThing=blah", "with array" );
909
910         params = {foo: ['a', 'b', 'c']};
911         equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
912
913         params = {foo: ["baz", 42, "All your base are belong to us"] };
914         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
915
916         params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
917         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" );
918
919         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?" };
920         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" );
921
922         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 ] };
923         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" );
924
925         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?" };
926         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" );
927
928         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." );
929
930         // Make sure empty arrays and objects are handled #6481
931         equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
932         equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
933         equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
934
935         jQuery.ajaxSetup({ traditional: true });
936
937         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
938         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
939
940         params = {someName: [1, 2, 3], regularThing: "blah" };
941         equals( jQuery.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
942
943         params = {foo: ['a', 'b', 'c']};
944         equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
945
946         params = {"foo[]":["baz", 42, "All your base are belong to us"]};
947         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
948
949         params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
950         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" );
951
952         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?" };
953         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" );
954
955         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 ] };
956         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)" );
957
958         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?" };
959         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" );
960
961         params = { param1: null };
962         equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
963 });
964
965 test("synchronous request", function() {
966         expect(1);
967         ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
968 });
969
970 test("synchronous request with callbacks", function() {
971         expect(2);
972         var result;
973         jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
974         ok( /^{ "data"/.test( result ), "check returned text" );
975 });
976
977 test("pass-through request object", function() {
978         expect(8);
979         stop();
980
981         var target = "data/name.html";
982         var successCount = 0;
983         var errorCount = 0;
984         var errorEx = "";
985         var success = function() {
986                 successCount++;
987         };
988         jQuery("#foo").ajaxError(function (e, xml, s, ex) {
989                 errorCount++;
990                 errorEx += ": " + xml.status;
991         });
992         jQuery("#foo").one('ajaxStop', function () {
993                 equals(successCount, 5, "Check all ajax calls successful");
994                 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
995                 jQuery("#foo").unbind('ajaxError');
996
997                 start();
998         });
999
1000         ok( jQuery.get(url(target), success), "get" );
1001         ok( jQuery.post(url(target), success), "post" );
1002         ok( jQuery.getScript(url("data/test.js"), success), "script" );
1003         ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
1004         ok( jQuery.ajax({url: url(target), success: success}), "generic" );
1005 });
1006
1007 test("ajax cache", function () {
1008         expect(18);
1009
1010         stop();
1011
1012         var count = 0;
1013
1014         jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
1015                 var re = /_=(.*?)(&|$)/g;
1016                 var oldOne = null;
1017                 for (var i = 0; i < 6; i++) {
1018                         var ret = re.exec(s.url);
1019                         if (!ret) {
1020                                 break;
1021                         }
1022                         oldOne = ret[1];
1023                 }
1024                 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
1025                 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
1026                 if(++count == 6)
1027                         start();
1028         });
1029
1030         ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
1031         ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
1032         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
1033         ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
1034         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
1035         ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
1036 });
1037
1038 /*
1039  * Test disabled.
1040  * The assertions expect that the passed-in object will be modified,
1041  * which shouldn't be the case. Fixes #5439.
1042 test("global ajaxSettings", function() {
1043         expect(2);
1044
1045         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
1046         var orig = { url: "data/with_fries.xml" };
1047         var t;
1048
1049         jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
1050
1051         t = jQuery.extend({}, orig);
1052         t.data = {};
1053         jQuery.ajax(t);
1054         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
1055
1056         t = jQuery.extend({}, orig);
1057         t.data = { zoo: 'a', ping: 'b' };
1058         jQuery.ajax(t);
1059         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' }" );
1060
1061         jQuery.ajaxSettings = tmp;
1062 });
1063 */
1064
1065 test("load(String)", function() {
1066         expect(1);
1067         stop(); // check if load can be called with only url
1068         jQuery('#first').load("data/name.html", start);
1069 });
1070
1071 test("load('url selector')", function() {
1072         expect(1);
1073         stop(); // check if load can be called with only url
1074         jQuery('#first').load("data/test3.html div.user", function(){
1075                 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
1076                 start();
1077         });
1078 });
1079
1080 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
1081         expect(1);
1082         stop();
1083         jQuery.ajaxSetup({ dataType: "json" });
1084         jQuery("#first").ajaxComplete(function (e, xml, s) {
1085                 equals( s.dataType, "html", "Verify the load() dataType was html" );
1086                 jQuery("#first").unbind("ajaxComplete");
1087                 jQuery.ajaxSetup({ dataType: "" });
1088                 start();
1089         });
1090         jQuery('#first').load("data/test3.html");
1091 });
1092
1093 test("load(String, Function) - simple: inject text into DOM", function() {
1094         expect(2);
1095         stop();
1096         jQuery('#first').load(url("data/name.html"), function() {
1097                 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
1098                 start();
1099         });
1100 });
1101
1102 test("load(String, Function) - check scripts", function() {
1103         expect(7);
1104         stop();
1105
1106         var verifyEvaluation = function() {
1107                 equals( foobar, "bar", 'Check if script src was evaluated after load' );
1108                 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
1109
1110                 start();
1111         };
1112         jQuery('#first').load(url('data/test.html'), function() {
1113                 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
1114                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1115                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1116                 setTimeout(verifyEvaluation, 600);
1117         });
1118 });
1119
1120 test("load(String, Function) - check file with only a script tag", function() {
1121         expect(3);
1122         stop();
1123
1124         jQuery('#first').load(url('data/test2.html'), function() {
1125                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1126                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1127
1128                 start();
1129         });
1130 });
1131
1132 test("load(String, Function) - dataFilter in ajaxSettings", function() {
1133         expect(2);
1134         stop();
1135         jQuery.ajaxSetup({ dataFilter: function() { return "Hello World"; } });
1136         var div = jQuery("<div/>").load(url("data/name.html"), function(responseText) {
1137                 strictEqual( div.html(), "Hello World" , "Test div was filled with filtered data" );
1138                 strictEqual( responseText, "Hello World" , "Test callback receives filtered data" );
1139                 jQuery.ajaxSetup({ dataFilter: 0 });
1140                 start();
1141         });
1142 });
1143
1144 test("load(String, Object, Function)", function() {
1145         expect(2);
1146         stop();
1147
1148         jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
1149                 var $post = jQuery(this).find('#post');
1150                 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
1151                 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
1152                 start();
1153         });
1154 });
1155
1156 test("load(String, String, Function)", function() {
1157         expect(2);
1158         stop();
1159
1160         jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
1161                 var $get = jQuery(this).find('#get');
1162                 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
1163                 equals( $get.find('#bar').text(), 'ok', 'Check if a      of data is passed correctly');
1164                 start();
1165         });
1166 });
1167
1168 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
1169         expect(2);
1170         stop();
1171         jQuery.get(url('data/dashboard.xml'), function(xml) {
1172                 var content = [];
1173                 jQuery('tab', xml).each(function() {
1174                         content.push(jQuery(this).text());
1175                 });
1176                 equals( content[0], 'blabla', 'Check first tab');
1177                 equals( content[1], 'blublu', 'Check second tab');
1178                 start();
1179         });
1180 });
1181
1182 test("jQuery.getScript(String, Function) - with callback", function() {
1183         expect(2);
1184         stop();
1185         jQuery.getScript(url("data/test.js"), function() {
1186                 equals( foobar, "bar", 'Check if script was evaluated' );
1187                 setTimeout(start, 100);
1188         });
1189 });
1190
1191 test("jQuery.getScript(String, Function) - no callback", function() {
1192         expect(1);
1193         stop();
1194         jQuery.getScript(url("data/test.js"), function(){
1195                 start();
1196         });
1197 });
1198
1199 jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) {
1200
1201         test("jQuery.ajax() - JSONP, " + label, function() {
1202                 expect(16);
1203
1204                 var count = 0;
1205                 function plus(){ if ( ++count == 16 ) start(); }
1206
1207                 stop();
1208
1209                 jQuery.ajax({
1210                         url: "data/jsonp.php",
1211                         dataType: "jsonp",
1212                         crossDomain: crossDomain,
1213                         success: function(data){
1214                                 ok( data.data, "JSON results returned (GET, no callback)" );
1215                                 plus();
1216                         },
1217                         error: function(data){
1218                                 ok( false, "Ajax error JSON (GET, no callback)" );
1219                                 plus();
1220                         }
1221                 });
1222
1223                 jQuery.ajax({
1224                         url: "data/jsonp.php?callback=?",
1225                         dataType: "jsonp",
1226                         crossDomain: crossDomain,
1227                         success: function(data){
1228                                 ok( data.data, "JSON results returned (GET, url callback)" );
1229                                 plus();
1230                         },
1231                         error: function(data){
1232                                 ok( false, "Ajax error JSON (GET, url callback)" );
1233                                 plus();
1234                         }
1235                 });
1236
1237                 jQuery.ajax({
1238                         url: "data/jsonp.php",
1239                         dataType: "jsonp",
1240                         crossDomain: crossDomain,
1241                         data: "callback=?",
1242                         success: function(data){
1243                                 ok( data.data, "JSON results returned (GET, data callback)" );
1244                                 plus();
1245                         },
1246                         error: function(data){
1247                                 ok( false, "Ajax error JSON (GET, data callback)" );
1248                                 plus();
1249                         }
1250                 });
1251
1252                 jQuery.ajax({
1253                         url: "data/jsonp.php?callback=??",
1254                         dataType: "jsonp",
1255                         crossDomain: crossDomain,
1256                         success: function(data){
1257                                 ok( data.data, "JSON results returned (GET, url context-free callback)" );
1258                                 plus();
1259                         },
1260                         error: function(data){
1261                                 ok( false, "Ajax error JSON (GET, url context-free callback)" );
1262                                 plus();
1263                         }
1264                 });
1265
1266                 jQuery.ajax({
1267                         url: "data/jsonp.php",
1268                         dataType: "jsonp",
1269                         crossDomain: crossDomain,
1270                         data: "callback=??",
1271                         success: function(data){
1272                                 ok( data.data, "JSON results returned (GET, data context-free callback)" );
1273                                 plus();
1274                         },
1275                         error: function(data){
1276                                 ok( false, "Ajax error JSON (GET, data context-free callback)" );
1277                                 plus();
1278                         }
1279                 });
1280
1281                 jQuery.ajax({
1282                         url: "data/jsonp.php/??",
1283                         dataType: "jsonp",
1284                         crossDomain: crossDomain,
1285                         success: function(data){
1286                                 ok( data.data, "JSON results returned (GET, REST-like)" );
1287                                 plus();
1288                         },
1289                         error: function(data){
1290                                 ok( false, "Ajax error JSON (GET, REST-like)" );
1291                                 plus();
1292                         }
1293                 });
1294
1295                 jQuery.ajax({
1296                         url: "data/jsonp.php/???json=1",
1297                         dataType: "jsonp",
1298                         crossDomain: crossDomain,
1299                         success: function(data){
1300                                 strictEqual( jQuery.type(data), "array", "JSON results returned (GET, REST-like with param)" );
1301                                 plus();
1302                         },
1303                         error: function(data){
1304                                 ok( false, "Ajax error JSON (GET, REST-like with param)" );
1305                                 plus();
1306                         }
1307                 });
1308
1309                 jQuery.ajax({
1310                         url: "data/jsonp.php",
1311                         dataType: "jsonp",
1312                         crossDomain: crossDomain,
1313                         jsonp: "callback",
1314                         success: function(data){
1315                                 ok( data.data, "JSON results returned (GET, data obj callback)" );
1316                                 plus();
1317                         },
1318                         error: function(data){
1319                                 ok( false, "Ajax error JSON (GET, data obj callback)" );
1320                                 plus();
1321                         }
1322                 });
1323
1324                 window.jsonpResults = function(data) {
1325                         ok( data.data, "JSON results returned (GET, custom callback function)" );
1326                         window.jsonpResults = undefined;
1327                         plus();
1328                 };
1329
1330                 jQuery.ajax({
1331                         url: "data/jsonp.php",
1332                         dataType: "jsonp",
1333                         crossDomain: crossDomain,
1334                         jsonpCallback: "jsonpResults",
1335                         success: function(data){
1336                                 ok( data.data, "JSON results returned (GET, custom callback name)" );
1337                                 plus();
1338                         },
1339                         error: function(data){
1340                                 ok( false, "Ajax error JSON (GET, custom callback name)" );
1341                                 plus();
1342                         }
1343                 });
1344
1345                 jQuery.ajax({
1346                         type: "POST",
1347                         url: "data/jsonp.php",
1348                         dataType: "jsonp",
1349                         crossDomain: crossDomain,
1350                         success: function(data){
1351                                 ok( data.data, "JSON results returned (POST, no callback)" );
1352                                 plus();
1353                         },
1354                         error: function(data){
1355                                 ok( false, "Ajax error JSON (GET, data obj callback)" );
1356                                 plus();
1357                         }
1358                 });
1359
1360                 jQuery.ajax({
1361                         type: "POST",
1362                         url: "data/jsonp.php",
1363                         data: "callback=?",
1364                         dataType: "jsonp",
1365                         crossDomain: crossDomain,
1366                         success: function(data){
1367                                 ok( data.data, "JSON results returned (POST, data callback)" );
1368                                 plus();
1369                         },
1370                         error: function(data){
1371                                 ok( false, "Ajax error JSON (POST, data callback)" );
1372                                 plus();
1373                         }
1374                 });
1375
1376                 jQuery.ajax({
1377                         type: "POST",
1378                         url: "data/jsonp.php",
1379                         jsonp: "callback",
1380                         dataType: "jsonp",
1381                         crossDomain: crossDomain,
1382                         success: function(data){
1383                                 ok( data.data, "JSON results returned (POST, data obj callback)" );
1384                                 plus();
1385                         },
1386                         error: function(data){
1387                                 ok( false, "Ajax error JSON (POST, data obj callback)" );
1388                                 plus();
1389                         }
1390                 });
1391
1392                 //#7578
1393                 jQuery.ajax({
1394                         url: "data/jsonp.php",
1395                         dataType: "jsonp",
1396                         crossDomain: crossDomain,
1397                         beforeSend: function(){
1398                                 strictEqual( this.cache, false, "cache must be false on JSON request" );
1399                                 plus();
1400                                 return false;
1401                         }
1402                 });
1403
1404                 jQuery.ajax({
1405                         url: "data/jsonp.php?callback=XXX",
1406                         dataType: "jsonp",
1407                         jsonp: false,
1408                         jsonpCallback: "XXX",
1409                         crossDomain: crossDomain,
1410                         beforeSend: function() {
1411                                 ok( /^data\/jsonp.php\?callback=XXX&_=\d+$/.test( this.url ) ,
1412                                         "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
1413                                 plus();
1414                         },
1415                         success: function(data){
1416                                 ok( data.data, "JSON results returned (GET, custom callback name with no url manipulation)" );
1417                                 plus();
1418                         },
1419                         error: function(data){
1420                                 ok( false, "Ajax error JSON (GET, custom callback name with no url manipulation)" );
1421                                 plus();
1422                         }
1423                 });
1424
1425         });
1426 });
1427
1428 test("jQuery.ajax() - script, Remote", function() {
1429         expect(2);
1430
1431         var base = window.location.href.replace(/[^\/]*$/, "");
1432
1433         stop();
1434
1435         jQuery.ajax({
1436                 url: base + "data/test.js",
1437                 dataType: "script",
1438                 success: function(data){
1439                         ok( foobar, "Script results returned (GET, no callback)" );
1440                         start();
1441                 }
1442         });
1443 });
1444
1445 test("jQuery.ajax() - script, Remote with POST", function() {
1446         expect(3);
1447
1448         var base = window.location.href.replace(/[^\/]*$/, "");
1449
1450         stop();
1451
1452         jQuery.ajax({
1453                 url: base + "data/test.js",
1454                 type: "POST",
1455                 dataType: "script",
1456                 success: function(data, status){
1457                         ok( foobar, "Script results returned (POST, no callback)" );
1458                         equals( status, "success", "Script results returned (POST, no callback)" );
1459                         start();
1460                 },
1461                 error: function(xhr) {
1462                         ok( false, "ajax error, status code: " + xhr.status );
1463                         start();
1464                 }
1465         });
1466 });
1467
1468 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1469         expect(2);
1470
1471         var base = window.location.href.replace(/[^\/]*$/, "");
1472         base = base.replace(/^.*?\/\//, "//");
1473
1474         stop();
1475
1476         jQuery.ajax({
1477                 url: base + "data/test.js",
1478                 dataType: "script",
1479                 success: function(data){
1480                         ok( foobar, "Script results returned (GET, no callback)" );
1481                         start();
1482                 }
1483         });
1484 });
1485
1486 test("jQuery.ajax() - malformed JSON", function() {
1487         expect(2);
1488
1489         stop();
1490
1491         jQuery.ajax({
1492                 url: "data/badjson.js",
1493                 dataType: "json",
1494                 success: function(){
1495                         ok( false, "Success." );
1496                         start();
1497                 },
1498                 error: function(xhr, msg, detailedMsg) {
1499                         equals( "parsererror", msg, "A parse error occurred." );
1500                         ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1501                         start();
1502                 }
1503         });
1504 });
1505
1506 test("jQuery.ajax() - script by content-type", function() {
1507         expect(1);
1508
1509         stop();
1510
1511         jQuery.ajax({
1512                 url: "data/script.php",
1513                 data: { header: "script" },
1514                 success: function() {
1515                         start();
1516                 }
1517         });
1518 });
1519
1520 test("jQuery.ajax() - json by content-type", function() {
1521         expect(5);
1522
1523         stop();
1524
1525         jQuery.ajax({
1526                 url: "data/json.php",
1527                 data: { header: "json", json: "array" },
1528                 success: function( json ) {
1529                         ok( json.length >= 2, "Check length");
1530                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1531                         equals( json[0].age, 21, 'Check JSON: first, age' );
1532                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1533                         equals( json[1].age, 25, 'Check JSON: second, age' );
1534                         start();
1535                 }
1536         });
1537 });
1538
1539 test("jQuery.ajax() - json by content-type disabled with options", function() {
1540         expect(6);
1541
1542         stop();
1543
1544         jQuery.ajax({
1545                 url: url("data/json.php"),
1546                 data: { header: "json", json: "array" },
1547                 contents: {
1548                         json: false
1549                 },
1550                 success: function( text ) {
1551                         equals( typeof text , "string" , "json wasn't auto-determined" );
1552                         var json = jQuery.parseJSON( text );
1553                         ok( json.length >= 2, "Check length");
1554                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1555                         equals( json[0].age, 21, 'Check JSON: first, age' );
1556                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1557                         equals( json[1].age, 25, 'Check JSON: second, age' );
1558                         start();
1559                 }
1560         });
1561 });
1562
1563 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1564         expect(5);
1565         stop();
1566         jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1567           ok( json.length >= 2, "Check length");
1568           equals( json[0].name, 'John', 'Check JSON: first, name' );
1569           equals( json[0].age, 21, 'Check JSON: first, age' );
1570           equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1571           equals( json[1].age, 25, 'Check JSON: second, age' );
1572           start();
1573         });
1574 });
1575
1576 test("jQuery.getJSON(String, Function) - JSON object", function() {
1577         expect(2);
1578         stop();
1579         jQuery.getJSON(url("data/json.php"), function(json) {
1580           if (json && json.data) {
1581                   equals( json.data.lang, 'en', 'Check JSON: lang' );
1582                   equals( json.data.length, 25, 'Check JSON: length' );
1583           }
1584           start();
1585         });
1586 });
1587
1588 test("jQuery.getJSON - Using Native JSON", function() {
1589         expect(2);
1590
1591         var old = window.JSON;
1592         JSON = {
1593                 parse: function(str){
1594                         ok( true, "Verifying that parse method was run" );
1595                         return true;
1596                 }
1597         };
1598
1599         stop();
1600         jQuery.getJSON(url("data/json.php"), function(json) {
1601                 window.JSON = old;
1602                 equals( json, true, "Verifying return value" );
1603                 start();
1604         });
1605 });
1606
1607 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1608         expect(2);
1609
1610         var base = window.location.href.replace(/[^\/]*$/, "");
1611
1612         stop();
1613         jQuery.getJSON(url(base + "data/json.php"), function(json) {
1614           equals( json.data.lang, 'en', 'Check JSON: lang' );
1615           equals( json.data.length, 25, 'Check JSON: length' );
1616           start();
1617         });
1618 });
1619
1620 test("jQuery.post - data", function() {
1621         expect(2);
1622         stop();
1623
1624         jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1625                 jQuery('math', xml).each(function() {
1626                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1627                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1628                 });
1629                 start();
1630         });
1631 });
1632
1633 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1634         expect(4);
1635         stop();
1636         var done = 0;
1637
1638         jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1639           jQuery('math', xml).each(function() {
1640                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1641                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1642                  });
1643           if ( ++done === 2 ) start();
1644         });
1645
1646         jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1647           jQuery('math', xml).each(function() {
1648                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1649                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1650                  });
1651           if ( ++done === 2 ) start();
1652         });
1653 });
1654
1655 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1656         stop();
1657
1658         var passed = 0;
1659
1660         jQuery.ajaxSetup({timeout: 1000});
1661
1662         var pass = function() {
1663                 passed++;
1664                 if ( passed == 2 ) {
1665                         ok( true, 'Check local and global callbacks after timeout' );
1666                         jQuery('#main').unbind("ajaxError");
1667                         start();
1668                 }
1669         };
1670
1671         var fail = function(a,b,c) {
1672                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1673                 start();
1674         };
1675
1676         jQuery('#main').ajaxError(pass);
1677
1678         jQuery.ajax({
1679           type: "GET",
1680           url: url("data/name.php?wait=5"),
1681           error: pass,
1682           success: fail
1683         });
1684
1685         // reset timeout
1686         jQuery.ajaxSetup({timeout: 0});
1687 });
1688
1689 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1690         stop();
1691         jQuery.ajaxSetup({timeout: 50});
1692
1693         jQuery.ajax({
1694           type: "GET",
1695           timeout: 15000,
1696           url: url("data/name.php?wait=1"),
1697           error: function() {
1698                    ok( false, 'Check for local timeout failed' );
1699                    start();
1700           },
1701           success: function() {
1702                 ok( true, 'Check for local timeout' );
1703                 start();
1704           }
1705         });
1706
1707         // reset timeout
1708         jQuery.ajaxSetup({timeout: 0});
1709 });
1710
1711 test("jQuery.ajax - simple get", function() {
1712         expect(1);
1713         stop();
1714         jQuery.ajax({
1715           type: "GET",
1716           url: url("data/name.php?name=foo"),
1717           success: function(msg){
1718                 equals( msg, 'bar', 'Check for GET' );
1719                 start();
1720           }
1721         });
1722 });
1723
1724 test("jQuery.ajax - simple post", function() {
1725         expect(1);
1726         stop();
1727         jQuery.ajax({
1728           type: "POST",
1729           url: url("data/name.php"),
1730           data: "name=peter",
1731           success: function(msg){
1732                 equals( msg, 'pan', 'Check for POST' );
1733                 start();
1734           }
1735         });
1736 });
1737
1738 test("ajaxSetup()", function() {
1739         expect(1);
1740         stop();
1741         jQuery.ajaxSetup({
1742                 url: url("data/name.php?name=foo"),
1743                 success: function(msg){
1744                         equals( msg, 'bar', 'Check for GET' );
1745                         start();
1746                 }
1747         });
1748         jQuery.ajax();
1749 });
1750
1751 /*
1752 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1753         stop();
1754         jQuery.ajax({
1755                 url: "data/name.php?wait=1",
1756                 timeout: 500,
1757                 error: function(request, status) {
1758                         ok( status != null, "status shouldn't be null in error handler" );
1759                         equals( "timeout", status );
1760                         start();
1761                 }
1762         });
1763 });
1764 */
1765
1766 test("data option: evaluate function values (#2806)", function() {
1767         stop();
1768         jQuery.ajax({
1769                 url: "data/echoQuery.php",
1770                 data: {
1771                         key: function() {
1772                                 return "value";
1773                         }
1774                 },
1775                 success: function(result) {
1776                         equals( result, "key=value" );
1777                         start();
1778                 }
1779         });
1780 });
1781
1782 test("data option: empty bodies for non-GET requests", function() {
1783         stop();
1784         jQuery.ajax({
1785                 url: "data/echoData.php",
1786                 data: undefined,
1787                 type: "post",
1788                 success: function(result) {
1789                         equals( result, "" );
1790                         start();
1791                 }
1792         });
1793 });
1794
1795 test("jQuery.ajax - If-Modified-Since support", function() {
1796         expect( 3 );
1797
1798         stop();
1799
1800         var url = "data/if_modified_since.php?ts=" + new Date();
1801
1802         jQuery.ajax({
1803                 url: url,
1804                 ifModified: true,
1805                 success: function(data, status) {
1806                         equals(status, "success");
1807
1808                         jQuery.ajax({
1809                                 url: url,
1810                                 ifModified: true,
1811                                 success: function(data, status) {
1812                                         if ( data === "FAIL" ) {
1813                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1814                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1815                                         } else {
1816                                                 equals(status, "notmodified");
1817                                                 ok(data == null, "response body should be empty");
1818                                         }
1819                                         start();
1820                         },
1821                                 error: function() {
1822                                         // Do this because opera simply refuses to implement 304 handling :(
1823                                         // A feature-driven way of detecting this would be appreciated
1824                                         // See: http://gist.github.com/599419
1825                                         ok(jQuery.browser.opera, "error");
1826                                         ok(jQuery.browser.opera, "error");
1827                                         start();
1828                         }
1829                         });
1830                 },
1831                 error: function() {
1832                         equals(false, "error");
1833                         // Do this because opera simply refuses to implement 304 handling :(
1834                         // A feature-driven way of detecting this would be appreciated
1835                         // See: http://gist.github.com/599419
1836                         ok(jQuery.browser.opera, "error");
1837                         start();
1838                 }
1839         });
1840 });
1841
1842 test("jQuery.ajax - Etag support", function() {
1843         expect( 3 );
1844
1845         stop();
1846
1847         var url = "data/etag.php?ts=" + new Date();
1848
1849         jQuery.ajax({
1850                 url: url,
1851                 ifModified: true,
1852                 success: function(data, status) {
1853                         equals(status, "success");
1854
1855                         jQuery.ajax({
1856                                 url: url,
1857                                 ifModified: true,
1858                                 success: function(data, status) {
1859                                         if ( data === "FAIL" ) {
1860                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1861                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1862                                         } else {
1863                                                 equals(status, "notmodified");
1864                                                 ok(data == null, "response body should be empty");
1865                                         }
1866                                         start();
1867                         },
1868                         error: function() {
1869                                         // Do this because opera simply refuses to implement 304 handling :(
1870                                         // A feature-driven way of detecting this would be appreciated
1871                                         // See: http://gist.github.com/599419
1872                                         ok(jQuery.browser.opera, "error");
1873                                         ok(jQuery.browser.opera, "error");
1874                                         start();
1875                                 }
1876                         });
1877                 },
1878                 error: function() {
1879                         // Do this because opera simply refuses to implement 304 handling :(
1880                         // A feature-driven way of detecting this would be appreciated
1881                         // See: http://gist.github.com/599419
1882                         ok(jQuery.browser.opera, "error");
1883                         start();
1884                 }
1885         });
1886 });
1887
1888 test("jQuery ajax - failing cross-domain", function() {
1889
1890         expect( 2 );
1891
1892         stop();
1893
1894         var i = 2;
1895
1896         jQuery.ajax({
1897                 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
1898                 success: function(){ ok( false , "success" ); },
1899                 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1900                 complete: function() { if ( ! --i ) start(); }
1901         });
1902
1903         jQuery.ajax({
1904                 url: 'http://www.google.com',
1905                 success: function(){ ok( false , "success" ); },
1906                 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
1907                 complete: function() { if ( ! --i ) start(); }
1908         });
1909
1910 });
1911
1912 test("jQuery ajax - atom+xml", function() {
1913
1914         stop();
1915
1916         jQuery.ajax({
1917                 url: url( 'data/atom+xml.php' ),
1918                 success: function(){ ok( true , "success" ); },
1919                 error: function(){ ok( false , "error" ); },
1920                 complete: function() { start(); }
1921         });
1922
1923 });
1924
1925 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
1926         var success = false;
1927         try {
1928                 var xhr = jQuery.ajax({ url: window.location });
1929                 success = true;
1930                 xhr.abort();
1931         } catch (e) {}
1932
1933         ok( success, "document.location did not generate exception" );
1934 });
1935
1936 test( "jQuery.ajax - statusCode" , function() {
1937
1938         var count = 12;
1939
1940         expect( 20 );
1941         stop();
1942
1943         function countComplete() {
1944                 if ( ! --count ) {
1945                         start();
1946                 }
1947         }
1948
1949         function createStatusCodes( name , isSuccess ) {
1950                 name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
1951                 return {
1952                         200: function() {
1953                                 ok( isSuccess , name );
1954                         },
1955                         404: function() {
1956                                 ok( ! isSuccess , name );
1957                         }
1958                 };
1959         }
1960
1961         jQuery.each( {
1962                 "data/name.html": true,
1963                 "data/someFileThatDoesNotExist.html": false
1964         } , function( uri , isSuccess ) {
1965
1966                 jQuery.ajax( url( uri ) , {
1967                         statusCode: createStatusCodes( "in options" , isSuccess ),
1968                         complete: countComplete
1969                 });
1970
1971                 jQuery.ajax( url( uri ) , {
1972                         complete: countComplete
1973                 }).statusCode( createStatusCodes( "immediately with method" , isSuccess ) );
1974
1975                 jQuery.ajax( url( uri ) , {
1976                         complete: function(jXHR) {
1977                                 jXHR.statusCode( createStatusCodes( "on complete" , isSuccess ) );
1978                                 countComplete();
1979                         }
1980                 });
1981
1982                 jQuery.ajax( url( uri ) , {
1983                         complete: function(jXHR) {
1984                                 setTimeout( function() {
1985                                         jXHR.statusCode( createStatusCodes( "very late binding" , isSuccess ) );
1986                                         countComplete();
1987                                 } , 100 );
1988                         }
1989                 });
1990
1991                 jQuery.ajax( url( uri ) , {
1992                         statusCode: createStatusCodes( "all (options)" , isSuccess ),
1993                         complete: function(jXHR) {
1994                                 jXHR.statusCode( createStatusCodes( "all (on complete)" , isSuccess ) );
1995                                 setTimeout( function() {
1996                                         jXHR.statusCode( createStatusCodes( "all (very late binding)" , isSuccess ) );
1997                                         countComplete();
1998                                 } , 100 );
1999                         }
2000                 }).statusCode( createStatusCodes( "all (immediately with method)" , isSuccess ) );
2001
2002                 var testString = "";
2003
2004                 jQuery.ajax( url( uri ), {
2005                         success: function( a , b , jXHR ) {
2006                                 ok( isSuccess , "success" );
2007                                 var statusCode = {};
2008                                 statusCode[ jXHR.status ] = function() {
2009                                         testString += "B";
2010                                 };
2011                                 jXHR.statusCode( statusCode );
2012                                 testString += "A";
2013                         },
2014                         error: function( jXHR ) {
2015                                 ok( ! isSuccess , "error" );
2016                                 var statusCode = {};
2017                                 statusCode[ jXHR.status ] = function() {
2018                                         testString += "B";
2019                                 };
2020                                 jXHR.statusCode( statusCode );
2021                                 testString += "A";
2022                         },
2023                         complete: function() {
2024                                 strictEqual( testString , "AB" , "Test statusCode callbacks are ordered like " +
2025                                                 ( isSuccess ? "success" :  "error" ) + " callbacks" );
2026                                 countComplete();
2027                         }
2028                 } );
2029
2030         });
2031 });
2032
2033 test("jQuery.ajax - transitive conversions", function() {
2034
2035         expect( 8 );
2036
2037         stop();
2038
2039         jQuery.when(
2040
2041                 jQuery.ajax( url("data/json.php") , {
2042                         converters: {
2043                                 "json myjson": function( data ) {
2044                                         ok( true , "converter called" );
2045                                         return data;
2046                                 }
2047                         },
2048                         dataType: "myjson",
2049                         success: function() {
2050                                 ok( true , "Transitive conversion worked" );
2051                                 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text" );
2052                                 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType" );
2053                         }
2054                 }),
2055
2056                 jQuery.ajax( url("data/json.php") , {
2057                         converters: {
2058                                 "json myjson": function( data ) {
2059                                         ok( true , "converter called (*)" );
2060                                         return data;
2061                                 }
2062                         },
2063                         contents: false, /* headers are wrong so we ignore them */
2064                         dataType: "* myjson",
2065                         success: function() {
2066                                 ok( true , "Transitive conversion worked (*)" );
2067                                 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text (*)" );
2068                                 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType (*)" );
2069                         }
2070                 })
2071
2072         ).then( start , start );
2073
2074 });
2075
2076 test("jQuery.ajax - active counter", function() {
2077     ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
2078 });
2079
2080 }
2081
2082 //}