Fixes #2994. Not finding a transport now fires the error callbacks and doesn't make...
[jquery.git] / test / unit / ajax.js
index d5f2bc0..49196cb 100644 (file)
@@ -240,6 +240,46 @@ test("jQuery.ajax() - error callbacks", function() {
        });
 });
 
+test("jQuery.ajax() - responseText on error", function() {
+
+       expect( 1 );
+
+       stop();
+
+       jQuery.ajax({
+               url: url("data/errorWithText.php"),
+               error: function(xhr) {
+                       strictEqual( xhr.responseText , "plain text message" , "Test jXHR.responseText is filled for HTTP errors" );
+               },
+               complete: function() {
+                       start();
+               }
+       });
+});
+
+test(".ajax() - retry with jQuery.ajax( this )", function() {
+
+       expect( 1 );
+
+       stop();
+
+       var firstTime = 1;
+
+       jQuery.ajax({
+               url: url("data/errorWithText.php"),
+               error: function() {
+                       if ( firstTime ) {
+                               firstTime = 0;
+                               jQuery.ajax( this );
+                       } else {
+                               ok( true , "Test retrying with jQuery.ajax(this) works" );
+                               start();
+                       }
+               }
+       });
+
+});
+
 test(".ajax() - headers" , function() {
 
        expect( 2 );
@@ -276,6 +316,28 @@ test(".ajax() - headers" , function() {
 
 });
 
+test(".ajax() - Accept header" , function() {
+
+       expect( 1 );
+
+       stop();
+
+       jQuery.ajax(url("data/headers.php?keys=accept"), {
+               headers: {
+                       Accept: "very wrong accept value"
+               },
+               beforeSend: function( xhr ) {
+                       xhr.setRequestHeader( "Accept", "*/*" );
+               },
+               success: function( data ) {
+                       strictEqual( data , "accept: */*\n" , "Test Accept header is set to last value provided" );
+                       start();
+               },
+               error: function(){ ok(false, "error"); }
+       });
+
+});
+
 test(".ajax() - contentType" , function() {
 
        expect( 2 );
@@ -341,6 +403,53 @@ test(".ajax() - hash", function() {
        });
 });
 
+test("jQuery ajax - cross-domain detection", function() {
+
+       expect( 4 );
+
+       var loc = document.location,
+               otherPort = loc.port === 666 ? 667 : 666,
+               otherProtocol = loc.protocol === "http:" ? "https:" : "http:";
+
+       jQuery.ajax({
+               dataType: "jsonp",
+               url: otherProtocol + "//" + loc.host,
+               beforeSend: function( _ , s ) {
+                       ok( s.crossDomain , "Test different protocols are detected as cross-domain" );
+                       return false;
+               }
+       });
+
+       jQuery.ajax({
+               dataType: "jsonp",
+               url: loc.protocol + '//somewebsitethatdoesnotexist-656329477541.com:' + ( loc.port || 80 ),
+               beforeSend: function( _ , s ) {
+                       ok( s.crossDomain , "Test different hostnames are detected as cross-domain" );
+                       return false;
+               }
+       });
+
+       jQuery.ajax({
+               dataType: "jsonp",
+               url: loc.protocol + "//" + loc.hostname + ":" + otherPort,
+               beforeSend: function( _ , s ) {
+                       ok( s.crossDomain , "Test different ports are detected as cross-domain" );
+                       return false;
+               }
+       });
+
+       jQuery.ajax({
+               dataType: "jsonp",
+               url: loc.protocol + "//" + loc.host,
+               crossDomain: true,
+               beforeSend: function( _ , s ) {
+                       ok( s.crossDomain , "Test forced crossDomain is detected as cross-domain" );
+                       return false;
+               }
+       });
+
+});
+
 test(".ajax() - 304", function() {
        expect( 1 );
        stop();
@@ -938,6 +1047,18 @@ test("load(String, Function) - check file with only a script tag", function() {
        });
 });
 
+test("load(String, Function) - dataFilter in ajaxSettings", function() {
+       expect(2);
+       stop();
+       jQuery.ajaxSetup({ dataFilter: function() { return "Hello World"; } });
+       var div = jQuery("<div/>").load(url("data/name.html"), function(responseText) {
+               strictEqual( div.html(), "Hello World" , "Test div was filled with filtered data" );
+               strictEqual( responseText, "Hello World" , "Test callback receives filtered data" );
+               jQuery.ajaxSetup({ dataFilter: 0 });
+               start();
+       });
+});
+
 test("load(String, Object, Function)", function() {
        expect(2);
        stop();
@@ -994,10 +1115,10 @@ test("jQuery.getScript(String, Function) - no callback", function() {
 });
 
 test("jQuery.ajax() - JSONP, Local", function() {
-       expect(9);
+       expect(14);
 
        var count = 0;
-       function plus(){ if ( ++count == 9 ) start(); }
+       function plus(){ if ( ++count == 14 ) start(); }
 
        stop();
 
@@ -1042,6 +1163,75 @@ test("jQuery.ajax() - JSONP, Local", function() {
        });
 
        jQuery.ajax({
+               url: "data/jsonp.php?callback=??",
+               dataType: "jsonp",
+               success: function(data){
+                       ok( data.data, "JSON results returned (GET, url context-free callback)" );
+                       plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, url context-free callback)" );
+                       plus();
+               }
+       });
+
+       jQuery.ajax({
+               url: "data/jsonp.php",
+               dataType: "jsonp",
+               data: "callback=??",
+               success: function(data){
+                       ok( data.data, "JSON results returned (GET, data context-free callback)" );
+                       plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, data context-free callback)" );
+                       plus();
+               }
+       });
+
+       jQuery.ajax({
+               url: "data/jsonp.php/??",
+               dataType: "jsonp",
+               success: function(data){
+                       ok( data.data, "JSON results returned (GET, REST-like)" );
+                       plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, REST-like)" );
+                       plus();
+               }
+       });
+
+       jQuery.ajax({
+               url: "data/jsonp.php/???json=1",
+               dataType: "jsonp",
+               success: function(data){
+                       strictEqual( jQuery.type(data), "array", "JSON results returned (GET, REST-like with param)" );
+                       plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, REST-like with param)" );
+                       plus();
+               }
+       });
+
+       jQuery.ajax({
+               url: "data/jsonp.php",
+               dataType: "jsonp",
+               data: {
+                       callback: "?"
+               },
+               success: function(data){
+                       ok( data.data, "JSON results returned (GET, processed data callback)" );
+                       plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, processed data callback)" );
+                       plus();
+               }
+       });
+
+       jQuery.ajax({
                url: "data/jsonp.php",
                dataType: "jsonp",
                jsonp: "callback",
@@ -1675,25 +1865,19 @@ test("jQuery ajax - failing cross-domain", function() {
 
        var i = 2;
 
-       if ( jQuery.ajax({
+       jQuery.ajax({
                url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
                success: function(){ ok( false , "success" ); },
                error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
                complete: function() { if ( ! --i ) start(); }
-       }) === false ) {
-               ok( true , "no transport" );
-               if ( ! --i ) start();
-       }
+       });
 
-       if ( jQuery.ajax({
+       jQuery.ajax({
                url: 'http://www.google.com',
                success: function(){ ok( false , "success" ); },
                error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
                complete: function() { if ( ! --i ) start(); }
-       }) === false ) {
-               ok( true , "no transport" );
-               if ( ! --i ) start();
-       }
+       });
 
 });
 
@@ -1725,6 +1909,76 @@ test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
        ok( success, "document.location did not generate exception" );
 });
 
+test( "jQuery.ajax - statusCode" , function() {
+
+       var count = 10;
+
+       expect( 16 );
+       stop();
+
+       function countComplete() {
+               if ( ! --count ) {
+                       start();
+               }
+       }
+
+       function createStatusCodes( name , isSuccess ) {
+               name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
+               return {
+                       200: function() {
+                               ok( isSuccess , name );
+                       },
+                       404: function() {
+                               ok( ! isSuccess , name );
+                       }
+               };
+       }
+
+       jQuery.each( {
+               "data/name.html": true,
+               "data/someFileThatDoesNotExist.html": false
+       } , function( uri , isSuccess ) {
+
+               jQuery.ajax( url( uri ) , {
+                       statusCode: createStatusCodes( "in options" , isSuccess ),
+                       complete: countComplete
+               });
+
+               jQuery.ajax( url( uri ) , {
+                       complete: countComplete
+               }).statusCode( createStatusCodes( "immediately with method" , isSuccess ) );
+
+               jQuery.ajax( url( uri ) , {
+                       complete: function(jXHR) {
+                               jXHR.statusCode( createStatusCodes( "on complete" , isSuccess ) );
+                               countComplete();
+                       }
+               });
+
+               jQuery.ajax( url( uri ) , {
+                       complete: function(jXHR) {
+                               setTimeout( function() {
+                                       jXHR.statusCode( createStatusCodes( "very late binding" , isSuccess ) );
+                                       countComplete();
+                               } , 100 );
+                       }
+               });
+
+               jQuery.ajax( url( uri ) , {
+                       statusCode: createStatusCodes( "all (options)" , isSuccess ),
+                       complete: function(jXHR) {
+                               jXHR.statusCode( createStatusCodes( "all (on complete)" , isSuccess ) );
+                               setTimeout( function() {
+                                       jXHR.statusCode( createStatusCodes( "all (very late binding)" , isSuccess ) );
+                                       countComplete();
+                               } , 100 );
+                       }
+               }).statusCode( createStatusCodes( "all (immediately with method)" , isSuccess ) );
+
+       });
+
+});
+
 }
 
 //}
\ No newline at end of file