test runner: translated all the ok() with '==' to equals(), as it gives more informat...
[jquery.git] / test / unit / ajax.js
index 22caf2a..c8bf2ce 100644 (file)
@@ -5,6 +5,8 @@ module("ajax");
 // tests and they'll pass
 //if ( !jQuery.browser.safari ) {
 
+if ( !isLocal ) {
+
 test("$.ajax() - success callbacks", function() {
        expect( 8 );
        
@@ -38,37 +40,35 @@ test("$.ajax() - success callbacks", function() {
     }, 13);
 });
 
-if ( !isLocal ) {
-       test("$.ajax() - error callbacks", function() {
-               expect( 8 );
-               stop();
-               
-               $('#foo').ajaxStart(function(){
-                       ok( true, "ajaxStart" );
-               }).ajaxStop(function(){
-                       ok( true, "ajaxStop" );
-                       start();
-               }).ajaxSend(function(){
-                       ok( true, "ajaxSend" );
-               }).ajaxComplete(function(){
-                       ok( true, "ajaxComplete" );
-               }).ajaxError(function(){
-                       ok( true, "ajaxError" );
-               }).ajaxSuccess(function(){
-                       ok( false, "ajaxSuccess" );
-               });
-               
-               $.ajaxSetup({ timeout: 500 });
-               
-               $.ajax({
-                       url: url("data/name.php?wait=5"),
-                       beforeSend: function(){ ok(true, "beforeSend"); },
-                       success: function(){ ok(false, "success"); },
-                       error: function(){ ok(true, "error"); },
-                       complete: function(){ ok(true, "complete"); }
-               });
-       });
-}
+test("$.ajax() - error callbacks", function() {
+    expect( 8 );
+    stop();
+    
+    $('#foo').ajaxStart(function(){
+        ok( true, "ajaxStart" );
+    }).ajaxStop(function(){
+        ok( true, "ajaxStop" );
+        start();
+    }).ajaxSend(function(){
+        ok( true, "ajaxSend" );
+    }).ajaxComplete(function(){
+        ok( true, "ajaxComplete" );
+    }).ajaxError(function(){
+        ok( true, "ajaxError" );
+    }).ajaxSuccess(function(){
+        ok( false, "ajaxSuccess" );
+    });
+    
+    $.ajaxSetup({ timeout: 500 });
+    
+    $.ajax({
+        url: url("data/name.php?wait=5"),
+        beforeSend: function(){ ok(true, "beforeSend"); },
+        success: function(){ ok(false, "success"); },
+        error: function(){ ok(true, "error"); },
+        complete: function(){ ok(true, "complete"); }
+    });
+});
 
 test("$.ajax() - disabled globals", function() {
        expect( 3 );
@@ -136,6 +136,27 @@ test("$.ajax - beforeSend", function() {
        });
 });
 
+test("$.ajax - beforeSend, cancel request (#2688)", function() {
+       expect(2);
+       var request = $.ajax({
+               url: url("data/name.html"), 
+               beforeSend: function() {
+                       ok( true, "beforeSend got called, canceling" );
+                       return false;
+               },
+               success: function() {
+                       ok( false, "request didn't get canceled" );
+               },
+               complete: function() {
+                       ok( false, "request didn't get canceled" );
+               },
+               error: function() {
+                       ok( false, "request didn't get canceled" );
+               }
+       });
+       ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
+});
+
 var foobar;
 
 test("$.ajax - dataType html", function() {
@@ -146,8 +167,8 @@ test("$.ajax - dataType html", function() {
        testFoo = undefined;
 
        var verifyEvaluation = function() {
-         ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
-         ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
+         equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
+         equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
          start();
        };
 
@@ -218,26 +239,64 @@ test("synchronous request with callbacks", function() {
 });
 
 test("pass-through request object", function() {
-       expect(1);
+       expect(8);
        stop(true);
        
        var target = "data/name.html";
-       var count = 0;
+       var successCount = 0;
+       var errorCount = 0;
+  var errorEx = "";
        var success = function() {
-               // Disabled
-               //if(count++ == 5)
-               start();
+               successCount++;
        };
+       $("#foo").ajaxError(function (e, xml, s, ex) {
+               errorCount++;
+    errorEx += ": " + xml.status;
+       });
+       $("#foo").one('ajaxStop', function () {
+               equals(successCount, 5, "Check all ajax calls successful");
+               equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
+               $("#foo").unbind('ajaxError');
+               start();
+       });
        
-       /* Test disabled, too many simultaneous requests
        ok( $.get(url(target), success), "get" );
        ok( $.post(url(target), success), "post" );
        ok( $.getScript(url("data/test.js"), success), "script" );
        ok( $.getJSON(url("data/json_obj.js"), success), "json" );
-       */
        ok( $.ajax({url: url(target), success: success}), "generic" );
 });
 
+test("ajax cache", function () {
+       expect(18);
+       stop();
+       
+       var count = 0;
+
+       $("#firstp").bind("ajaxSuccess", function (e, xml, s) {
+               var re = /_=(.*?)(&|$)/g;
+    var oldOne = null;
+               for (var i = 0; i < 6; i++) {
+      var ret = re.exec(s.url);
+                       if (!ret) {
+                               break;
+                       }
+      oldOne = ret[1];
+               }
+               equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
+               ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
+               if(++count == 6)
+                       start();
+       });
+
+       ok( $.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
+       ok( $.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
+       ok( $.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
+       ok( $.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
+       ok( $.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
+       ok( $.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
+});
+
 test("global ajaxSettings", function() {
        expect(3);
 
@@ -279,6 +338,19 @@ test("load('url selector')", function() {
        });
 });
 
+test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
+       expect(1);
+       stop();
+       $.ajaxSetup({ dataType: "json" });
+       $("#first").ajaxComplete(function (e, xml, s) {
+               equals( s.dataType, "html", "Verify the load() dataType was html" );
+               $("#first").unbind("ajaxComplete");
+               $.ajaxSetup({ dataType: "" });
+               start();
+       });
+       $('#first').load("data/test3.html");
+});
+
 test("load(String, Function) - simple: inject text into DOM", function() {
        expect(2);
        stop();
@@ -294,15 +366,15 @@ test("load(String, Function) - check scripts", function() {
        window.testFoo = undefined;
        window.foobar = null;
        var verifyEvaluation = function() {
-         equals( foobar, "bar", 'Check if script src was evaluated after load' );
-         equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
-         start();
+               equals( foobar, "bar", 'Check if script src was evaluated after load' );
+               equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
+                start();
        };
        $('#first').load(url('data/test.html'), function() {
-         ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
-         equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
-         equals( testFoo, "foo", 'Check if script was evaluated after load' );
-         setTimeout(verifyEvaluation, 600);
+               ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
+               equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
+               equals( testFoo, "foo", 'Check if script was evaluated after load' );
+               setTimeout(verifyEvaluation, 600);
        });
 });
 
@@ -311,9 +383,9 @@ test("load(String, Function) - check file with only a script tag", function() {
        stop();
        testFoo = undefined;
        $('#first').load(url('data/test2.html'), function() {
-         ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
-         ok( testFoo == "foo", 'Check if script was evaluated after load' );
-         start();
+               equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
+               equals( testFoo, "foo", 'Check if script was evaluated after load' );
+               start();
        });
 });
 
@@ -334,6 +406,7 @@ test("$.get(String, Hash, Function) - parse xml and use text() on nodes", functi
 test("$.getScript(String, Function) - with callback", function() {
        expect(2);
        stop();
+       window.foobar = null;
        $.getScript(url("data/test.js"), function() {
                equals( foobar, "bar", 'Check if script was evaluated' );
                setTimeout(start, 100);
@@ -346,8 +419,6 @@ test("$.getScript(String, Function) - no callback", function() {
        $.getScript(url("data/test.js"), start);
 });
 
-if ( !isLocal ) {
-
 test("$.ajax() - JSONP, Local", function() {
        expect(7);
 
@@ -362,6 +433,10 @@ test("$.ajax() - JSONP, Local", function() {
                success: function(data){
                        ok( data.data, "JSON results returned (GET, no callback)" );
                        plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, no callback)" );
+                       plus();
                }
        });
 
@@ -371,6 +446,10 @@ test("$.ajax() - JSONP, Local", function() {
                success: function(data){
                        ok( data.data, "JSON results returned (GET, url callback)" );
                        plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, url callback)" );
+                       plus();
                }
        });
 
@@ -381,16 +460,24 @@ test("$.ajax() - JSONP, Local", function() {
                success: function(data){
                        ok( data.data, "JSON results returned (GET, data callback)" );
                        plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, data callback)" );
+                       plus();
                }
        });
 
        $.ajax({
                url: "data/jsonp.php",
                dataType: "jsonp",
-               data: { callback: "?" },
+               jsonp: "callback",
                success: function(data){
                        ok( data.data, "JSON results returned (GET, data obj callback)" );
                        plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, data obj callback)" );
+                       plus();
                }
        });
 
@@ -401,6 +488,10 @@ test("$.ajax() - JSONP, Local", function() {
                success: function(data){
                        ok( data.data, "JSON results returned (POST, no callback)" );
                        plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, data obj callback)" );
+                       plus();
                }
        });
 
@@ -412,17 +503,25 @@ test("$.ajax() - JSONP, Local", function() {
                success: function(data){
                        ok( data.data, "JSON results returned (POST, data callback)" );
                        plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (POST, data callback)" );
+                       plus();
                }
        });
 
        $.ajax({
                type: "POST",
                url: "data/jsonp.php",
-               data: { callback: "?" },
+               jsonp: "callback",
                dataType: "jsonp",
                success: function(data){
                        ok( data.data, "JSON results returned (POST, data obj callback)" );
                        plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (POST, data obj callback)" );
+                       plus();
                }
        });
 });
@@ -443,6 +542,10 @@ test("$.ajax() - JSONP, Remote", function() {
                success: function(data){
                        ok( data.data, "JSON results returned (GET, no callback)" );
                        plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, no callback)" );
+                       plus();
                }
        });
 
@@ -452,6 +555,10 @@ test("$.ajax() - JSONP, Remote", function() {
                success: function(data){
                        ok( data.data, "JSON results returned (GET, url callback)" );
                        plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, url callback)" );
+                       plus();
                }
        });
 
@@ -462,16 +569,24 @@ test("$.ajax() - JSONP, Remote", function() {
                success: function(data){
                        ok( data.data, "JSON results returned (GET, data callback)" );
                        plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, data callback)" );
+                       plus();
                }
        });
 
        $.ajax({
                url: base + "data/jsonp.php",
                dataType: "jsonp",
-               data: { callback: "?" },
+               jsonp: "callback",
                success: function(data){
                        ok( data.data, "JSON results returned (GET, data obj callback)" );
                        plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, data obj callback)" );
+                       plus();
                }
        });
 });
@@ -483,6 +598,46 @@ test("$.ajax() - script, Remote", function() {
 
        stop();
 
+       window.foobar = null;
+       $.ajax({
+               url: base + "data/test.js",
+               dataType: "script",
+               success: function(data){
+                       ok( foobar, "Script results returned (GET, no callback)" );
+                       start();
+               }
+       });
+});
+
+test("$.ajax() - script, Remote with POST", function() {
+       expect(3);
+
+       var base = window.location.href.replace(/\?.*$/, "");
+
+       stop();
+
+       window.foobar = null;
+       $.ajax({
+               url: base + "data/test.js",
+               type: "POST",
+               dataType: "script",
+               success: function(data, status){
+                       ok( foobar, "Script results returned (GET, no callback)" );
+                       equals( status, "success", "Script results returned (GET, no callback)" );
+                       start();
+               }
+       });
+});
+
+test("$.ajax() - script, Remote with scheme-less URL", function() {
+       expect(2);
+
+       var base = window.location.href.replace(/\?.*$/, "");
+       base = base.replace(/^.*?\/\//, "//");
+
+       stop();
+
+       window.foobar = null;
        $.ajax({
                url: base + "data/test.js",
                dataType: "script",
@@ -497,10 +652,10 @@ test("$.getJSON(String, Hash, Function) - JSON array", function() {
        expect(4);
        stop();
        $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
-         ok( json[0].name == 'John', 'Check JSON: first, name' );
-         ok( json[0].age == 21, 'Check JSON: first, age' );
-         ok( json[1].name == 'Peter', 'Check JSON: second, name' );
-         ok( json[1].age == 25, 'Check JSON: second, age' );
+         equals( json[0].name, 'John', 'Check JSON: first, name' );
+         equals( json[0].age, 21, 'Check JSON: first, age' );
+         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
+         equals( json[1].age, 25, 'Check JSON: second, age' );
          start();
        });
 });
@@ -509,19 +664,39 @@ test("$.getJSON(String, Function) - JSON object", function() {
        expect(2);
        stop();
        $.getJSON(url("data/json.php"), function(json) {
-         ok( json.data.lang == 'en', 'Check JSON: lang' );
-         ok( json.data.length == 25, 'Check JSON: length' );
+         equals( json.data.lang, 'en', 'Check JSON: lang' );
+         equals( json.data.length, 25, 'Check JSON: length' );
          start();
        });
 });
 
-test("$.post(String, Hash, Function) - simple with xml", function() {
+test("$.getJSON(String, Function) - JSON object with absolute url to local content", function() {
        expect(2);
+
+       var base = window.location.href.replace(/\?.*$/, "");
+
+       stop();
+       $.getJSON(url(base + "data/json.php"), function(json) {
+         equals( json.data.lang, 'en', 'Check JSON: lang' );
+         equals( json.data.length, 25, 'Check JSON: length' );
+         start();
+       });
+});
+
+test("$.post(String, Hash, Function) - simple with xml", function() {
+       expect(4);
        stop();
        $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
          $('math', xml).each(function() {
-                   ok( $('calculation', this).text() == '5-2', 'Check for XML' );
-                   ok( $('result', this).text() == '3', 'Check for XML' );
+                   equals( $('calculation', this).text(), '5-2', 'Check for XML' );
+                   equals( $('result', this).text(), '3', 'Check for XML' );
+                });
+       });
+
+       $.post(url("data/name.php?xml=5-2"), {}, function(xml){
+         $('math', xml).each(function() {
+                   equals( $('calculation', this).text(), '5-2', 'Check for XML' );
+                   equals( $('result', this).text(), '3', 'Check for XML' );
                 });
          start();
        });
@@ -590,7 +765,7 @@ test("$.ajax - simple get", function() {
          type: "GET",
          url: url("data/name.php?name=foo"),
          success: function(msg){
-           ok( msg == 'bar', 'Check for GET' );
+           equals( msg, 'bar', 'Check for GET' );
            start();
          }
        });
@@ -604,7 +779,7 @@ test("$.ajax - simple post", function() {
          url: url("data/name.php"),
          data: "name=peter",
          success: function(msg){
-           ok( msg == 'pan', 'Check for POST' );
+           equals( msg, 'pan', 'Check for POST' );
            start();
          }
        });
@@ -616,7 +791,7 @@ test("ajaxSetup()", function() {
        $.ajaxSetup({
                url: url("data/name.php?name=foo"),
                success: function(msg){
-               ok( msg == 'bar', 'Check for GET' );
+               equals( msg, 'bar', 'Check for GET' );
                        start();
                }
        });