+test("jQuery.ajax() - success/error callbacks (remote)", function() {
+
+ var supports = jQuery.support.cors;
+
+ expect( supports ? 9 : 6 );
+
+ jQuery.ajaxSetup({ timeout: 0 });
+
+ stop();
+
+ setTimeout(function(){
+ jQuery('#foo').ajaxStart(function(){
+ ok( true, "ajaxStart" );
+ }).ajaxStop(function(){
+ ok( true, "ajaxStop" );
+ start();
+ }).ajaxSend(function(){
+ ok( supports , "ajaxSend" );
+ }).ajaxComplete(function(){
+ ok( true, "ajaxComplete" );
+ }).ajaxError(function(){
+ ok( ! supports, "ajaxError" );
+ }).ajaxSuccess(function(){
+ ok( supports, "ajaxSuccess" );
+ });
+
+ jQuery.ajax({
+ // JULIAN TODO: Get an url especially for jQuery
+ url: "http://rockstarapps.com/test.php",
+ dataType: "text",
+ beforeSend: function(){ ok(supports, "beforeSend"); },
+ success: function( val ){ ok(supports, "success"); ok(supports && val.length, "data received"); },
+ error: function(_ , a , b ){ ok(!supports, "error"); },
+ complete: function(){ ok(true, "complete"); }
+ });
+ }, 13);
+});
+
+test("jQuery.ajax() - success callbacks (late binding)", function() {
+ expect( 8 );
+
+ jQuery.ajaxSetup({ timeout: 0 });
+
+ stop();
+
+ setTimeout(function(){
+ jQuery('#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( false, "ajaxError" );
+ }).ajaxSuccess(function(){
+ ok( true, "ajaxSuccess" );
+ });
+
+ jQuery.ajax({
+ url: url("data/name.html"),
+ beforeSend: function(){ ok(true, "beforeSend"); }
+ })
+ .complete(function(){ ok(true, "complete"); })
+ .success(function(){ ok(true, "success"); })
+ .error(function(){ ok(false, "error"); });
+ }, 13);
+});
+
+test("jQuery.ajax() - success callbacks (oncomplete binding)", function() {
+ expect( 8 );
+
+ jQuery.ajaxSetup({ timeout: 0 });
+
+ stop();
+
+ setTimeout(function(){
+ jQuery('#foo').ajaxStart(function(){
+ ok( true, "ajaxStart" );
+ }).ajaxStop(function(){
+ ok( true, "ajaxStop" );
+ }).ajaxSend(function(){
+ ok( true, "ajaxSend" );
+ }).ajaxComplete(function(){
+ ok( true, "ajaxComplete" );
+ }).ajaxError(function(){
+ ok( false, "ajaxError" );
+ }).ajaxSuccess(function(){
+ ok( true, "ajaxSuccess" );
+ });
+
+ jQuery.ajax({
+ url: url("data/name.html"),
+ beforeSend: function(){ ok(true, "beforeSend"); },
+ complete: function(xhr) {
+ xhr
+ .complete(function(){ ok(true, "complete"); })
+ .success(function(){ ok(true, "success"); })
+ .error(function(){ ok(false, "error"); })
+ .complete(function(){ start(); });
+ }
+ })
+ }, 13);
+});
+
+test("jQuery.ajax() - success callbacks (very late binding)", function() {
+ expect( 8 );
+
+ jQuery.ajaxSetup({ timeout: 0 });
+
+ stop();
+
+ setTimeout(function(){
+ jQuery('#foo').ajaxStart(function(){
+ ok( true, "ajaxStart" );
+ }).ajaxStop(function(){
+ ok( true, "ajaxStop" );
+ }).ajaxSend(function(){
+ ok( true, "ajaxSend" );
+ }).ajaxComplete(function(){
+ ok( true, "ajaxComplete" );
+ }).ajaxError(function(){
+ ok( false, "ajaxError" );
+ }).ajaxSuccess(function(){
+ ok( true, "ajaxSuccess" );
+ });
+
+ jQuery.ajax({
+ url: url("data/name.html"),
+ beforeSend: function(){ ok(true, "beforeSend"); },
+ complete: function(xhr) {
+ setTimeout (function() {
+ xhr
+ .complete(function(){ ok(true, "complete"); })
+ .success(function(){ ok(true, "success"); })
+ .error(function(){ ok(false, "error"); })
+ .complete(function(){ start(); });
+ },100);
+ }
+ })
+ }, 13);
+});
+
+test("jQuery.ajax() - success callbacks (order)", function() {
+ expect( 1 );
+
+ jQuery.ajaxSetup({ timeout: 0 });
+
+ stop();
+
+ var testString = "";
+
+ setTimeout(function(){
+ jQuery.ajax({
+ url: url("data/name.html"),
+ success: function( _1 , _2 , xhr ) {
+ xhr.success(function() {
+ xhr.success(function() {
+ testString += "E";
+ });
+ testString += "D";
+ });
+ testString += "A";
+ },
+ complete: function() {
+ strictEqual(testString, "ABCDE", "Proper order");
+ start();
+ }
+ }).success(function() {
+ testString += "B";
+ }).success(function() {
+ testString += "C";
+ });
+ }, 13);
+});
+
+test("jQuery.ajax() - error callbacks", function() {
+ expect( 8 );
+ stop();
+
+ jQuery('#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" );
+ });
+
+ jQuery.ajaxSetup({ timeout: 500 });
+
+ jQuery.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() - headers" , function() {
+
+ expect( 2 );
+
+ stop();
+
+ var requestHeaders = {
+ siMPle: "value",
+ "SometHing-elsE": "other value",
+ OthEr: "something else"
+ },
+ list = [],
+ i;
+
+ for( i in requestHeaders ) {
+ list.push( i );
+ }
+
+ jQuery.ajax(url("data/headers.php?keys="+list.join( "_" ) ), {
+ headers: requestHeaders,
+ success: function( data , _ , xhr ) {
+ var tmp = [];
+ for ( i in requestHeaders ) {
+ tmp.push( i , ": " , requestHeaders[ i ] , "\n" );
+ }
+ tmp = tmp.join( "" );
+
+ equals( data , tmp , "Headers were sent" );
+ equals( xhr.getResponseHeader( "Sample-Header" ) , "Hello World" , "Sample header received" );
+ start();
+ },
+ error: function(){ ok(false, "error"); }
+ });
+
+});
+
+test(".ajax() - hash", function() {
+ expect(3);
+
+ jQuery.ajax({
+ url: "data/name.html#foo",
+ beforeSend: function( xhr, settings ) {
+ equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
+ return false;
+ }
+ });
+
+ jQuery.ajax({
+ url: "data/name.html?abc#foo",
+ beforeSend: function( xhr, settings ) {
+ equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
+ return false;
+ }
+ });
+
+ jQuery.ajax({
+ url: "data/name.html?abc#foo",
+ data: { "test": 123 },
+ beforeSend: function( xhr, settings ) {
+ equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
+ return false;
+ }
+ });
+});
+
+test(".ajax() - 304", function() {
+ expect( 1 );
+ stop();
+
+ jQuery.ajax({
+ url: url("data/notmodified.php"),
+ success: function(){ ok(true, "304 ok"); },
+ // Do this because opera simply refuses to implement 304 handling :(
+ // A feature-driven way of detecting this would be appreciated
+ // See: http://gist.github.com/599419
+ error: function(){ ok(jQuery.browser.opera, "304 not ok "); },
+ complete: function(xhr){ start(); }
+ });
+});
+
+test(".load()) - 404 error callbacks", function() {
+ expect( 6 );
+ stop();
+
+ jQuery('#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" );
+ });
+
+ jQuery("<div/>").load("data/404.html", function(){
+ ok(true, "complete");
+ });
+});
+
+test("jQuery.ajax() - abort", function() {
+ expect( 8 );
+ stop();
+
+ jQuery('#foo').ajaxStart(function(){
+ ok( true, "ajaxStart" );
+ }).ajaxStop(function(){
+ ok( true, "ajaxStop" );
+ start();
+ }).ajaxSend(function(){
+ ok( true, "ajaxSend" );
+ }).ajaxComplete(function(){
+ ok( true, "ajaxComplete" );
+ });
+
+ var xhr = jQuery.ajax({
+ url: url("data/name.php?wait=5"),
+ beforeSend: function(){ ok(true, "beforeSend"); },
+ complete: function(){ ok(true, "complete"); }
+ });
+
+ equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
+
+ xhr.abort();
+ equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
+});
+
+test("jQuery.ajax() - readyState (success)", function() {
+ expect( 1 );
+
+ jQuery.ajaxSetup({ timeout: 0 });
+
+ stop();
+
+ var control = "";
+
+ setTimeout(function(){
+ jQuery.ajax({
+ url: url("data/name.html"),
+ beforeSend: function( xhr ) {
+ xhr.onreadystatechange = function() {
+ control += xhr.readyState;
+ }
+ },
+ complete: function(){
+ setTimeout( function() {
+ equals( control , "1234" , "onreadystatechange was properly called" );
+ }, 13 );
+ start();
+ }
+ });
+ }, 13);
+});
+
+test("jQuery.ajax() - readyState (abort)", function() {
+ expect( 2 );
+
+ jQuery.ajaxSetup({ timeout: 0 });
+
+ stop();
+
+ var control = "";
+
+ setTimeout(function(){
+
+ jQuery.ajaxSetup({ timeout: 500 });
+
+ jQuery.ajax({
+ url: url("data/name.php?wait=5"),
+ beforeSend: function( xhr ) {
+ xhr.onreadystatechange = function() {
+ control += xhr.readyState;
+ }
+ },
+ complete: function( xhr ){
+ setTimeout( function() {
+ equals( control , "14" , "onreadystatechange was properly called" );
+ equals( xhr.readyState, 0 , "readyState is 0" );
+ }, 13 );
+ start();
+ }
+ });
+ }, 13);
+});
+
+test("jQuery.xhr() - reuse", function() {
+ expect( 15 );
+
+ jQuery.ajaxSetup({ timeout: 0 });
+
+ stop();
+
+ var number = 0;
+
+ setTimeout(function(){
+ jQuery('#foo').ajaxStart(function(){
+ ok( true, "ajaxStart" );
+ }).ajaxStop(function(){
+ ok( true, "ajaxStop" );
+ start();
+ }).ajaxSend(function(){
+ number++;
+ ok( true, "ajaxSend (" + number +")" );
+ }).ajaxComplete(function(){
+ ok( true, "ajaxComplete (" + number +")" );
+ }).ajaxError(function(){
+ ok( false, "ajaxError (" + number +")" );
+ }).ajaxSuccess(function(){
+ ok( true, "ajaxSuccess (" + number +")" );
+ });
+
+ jQuery.ajax({
+ url: url("data/name.html"),
+ beforeSend: function(){ ok(true, "beforeSend (1)"); },
+ success: function( _1 , _2 , xhr ){
+ ok(true, "success (1)");
+ xhr.complete(function() {
+ ok(true, "complete (1bis)");
+ });
+ xhr.open( "GET", url("data/name.html") );
+ xhr.success( function(){ ok(true, "beforeSend (2)"); } )
+ xhr.send( null, {
+ success: function(){ ok(true, "success (2)"); },
+ error: function(){ ok(false, "error (2)"); },
+ complete: function(){ ok(true, "complete (2)"); }
+ } );
+ },
+ error: function(){ ok(false, "error (1)"); },
+ complete: function(){ ok(true, "complete (1)"); }
+ });
+ }, 13);
+});
+
+test("jQuery.xhr() - early binding", function() {
+ expect( 2 );
+
+ jQuery.ajaxSetup({ timeout: 0 });
+
+ stop();
+
+ jQuery.xhr()
+ .success( function(){ ok(true, "success"); } )
+ .error( function(){ ok(false, "error"); } )
+ .complete( function(){ ok(true, "complete"); start(); } )
+ .open( "GET", url("data/name.html") )
+ .send();
+});
+
+test("jQuery.xhr() - get native implementation", function() {
+
+ var xhr = jQuery.xhr(true);
+
+ ok( xhr.readyState !== undefined , "implements XMLHttpRequest" );
+ ok( ! jQuery.isFunction( xhr.success ) , "is not jQuery's abstraction" );
+
+});
+
+test("Ajax events with context", function() {
+ expect(14);
+
+ stop();
+ var context = document.createElement("div");
+
+ function event(e){
+ equals( this, context, e.type );
+ }
+
+ function callback(msg){
+ return function(){
+ equals( this, context, "context is preserved on callback " + msg );
+ };
+ }
+
+ function nocallback(msg){
+ return function(){
+ equals( typeof this.url, "string", "context is settings on callback " + msg );
+ };
+ }
+
+ jQuery('#foo').add(context)
+ .ajaxSend(event)
+ .ajaxComplete(event)
+ .ajaxError(event)
+ .ajaxSuccess(event);
+
+ jQuery.ajax({
+ url: url("data/name.html"),
+ beforeSend: callback("beforeSend"),
+ success: callback("success"),
+ error: callback("error"),
+ complete:function(){
+ callback("complete").call(this);
+
+ jQuery.ajax({
+ url: url("data/404.html"),
+ context: context,
+ beforeSend: callback("beforeSend"),
+ error: callback("error"),
+ complete: function(){
+ callback("complete").call(this);
+
+ jQuery('#foo').add(context).unbind();
+
+ jQuery.ajax({
+ url: url("data/404.html"),
+ beforeSend: nocallback("beforeSend"),
+ error: nocallback("error"),
+ complete: function(){
+ nocallback("complete").call(this);
+ start();
+ }
+ });
+ }
+ });
+ },
+ context:context
+ });
+});
+
+test("jQuery.ajax context modification", function() {
+ expect(1);