michael@0: module("ajax"); michael@0: michael@0: // Safari 3 randomly crashes when running these tests, michael@0: // but only in the full suite - you can run just the Ajax michael@0: // tests and they'll pass michael@0: //if ( !jQuery.browser.safari ) { michael@0: michael@0: if ( !isLocal ) { michael@0: michael@0: test("$.ajax() - success callbacks", function() { michael@0: expect( 8 ); michael@0: michael@0: $.ajaxSetup({ timeout: 0 }); michael@0: michael@0: stop(); michael@0: michael@0: setTimeout(function(){ michael@0: $('#foo').ajaxStart(function(){ michael@0: ok( true, "ajaxStart" ); michael@0: }).ajaxStop(function(){ michael@0: ok( true, "ajaxStop" ); michael@0: start(); michael@0: }).ajaxSend(function(){ michael@0: ok( true, "ajaxSend" ); michael@0: }).ajaxComplete(function(){ michael@0: ok( true, "ajaxComplete" ); michael@0: }).ajaxError(function(){ michael@0: ok( false, "ajaxError" ); michael@0: }).ajaxSuccess(function(){ michael@0: ok( true, "ajaxSuccess" ); michael@0: }); michael@0: michael@0: $.ajax({ michael@0: url: url("data/name.html"), michael@0: beforeSend: function(){ ok(true, "beforeSend"); }, michael@0: success: function(){ ok(true, "success"); }, michael@0: error: function(){ ok(false, "error"); }, michael@0: complete: function(){ ok(true, "complete"); } michael@0: }); michael@0: }, 13); michael@0: }); michael@0: michael@0: /* mozilla: the ajaxSuccess part fails intermittently on MacOSX michael@0: michael@0: test("$.ajax() - error callbacks", function() { michael@0: expect( 7 ); michael@0: stop(); michael@0: michael@0: $('#foo').ajaxStart(function(){ michael@0: ok( true, "ajaxStart" ); michael@0: }).ajaxStop(function(){ michael@0: ok( true, "ajaxStop" ); michael@0: start(); michael@0: }).ajaxSend(function(){ michael@0: ok( true, "ajaxSend" ); michael@0: }).ajaxComplete(function(){ michael@0: ok( true, "ajaxComplete" ); michael@0: }).ajaxError(function(){ michael@0: ok( true, "ajaxError" ); michael@0: }).ajaxSuccess(function(){ michael@0: ok( false, "ajaxSuccess" ); michael@0: }) michael@0: ; michael@0: michael@0: $.ajaxSetup({ timeout: 500 }); michael@0: michael@0: $.ajax({ michael@0: url: url("data/name.php?wait=5"), michael@0: beforeSend: function(){ ok(true, "beforeSend"); }, michael@0: success: function(){ ok(false, "success"); }, michael@0: error: function(){ ok(true, "error"); }, michael@0: complete: function(){ ok(true, "complete"); } michael@0: }); michael@0: }); michael@0: michael@0: */ michael@0: michael@0: test("$.ajax() - disabled globals", function() { michael@0: expect( 3 ); michael@0: stop(); michael@0: michael@0: $('#foo').ajaxStart(function(){ michael@0: ok( false, "ajaxStart" ); michael@0: }).ajaxStop(function(){ michael@0: ok( false, "ajaxStop" ); michael@0: }).ajaxSend(function(){ michael@0: ok( false, "ajaxSend" ); michael@0: }).ajaxComplete(function(){ michael@0: ok( false, "ajaxComplete" ); michael@0: }).ajaxError(function(){ michael@0: ok( false, "ajaxError" ); michael@0: }).ajaxSuccess(function(){ michael@0: ok( false, "ajaxSuccess" ); michael@0: }); michael@0: michael@0: $.ajax({ michael@0: global: false, michael@0: url: url("data/name.html"), michael@0: beforeSend: function(){ ok(true, "beforeSend"); }, michael@0: success: function(){ ok(true, "success"); }, michael@0: error: function(){ ok(false, "error"); }, michael@0: complete: function(){ michael@0: ok(true, "complete"); michael@0: setTimeout(function(){ start(); }, 13); michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: test("$.ajax - xml: non-namespace elements inside namespaced elements", function() { michael@0: expect(3); michael@0: stop(); michael@0: $.ajax({ michael@0: url: url("data/with_fries.xml"), michael@0: dataType: "xml", michael@0: success: function(resp) { michael@0: equals( $("properties", resp).length, 1, 'properties in responseXML' ); michael@0: equals( $("jsconf", resp).length, 1, 'jsconf in responseXML' ); michael@0: equals( $("thing", resp).length, 2, 'things in responseXML' ); michael@0: start(); michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: test("$.ajax - beforeSend", function() { michael@0: expect(1); michael@0: stop(); michael@0: michael@0: var check = false; michael@0: michael@0: $.ajaxSetup({ timeout: 0 }); michael@0: michael@0: $.ajax({ michael@0: url: url("data/name.html"), michael@0: beforeSend: function(xml) { michael@0: check = true; michael@0: }, michael@0: success: function(data) { michael@0: ok( check, "check beforeSend was executed" ); michael@0: start(); michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: test("$.ajax - beforeSend, cancel request (#2688)", function() { michael@0: expect(2); michael@0: var request = $.ajax({ michael@0: url: url("data/name.html"), michael@0: beforeSend: function() { michael@0: ok( true, "beforeSend got called, canceling" ); michael@0: return false; michael@0: }, michael@0: success: function() { michael@0: ok( false, "request didn't get canceled" ); michael@0: }, michael@0: complete: function() { michael@0: ok( false, "request didn't get canceled" ); michael@0: }, michael@0: error: function() { michael@0: ok( false, "request didn't get canceled" ); michael@0: } michael@0: }); michael@0: ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" ); michael@0: }); michael@0: michael@0: var foobar; michael@0: michael@0: test("$.ajax - dataType html", function() { michael@0: expect(5); michael@0: stop(); michael@0: michael@0: foobar = null; michael@0: testFoo = undefined; michael@0: michael@0: var verifyEvaluation = function() { michael@0: equals( testFoo, "foo", 'Check if script was evaluated for datatype html' ); michael@0: equals( foobar, "bar", 'Check if script src was evaluated for datatype html' ); michael@0: start(); michael@0: }; michael@0: michael@0: $.ajax({ michael@0: dataType: "html", michael@0: url: url("data/test.html"), michael@0: success: function(data) { michael@0: $("#ap").html(data); michael@0: ok( data.match(/^html text/), 'Check content for datatype html' ); michael@0: setTimeout(verifyEvaluation, 600); michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: test("serialize()", function() { michael@0: expect(6); michael@0: michael@0: equals( $('#form').serialize(), michael@0: "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2", michael@0: 'Check form serialization as query string'); michael@0: michael@0: equals( $('#form :input').serialize(), michael@0: "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2", michael@0: 'Check input serialization as query string'); michael@0: michael@0: equals( $('#testForm').serialize(), michael@0: 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=', michael@0: 'Check form serialization as query string'); michael@0: michael@0: equals( $('#testForm :input').serialize(), michael@0: 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=', michael@0: 'Check input serialization as query string'); michael@0: michael@0: equals( $('#form, #testForm').serialize(), michael@0: "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=", michael@0: 'Multiple form serialization as query string'); michael@0: michael@0: equals( $('#form, #testForm :input').serialize(), michael@0: "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=", michael@0: 'Mixed form/input serialization as query string'); michael@0: }); michael@0: michael@0: test("$.param()", function() { michael@0: expect(4); michael@0: var params = {foo:"bar", baz:42, quux:"All your base are belong to us"}; michael@0: equals( $.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" ); michael@0: michael@0: params = {someName: [1, 2, 3], regularThing: "blah" }; michael@0: equals( $.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" ); michael@0: michael@0: params = {"foo[]":["baz", 42, "All your base are belong to us"]}; michael@0: equals( $.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" ); michael@0: michael@0: params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"}; michael@0: equals( $.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" ); michael@0: }); michael@0: michael@0: test("synchronous request", function() { michael@0: expect(1); michael@0: ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" ); michael@0: }); michael@0: michael@0: test("synchronous request with callbacks", function() { michael@0: expect(2); michael@0: var result; michael@0: $.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "success callback executed"); result = data; } }); michael@0: ok( /^{ "data"/.test( result ), "check returned text" ); michael@0: }); michael@0: michael@0: test("pass-through request object", function() { michael@0: expect(8); michael@0: stop(true); michael@0: michael@0: var target = "data/name.html"; michael@0: var successCount = 0; michael@0: var errorCount = 0; michael@0: var errorEx = ""; michael@0: var success = function() { michael@0: successCount++; michael@0: }; michael@0: $("#foo").ajaxError(function (e, xml, s, ex) { michael@0: errorCount++; michael@0: errorEx += ": " + xml.status; michael@0: }); michael@0: $("#foo").one('ajaxStop', function () { michael@0: equals(successCount, 5, "Check all ajax calls successful"); michael@0: equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")"); michael@0: $("#foo").unbind('ajaxError'); michael@0: start(); michael@0: }); michael@0: michael@0: ok( $.get(url(target), success), "get" ); michael@0: ok( $.post(url(target), success), "post" ); michael@0: ok( $.getScript(url("data/test.js"), success), "script" ); michael@0: ok( $.getJSON(url("data/json_obj.js"), success), "json" ); michael@0: ok( $.ajax({url: url(target), success: success}), "generic" ); michael@0: }); michael@0: michael@0: /* mozilla: php not currently supported in mochitest (08/08/2008) michael@0: test("ajax cache", function () { michael@0: expect(18); michael@0: stop(); michael@0: michael@0: var count = 0; michael@0: michael@0: $("#firstp").bind("ajaxSuccess", function (e, xml, s) { michael@0: var re = /_=(.*?)(&|$)/g; michael@0: var oldOne = null; michael@0: for (var i = 0; i < 6; i++) { michael@0: var ret = re.exec(s.url); michael@0: if (!ret) { michael@0: break; michael@0: } michael@0: oldOne = ret[1]; michael@0: } michael@0: equals(i, 1, "Test to make sure only one 'no-cache' parameter is there"); michael@0: ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced"); michael@0: if(++count == 6) michael@0: start(); michael@0: }); michael@0: michael@0: ok( $.ajax({url: "data/text.php", cache:false}), "test with no parameters" ); michael@0: ok( $.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" ); michael@0: ok( $.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" ); michael@0: ok( $.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" ); michael@0: ok( $.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" ); michael@0: ok( $.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" ); michael@0: }); michael@0: */ michael@0: michael@0: test("global ajaxSettings", function() { michael@0: expect(2); michael@0: michael@0: var tmp = jQuery.extend({}, jQuery.ajaxSettings); michael@0: var orig = { url: "data/with_fries.xml" }; michael@0: var t; michael@0: michael@0: $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} }); michael@0: michael@0: t = jQuery.extend({}, orig); michael@0: t.data = {}; michael@0: $.ajax(t); michael@0: ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" ); michael@0: michael@0: t = jQuery.extend({}, orig); michael@0: t.data = { zoo: 'a', ping: 'b' }; michael@0: $.ajax(t); michael@0: ok( t.url.indexOf('ping') > -1 && t.url.indexOf('zoo') > -1 && t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending { zoo: 'a', ping: 'b' }" ); michael@0: michael@0: jQuery.ajaxSettings = tmp; michael@0: }); michael@0: michael@0: test("load(String)", function() { michael@0: expect(1); michael@0: stop(true); // check if load can be called with only url michael@0: $('#first').load("data/name.html", start); michael@0: }); michael@0: michael@0: test("load('url selector')", function() { michael@0: expect(1); michael@0: stop(true); // check if load can be called with only url michael@0: $('#first').load("data/test3.html div.user", function(){ michael@0: equals( $(this).children("div").length, 2, "Verify that specific elements were injected" ); michael@0: start(); michael@0: }); michael@0: }); michael@0: michael@0: test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() { michael@0: expect(1); michael@0: stop(); michael@0: $.ajaxSetup({ dataType: "json" }); michael@0: $("#first").ajaxComplete(function (e, xml, s) { michael@0: equals( s.dataType, "html", "Verify the load() dataType was html" ); michael@0: $("#first").unbind("ajaxComplete"); michael@0: $.ajaxSetup({ dataType: "" }); michael@0: start(); michael@0: }); michael@0: $('#first').load("data/test3.html"); michael@0: }); michael@0: michael@0: test("load(String, Function) - simple: inject text into DOM", function() { michael@0: expect(2); michael@0: stop(); michael@0: $('#first').load(url("data/name.html"), function() { michael@0: ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' ); michael@0: start(); michael@0: }); michael@0: }); michael@0: michael@0: test("load(String, Function) - check scripts", function() { michael@0: expect(7); michael@0: stop(); michael@0: window.testFoo = undefined; michael@0: window.foobar = null; michael@0: var verifyEvaluation = function() { michael@0: equals( foobar, "bar", 'Check if script src was evaluated after load' ); michael@0: equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM'); michael@0: start(); michael@0: }; michael@0: $('#first').load(url('data/test.html'), function() { michael@0: ok( $('#first').html().match(/^html text/), 'Check content after loading html' ); michael@0: equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM'); michael@0: equals( testFoo, "foo", 'Check if script was evaluated after load' ); michael@0: setTimeout(verifyEvaluation, 600); michael@0: }); michael@0: }); michael@0: michael@0: test("load(String, Function) - check file with only a script tag", function() { michael@0: expect(3); michael@0: stop(); michael@0: testFoo = undefined; michael@0: $('#first').load(url('data/test2.html'), function() { michael@0: equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM'); michael@0: equals( testFoo, "foo", 'Check if script was evaluated after load' ); michael@0: start(); michael@0: }); michael@0: }); michael@0: michael@0: test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() { michael@0: expect(2); michael@0: stop(); michael@0: $.get(url('data/dashboard.xml'), function(xml) { michael@0: var content = []; michael@0: $('tab', xml).each(function() { michael@0: content.push($(this).text()); michael@0: }); michael@0: equals( content[0], 'blabla', 'Check first tab'); michael@0: equals( content[1], 'blublu', 'Check second tab'); michael@0: start(); michael@0: }); michael@0: }); michael@0: michael@0: test("$.getScript(String, Function) - with callback", function() { michael@0: expect(2); michael@0: stop(); michael@0: window.foobar = null; michael@0: $.getScript(url("data/test.js"), function() { michael@0: equals( foobar, "bar", 'Check if script was evaluated' ); michael@0: setTimeout(start, 100); michael@0: }); michael@0: }); michael@0: michael@0: test("$.getScript(String, Function) - no callback", function() { michael@0: expect(1); michael@0: stop(true); michael@0: $.getScript(url("data/test.js"), start); michael@0: }); michael@0: michael@0: /* mozilla: Tests using php scripts not currently supported (06/26/2008) michael@0: michael@0: test("$.ajax() - JSONP, Local", function() { michael@0: expect(7); michael@0: michael@0: var count = 0; michael@0: function plus(){ if ( ++count == 7 ) start(); } michael@0: michael@0: stop(); michael@0: michael@0: $.ajax({ michael@0: url: "data/jsonp.php", michael@0: dataType: "jsonp", michael@0: success: function(data){ michael@0: ok( data.data, "JSON results returned (GET, no callback)" ); michael@0: plus(); michael@0: }, michael@0: error: function(data){ michael@0: ok( false, "Ajax error JSON (GET, no callback)" ); michael@0: plus(); michael@0: } michael@0: }); michael@0: michael@0: $.ajax({ michael@0: url: "data/jsonp.php?callback=?", michael@0: dataType: "jsonp", michael@0: success: function(data){ michael@0: ok( data.data, "JSON results returned (GET, url callback)" ); michael@0: plus(); michael@0: }, michael@0: error: function(data){ michael@0: ok( false, "Ajax error JSON (GET, url callback)" ); michael@0: plus(); michael@0: } michael@0: }); michael@0: michael@0: $.ajax({ michael@0: url: "data/jsonp.php", michael@0: dataType: "jsonp", michael@0: data: "callback=?", michael@0: success: function(data){ michael@0: ok( data.data, "JSON results returned (GET, data callback)" ); michael@0: plus(); michael@0: }, michael@0: error: function(data){ michael@0: ok( false, "Ajax error JSON (GET, data callback)" ); michael@0: plus(); michael@0: } michael@0: }); michael@0: michael@0: $.ajax({ michael@0: url: "data/jsonp.php", michael@0: dataType: "jsonp", michael@0: jsonp: "callback", michael@0: success: function(data){ michael@0: ok( data.data, "JSON results returned (GET, data obj callback)" ); michael@0: plus(); michael@0: }, michael@0: error: function(data){ michael@0: ok( false, "Ajax error JSON (GET, data obj callback)" ); michael@0: plus(); michael@0: } michael@0: }); michael@0: michael@0: $.ajax({ michael@0: type: "POST", michael@0: url: "data/jsonp.php", michael@0: dataType: "jsonp", michael@0: success: function(data){ michael@0: ok( data.data, "JSON results returned (POST, no callback)" ); michael@0: plus(); michael@0: }, michael@0: error: function(data){ michael@0: ok( false, "Ajax error JSON (GET, data obj callback)" ); michael@0: plus(); michael@0: } michael@0: }); michael@0: michael@0: $.ajax({ michael@0: type: "POST", michael@0: url: "data/jsonp.php", michael@0: data: "callback=?", michael@0: dataType: "jsonp", michael@0: success: function(data){ michael@0: ok( data.data, "JSON results returned (POST, data callback)" ); michael@0: plus(); michael@0: }, michael@0: error: function(data){ michael@0: ok( false, "Ajax error JSON (POST, data callback)" ); michael@0: plus(); michael@0: } michael@0: }); michael@0: michael@0: $.ajax({ michael@0: type: "POST", michael@0: url: "data/jsonp.php", michael@0: jsonp: "callback", michael@0: dataType: "jsonp", michael@0: success: function(data){ michael@0: ok( data.data, "JSON results returned (POST, data obj callback)" ); michael@0: plus(); michael@0: }, michael@0: error: function(data){ michael@0: ok( false, "Ajax error JSON (POST, data obj callback)" ); michael@0: plus(); michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: test("$.ajax() - JSONP, Remote", function() { michael@0: expect(4); michael@0: michael@0: var count = 0; michael@0: function plus(){ if ( ++count == 4 ) start(); } michael@0: michael@0: var base = window.location.href.replace(/\?.*$/, ""); michael@0: michael@0: stop(); michael@0: michael@0: $.ajax({ michael@0: url: base + "data/jsonp.php", michael@0: dataType: "jsonp", michael@0: success: function(data){ michael@0: ok( data.data, "JSON results returned (GET, no callback)" ); michael@0: plus(); michael@0: }, michael@0: error: function(data){ michael@0: ok( false, "Ajax error JSON (GET, no callback)" ); michael@0: plus(); michael@0: } michael@0: }); michael@0: michael@0: $.ajax({ michael@0: url: base + "data/jsonp.php?callback=?", michael@0: dataType: "jsonp", michael@0: success: function(data){ michael@0: ok( data.data, "JSON results returned (GET, url callback)" ); michael@0: plus(); michael@0: }, michael@0: error: function(data){ michael@0: ok( false, "Ajax error JSON (GET, url callback)" ); michael@0: plus(); michael@0: } michael@0: }); michael@0: michael@0: $.ajax({ michael@0: url: base + "data/jsonp.php", michael@0: dataType: "jsonp", michael@0: data: "callback=?", michael@0: success: function(data){ michael@0: ok( data.data, "JSON results returned (GET, data callback)" ); michael@0: plus(); michael@0: }, michael@0: error: function(data){ michael@0: ok( false, "Ajax error JSON (GET, data callback)" ); michael@0: plus(); michael@0: } michael@0: }); michael@0: michael@0: $.ajax({ michael@0: url: base + "data/jsonp.php", michael@0: dataType: "jsonp", michael@0: jsonp: "callback", michael@0: success: function(data){ michael@0: ok( data.data, "JSON results returned (GET, data obj callback)" ); michael@0: plus(); michael@0: }, michael@0: error: function(data){ michael@0: ok( false, "Ajax error JSON (GET, data obj callback)" ); michael@0: plus(); michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: test("$.ajax() - script, Remote", function() { michael@0: expect(2); michael@0: michael@0: var base = window.location.href.replace(/\?.*$/, ""); michael@0: michael@0: stop(); michael@0: michael@0: window.foobar = null; michael@0: $.ajax({ michael@0: url: base + "data/test.js", michael@0: dataType: "script", michael@0: success: function(data){ michael@0: ok( foobar, "Script results returned (GET, no callback)" ); michael@0: start(); michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: test("$.ajax() - script, Remote with POST", function() { michael@0: expect(3); michael@0: michael@0: var base = window.location.href.replace(/\?.*$/, ""); michael@0: michael@0: stop(); michael@0: michael@0: window.foobar = null; michael@0: $.ajax({ michael@0: url: base + "data/test.js", michael@0: type: "POST", michael@0: dataType: "script", michael@0: success: function(data, status){ michael@0: ok( foobar, "Script results returned (GET, no callback)" ); michael@0: equals( status, "success", "Script results returned (GET, no callback)" ); michael@0: start(); michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: test("$.ajax() - script, Remote with scheme-less URL", function() { michael@0: expect(2); michael@0: michael@0: var base = window.location.href.replace(/\?.*$/, ""); michael@0: base = base.replace(/^.*?\/\//, "//"); michael@0: michael@0: stop(); michael@0: michael@0: window.foobar = null; michael@0: $.ajax({ michael@0: url: base + "data/test.js", michael@0: dataType: "script", michael@0: success: function(data){ michael@0: ok( foobar, "Script results returned (GET, no callback)" ); michael@0: start(); michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: test("$.getJSON(String, Hash, Function) - JSON array", function() { michael@0: expect(4); michael@0: stop(); michael@0: $.getJSON(url("data/json.php"), {json: "array"}, function(json) { michael@0: equals( json[0].name, 'John', 'Check JSON: first, name' ); michael@0: equals( json[0].age, 21, 'Check JSON: first, age' ); michael@0: equals( json[1].name, 'Peter', 'Check JSON: second, name' ); michael@0: equals( json[1].age, 25, 'Check JSON: second, age' ); michael@0: start(); michael@0: }); michael@0: }); michael@0: michael@0: test("$.getJSON(String, Function) - JSON object", function() { michael@0: expect(2); michael@0: stop(); michael@0: $.getJSON(url("data/json.php"), function(json) { michael@0: equals( json.data.lang, 'en', 'Check JSON: lang' ); michael@0: equals( json.data.length, 25, 'Check JSON: length' ); michael@0: start(); michael@0: }); michael@0: }); michael@0: michael@0: test("$.getJSON(String, Function) - JSON object with absolute url to local content", function() { michael@0: expect(2); michael@0: michael@0: var base = window.location.href.replace(/\?.*$/, ""); michael@0: michael@0: stop(); michael@0: $.getJSON(url(base + "data/json.php"), function(json) { michael@0: equals( json.data.lang, 'en', 'Check JSON: lang' ); michael@0: equals( json.data.length, 25, 'Check JSON: length' ); michael@0: start(); michael@0: }); michael@0: }); michael@0: michael@0: test("$.post(String, Hash, Function) - simple with xml", function() { michael@0: expect(4); michael@0: stop(); michael@0: $.post(url("data/name.php"), {xml: "5-2"}, function(xml){ michael@0: $('math', xml).each(function() { michael@0: equals( $('calculation', this).text(), '5-2', 'Check for XML' ); michael@0: equals( $('result', this).text(), '3', 'Check for XML' ); michael@0: }); michael@0: }); michael@0: michael@0: $.post(url("data/name.php?xml=5-2"), {}, function(xml){ michael@0: $('math', xml).each(function() { michael@0: equals( $('calculation', this).text(), '5-2', 'Check for XML' ); michael@0: equals( $('result', this).text(), '3', 'Check for XML' ); michael@0: }); michael@0: start(); michael@0: }); michael@0: }); michael@0: michael@0: test("$.ajaxSetup({timeout: Number}) - with global timeout", function() { michael@0: stop(); michael@0: michael@0: var passed = 0; michael@0: michael@0: $.ajaxSetup({timeout: 1000}); michael@0: michael@0: var pass = function() { michael@0: passed++; michael@0: if ( passed == 2 ) { michael@0: ok( true, 'Check local and global callbacks after timeout' ); michael@0: $('#main').unbind("ajaxError"); michael@0: start(); michael@0: } michael@0: }; michael@0: michael@0: var fail = function(a,b,c) { michael@0: ok( false, 'Check for timeout failed ' + a + ' ' + b ); michael@0: start(); michael@0: }; michael@0: michael@0: $('#main').ajaxError(pass); michael@0: michael@0: $.ajax({ michael@0: type: "GET", michael@0: url: url("data/name.php?wait=5"), michael@0: error: pass, michael@0: success: fail michael@0: }); michael@0: michael@0: // reset timeout michael@0: $.ajaxSetup({timeout: 0}); michael@0: }); michael@0: michael@0: test("$.ajaxSetup({timeout: Number}) with localtimeout", function() { michael@0: stop(); michael@0: $.ajaxSetup({timeout: 50}); michael@0: michael@0: $.ajax({ michael@0: type: "GET", michael@0: timeout: 5000, michael@0: url: url("data/name.php?wait=1"), michael@0: error: function() { michael@0: ok( false, 'Check for local timeout failed' ); michael@0: start(); michael@0: }, michael@0: success: function() { michael@0: ok( true, 'Check for local timeout' ); michael@0: start(); michael@0: } michael@0: }); michael@0: michael@0: // reset timeout michael@0: $.ajaxSetup({timeout: 0}); michael@0: }); michael@0: michael@0: test("$.ajax - simple get", function() { michael@0: expect(1); michael@0: stop(); michael@0: $.ajax({ michael@0: type: "GET", michael@0: url: url("data/name.php?name=foo"), michael@0: success: function(msg){ michael@0: equals( msg, 'bar', 'Check for GET' ); michael@0: start(); michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: test("$.ajax - simple post", function() { michael@0: expect(1); michael@0: stop(); michael@0: $.ajax({ michael@0: type: "POST", michael@0: url: url("data/name.php"), michael@0: data: "name=peter", michael@0: success: function(msg){ michael@0: equals( msg, 'pan', 'Check for POST' ); michael@0: start(); michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: test("ajaxSetup()", function() { michael@0: expect(1); michael@0: stop(); michael@0: $.ajaxSetup({ michael@0: url: url("data/name.php?name=foo"), michael@0: success: function(msg){ michael@0: equals( msg, 'bar', 'Check for GET' ); michael@0: start(); michael@0: } michael@0: }); michael@0: $.ajax(); michael@0: }); michael@0: michael@0: test("custom timeout does not set error message when timeout occurs, see #970", function() { michael@0: stop(); michael@0: $.ajax({ michael@0: url: "data/name.php?wait=10", michael@0: timeout: 500, michael@0: error: function(request, status) { michael@0: ok( status != null, "status shouldn't be null in error handler" ); michael@0: equals( "timeout", status ); michael@0: start(); michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: test("data option: evaluate function values (#2806)", function() { michael@0: stop(); michael@0: $.ajax({ michael@0: url: "data/echoQuery.php", michael@0: data: { michael@0: key: function() { michael@0: return "value"; michael@0: } michael@0: }, michael@0: success: function(result) { michael@0: equals( result, "key=value" ); michael@0: start(); michael@0: } michael@0: }) michael@0: }); michael@0: */ michael@0: } michael@0: michael@0: //}