+ 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);
+});