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