Added test for sync requests with callbacks (#534)
[jquery.git] / src / ajax / ajaxTest.js
index 0462aad..3159fb8 100644 (file)
@@ -1,10 +1,59 @@
 module("ajax");
 
-test("load(String, Object, Function) - simple: inject text into DOM", function() {
+test("serialize()", function() {
        expect(1);
+       var data = $(':input').not('button').serialize();
+       // ignore button, IE takes text content as value, not relevant for this test
+       ok( data == 'action=Test&text2=Test&radio1=on&radio2=on&check=on&=on&hidden=&foo[bar]=&name=name&=foobar&select1=&select2=3&select3=1', 'Check form serialization as query string' );
+});
+
+test("param", function() {
+       expect(4);
+       var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
+       ok( $.param(params) == "foo=bar&baz=42&quux=All%20your%20base%20are%20belong%20to%20us", "simple" );
+       
+       params = {someName: [1, 2, 3], regularThing: "blah" };
+       ok( $.param(params) == "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
+       
+       params = {"foo[]":["baz", 42, "All your base are belong to us"]};
+       ok( $.param(params) == "foo[]=baz&foo[]=42&foo[]=All%20your%20base%20are%20belong%20to%20us", "more array" );
+       
+       params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
+       ok( $.param(params) == "foo[bar]=baz&foo[beep]=42&foo[quux]=All%20your%20base%20are%20belong%20to%20us", "even more arrays" );
+});
+
+test("pass-through request object", function() {
+       expect(7);
+       stop();
+       var count = 0;
+       var success = function() {
+               if(count++ == 6)
+                       start();
+       }
+       var url = "data/name.php";
+       ok( $.get(url, success), "get" );
+       ok( $.getIfModified(url, success), "getIfModified" );
+       ok( $.post(url, success), "post" );
+       ok( $.getScript("data/test.js", success), "script" );
+       ok( $.getJSON("data/json.php", success), "json" );
+       ok( $.ajax({url: url, success: success}), "generic" );
+});
+
+test("synchronous request", function() {
+       ok( /^{ "data"/.test( $.ajax({url: "data/json.php", async: false}).responseText ), "check returned text" );
+});
+
+test("synchronous request with callbacks", function() {
+       var result;
+       $.ajax({url: "data/json.php", async: false, success: function(data) { result = data; }});
+       ok( /^{ "data"/.test( result ), "check returned text" );
+});
+
+test("load(String, Object, Function) - simple: inject text into DOM", function() {
+       expect(2);
        stop();
        $('#first').load("data/name.php", function() {
-               ok( $('#first').text() == 'ERROR', 'Check if content was injected into the DOM' );
+               ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
                start();
        });
 });
@@ -13,17 +62,12 @@ test("load(String, Object, Function) - inject without callback", function() {
        expect(1);
        stop(); // check if load can be called with only url
        $('#first').load("data/name.php");
-       $.get("data/name.php", function() {
-         ok( $('#first').text() == 'ERROR', 'Check if load works without callback');
-         start();
-       });
 });
 
 test("load(String, Object, Function) - check scripts", function() {
-       expect(6);
+       expect(7);
        stop();
-       window.foobar = undefined;
-       window.foo = undefined;
+       testFoo = undefined;
        var verifyEvaluation = function() {
          ok( foobar == "bar", 'Check if script src was evaluated after load' );
          ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
@@ -32,60 +76,61 @@ test("load(String, Object, Function) - check scripts", function() {
        };
        $('#first').load('data/test.html', function() {
          ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
-         ok( foo == "foo", 'Check if script was evaluated after load' );
+         ok( testFoo == "foo", 'Check if script was evaluated after load' );
          setTimeout(verifyEvaluation, 600);
        });
 });
 
-test("serialize()", function() {
-       expect(1);
-       var data = $(':input').not('button').serialize();
-       // ignore button, IE takes text content as value, not relevant for this test
-       ok( data == 'action=Test&text2=Test&radio1=on&radio2=on&check=on&=on&hidden=&foo[bar]=&name=name&=foobar&select1=&select2=3&select3=1', 'Check form serialization as query string' );
-});
-
 test("test global handlers - success", function() {
-       expect(6);
+       expect(8);
        stop();
-       var counter = { complete: 0, success: 0, error: 0 },
+       var counter = { complete: 0, success: 0, error: 0, send: 0 },
                success = function() { counter.success++ },
                error = function() { counter.error++ },
-               complete = function() { counter.complete++ };
+               complete = function() { counter.complete++ },
+               send = function() { counter.send++ };
 
-       $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
+       $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
        // start with successful test
-       $.ajax({url: "data/name.php", success: success, error: error, complete: function() {
+       $.ajax({url: "data/name.php", beforeSend: send, success: success, error: error, complete: function() {
          ok( counter.error == 0, 'Check succesful request' );
          ok( counter.success == 2, 'Check succesful request' );
          ok( counter.complete == 3, 'Check succesful request' );
-         counter.error = counter.success = counter.complete = 0;
+         ok( counter.send == 2, 'Check succesful request' );
+         counter.error = counter.success = counter.complete = counter.send = 0;
          $.ajaxTimeout(500);
-         $.ajax({url: "data/name.php?wait=5", success: success, error: error, complete: function() {
+         $.ajax({url: "data/name.php?wait=5", beforeSend: send, success: success, error: error, complete: function() {
            ok( counter.error == 2, 'Check failed request' );
            ok( counter.success == 0, 'Check failed request' );
            ok( counter.complete == 3, 'Check failed request' );
+           ok( counter.send == 2, 'Check failed request' );
            start();
          }});
        }});
 });
 
 test("test global handlers - failure", function() {
-       expect(6);
+       expect(8);
        stop();
-       var counter = { complete: 0, success: 0, error: 0 },
+       var counter = { complete: 0, success: 0, error: 0, send: 0 },
                success = function() { counter.success++ },
-               error = function() { counter.error++ };
+               error = function() { counter.error++ },
+               complete = function() { counter.complete++ },
+               send = function() { counter.send++ };
        $.ajaxTimeout(0);
-       $.ajax({url: "data/name.php", global: false, success: success, error: error, complete: function() {
+       $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
+       $.ajax({url: "data/name.php", global: false, beforeSend: send, success: success, error: error, complete: function() {
          ok( counter.error == 0, 'Check sucesful request without globals' );
          ok( counter.success == 1, 'Check sucesful request without globals' );
          ok( counter.complete == 0, 'Check sucesful request without globals' );
-         counter.error = counter.success = counter.complete = 0;
+         ok( counter.send == 1, 'Check sucesful request without globals' );
+         counter.error = counter.success = counter.complete = counter.send = 0;
          $.ajaxTimeout(500);
-         $.ajax({url: "data/name.php?wait=5", global: false, success: success, error: error, complete: function() {
+         $.ajax({url: "data/name.php?wait=5", global: false, beforeSend: send, success: success, error: error, complete: function() {
             ok( counter.error == 1, 'Check failed request without globals' );
             ok( counter.success == 0, 'Check failed request without globals' );
             ok( counter.complete == 0, 'Check failed request without globals' );
+            ok( counter.send == 1, 'Check failed request without globals' );
             start();
          }});
        }});
@@ -109,7 +154,7 @@ test("$.getIfModified(String, Hash, Function)", function() {
        expect(1);
        stop();
        $.getIfModified("data/name.php", function(msg) {
-           ok( msg == 'ERROR', 'Check ifModified' );
+           ok( /^ERROR/.test(msg), 'Check ifModified' );
            start();
        });
 });
@@ -119,7 +164,7 @@ test("$.getScript(String, Function) - with callback", function() {
        stop();
        $.getScript("data/test.js", function() {
                ok( foobar == "bar", 'Check if script was evaluated' );
-               start();
+               setTimeout(start, 100);
        });
 });
 
@@ -238,10 +283,9 @@ test("$.ajax - simple post", function() {
 });
        
 test("$.ajax - dataType html", function() {
-       expect(4);
+       expect(5);
        stop();
-       window.foobar = undefined;
-       window.foo = undefined;
+       testFoo = undefined;
        var verifyEvaluation = function() {
          ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
          start();
@@ -251,7 +295,7 @@ test("$.ajax - dataType html", function() {
          url: "data/test.html",
          success: function(data) {
            ok( data.match(/^html text/), 'Check content for datatype html' );
-           ok( foo == "foo", 'Check if script was evaluated for datatype html' );
+           ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
            setTimeout(verifyEvaluation, 600);
          }
        });
@@ -268,4 +312,21 @@ test("$.ajax - xml: non-namespace elements inside namespaced elements", function
            start();
          }
        });
+});
+
+test("$.ajax - beforeSend", function() {
+       expect(1);
+       stop();
+       var customHeader = "value";
+       $.ajax({
+               url: "data/name.php", 
+               data: {'req': true},
+               beforeSend: function(xml) {
+                       xml.setRequestHeader('X-Custom-Header', customHeader);
+               },
+               success: function(data) {
+                       ok( data == customHeader, "check return value, should be the custom header sent" );
+                       start();
+               }
+       });
 });
\ No newline at end of file