1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/ajax/jquery/test/unit/ajax.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,837 @@ 1.4 +module("ajax"); 1.5 + 1.6 +// Safari 3 randomly crashes when running these tests, 1.7 +// but only in the full suite - you can run just the Ajax 1.8 +// tests and they'll pass 1.9 +//if ( !jQuery.browser.safari ) { 1.10 + 1.11 +if ( !isLocal ) { 1.12 + 1.13 +test("$.ajax() - success callbacks", function() { 1.14 + expect( 8 ); 1.15 + 1.16 + $.ajaxSetup({ timeout: 0 }); 1.17 + 1.18 + stop(); 1.19 + 1.20 + setTimeout(function(){ 1.21 + $('#foo').ajaxStart(function(){ 1.22 + ok( true, "ajaxStart" ); 1.23 + }).ajaxStop(function(){ 1.24 + ok( true, "ajaxStop" ); 1.25 + start(); 1.26 + }).ajaxSend(function(){ 1.27 + ok( true, "ajaxSend" ); 1.28 + }).ajaxComplete(function(){ 1.29 + ok( true, "ajaxComplete" ); 1.30 + }).ajaxError(function(){ 1.31 + ok( false, "ajaxError" ); 1.32 + }).ajaxSuccess(function(){ 1.33 + ok( true, "ajaxSuccess" ); 1.34 + }); 1.35 + 1.36 + $.ajax({ 1.37 + url: url("data/name.html"), 1.38 + beforeSend: function(){ ok(true, "beforeSend"); }, 1.39 + success: function(){ ok(true, "success"); }, 1.40 + error: function(){ ok(false, "error"); }, 1.41 + complete: function(){ ok(true, "complete"); } 1.42 + }); 1.43 + }, 13); 1.44 +}); 1.45 + 1.46 +/* mozilla: the ajaxSuccess part fails intermittently on MacOSX 1.47 + 1.48 +test("$.ajax() - error callbacks", function() { 1.49 + expect( 7 ); 1.50 + stop(); 1.51 + 1.52 + $('#foo').ajaxStart(function(){ 1.53 + ok( true, "ajaxStart" ); 1.54 + }).ajaxStop(function(){ 1.55 + ok( true, "ajaxStop" ); 1.56 + start(); 1.57 + }).ajaxSend(function(){ 1.58 + ok( true, "ajaxSend" ); 1.59 + }).ajaxComplete(function(){ 1.60 + ok( true, "ajaxComplete" ); 1.61 + }).ajaxError(function(){ 1.62 + ok( true, "ajaxError" ); 1.63 + }).ajaxSuccess(function(){ 1.64 + ok( false, "ajaxSuccess" ); 1.65 + }) 1.66 + ; 1.67 + 1.68 + $.ajaxSetup({ timeout: 500 }); 1.69 + 1.70 + $.ajax({ 1.71 + url: url("data/name.php?wait=5"), 1.72 + beforeSend: function(){ ok(true, "beforeSend"); }, 1.73 + success: function(){ ok(false, "success"); }, 1.74 + error: function(){ ok(true, "error"); }, 1.75 + complete: function(){ ok(true, "complete"); } 1.76 + }); 1.77 +}); 1.78 + 1.79 +*/ 1.80 + 1.81 +test("$.ajax() - disabled globals", function() { 1.82 + expect( 3 ); 1.83 + stop(); 1.84 + 1.85 + $('#foo').ajaxStart(function(){ 1.86 + ok( false, "ajaxStart" ); 1.87 + }).ajaxStop(function(){ 1.88 + ok( false, "ajaxStop" ); 1.89 + }).ajaxSend(function(){ 1.90 + ok( false, "ajaxSend" ); 1.91 + }).ajaxComplete(function(){ 1.92 + ok( false, "ajaxComplete" ); 1.93 + }).ajaxError(function(){ 1.94 + ok( false, "ajaxError" ); 1.95 + }).ajaxSuccess(function(){ 1.96 + ok( false, "ajaxSuccess" ); 1.97 + }); 1.98 + 1.99 + $.ajax({ 1.100 + global: false, 1.101 + url: url("data/name.html"), 1.102 + beforeSend: function(){ ok(true, "beforeSend"); }, 1.103 + success: function(){ ok(true, "success"); }, 1.104 + error: function(){ ok(false, "error"); }, 1.105 + complete: function(){ 1.106 + ok(true, "complete"); 1.107 + setTimeout(function(){ start(); }, 13); 1.108 + } 1.109 + }); 1.110 +}); 1.111 + 1.112 +test("$.ajax - xml: non-namespace elements inside namespaced elements", function() { 1.113 + expect(3); 1.114 + stop(); 1.115 + $.ajax({ 1.116 + url: url("data/with_fries.xml"), 1.117 + dataType: "xml", 1.118 + success: function(resp) { 1.119 + equals( $("properties", resp).length, 1, 'properties in responseXML' ); 1.120 + equals( $("jsconf", resp).length, 1, 'jsconf in responseXML' ); 1.121 + equals( $("thing", resp).length, 2, 'things in responseXML' ); 1.122 + start(); 1.123 + } 1.124 + }); 1.125 +}); 1.126 + 1.127 +test("$.ajax - beforeSend", function() { 1.128 + expect(1); 1.129 + stop(); 1.130 + 1.131 + var check = false; 1.132 + 1.133 + $.ajaxSetup({ timeout: 0 }); 1.134 + 1.135 + $.ajax({ 1.136 + url: url("data/name.html"), 1.137 + beforeSend: function(xml) { 1.138 + check = true; 1.139 + }, 1.140 + success: function(data) { 1.141 + ok( check, "check beforeSend was executed" ); 1.142 + start(); 1.143 + } 1.144 + }); 1.145 +}); 1.146 + 1.147 +test("$.ajax - beforeSend, cancel request (#2688)", function() { 1.148 + expect(2); 1.149 + var request = $.ajax({ 1.150 + url: url("data/name.html"), 1.151 + beforeSend: function() { 1.152 + ok( true, "beforeSend got called, canceling" ); 1.153 + return false; 1.154 + }, 1.155 + success: function() { 1.156 + ok( false, "request didn't get canceled" ); 1.157 + }, 1.158 + complete: function() { 1.159 + ok( false, "request didn't get canceled" ); 1.160 + }, 1.161 + error: function() { 1.162 + ok( false, "request didn't get canceled" ); 1.163 + } 1.164 + }); 1.165 + ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" ); 1.166 +}); 1.167 + 1.168 +var foobar; 1.169 + 1.170 +test("$.ajax - dataType html", function() { 1.171 + expect(5); 1.172 + stop(); 1.173 + 1.174 + foobar = null; 1.175 + testFoo = undefined; 1.176 + 1.177 + var verifyEvaluation = function() { 1.178 + equals( testFoo, "foo", 'Check if script was evaluated for datatype html' ); 1.179 + equals( foobar, "bar", 'Check if script src was evaluated for datatype html' ); 1.180 + start(); 1.181 + }; 1.182 + 1.183 + $.ajax({ 1.184 + dataType: "html", 1.185 + url: url("data/test.html"), 1.186 + success: function(data) { 1.187 + $("#ap").html(data); 1.188 + ok( data.match(/^html text/), 'Check content for datatype html' ); 1.189 + setTimeout(verifyEvaluation, 600); 1.190 + } 1.191 + }); 1.192 +}); 1.193 + 1.194 +test("serialize()", function() { 1.195 + expect(6); 1.196 + 1.197 + equals( $('#form').serialize(), 1.198 + "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2", 1.199 + 'Check form serialization as query string'); 1.200 + 1.201 + equals( $('#form :input').serialize(), 1.202 + "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2", 1.203 + 'Check input serialization as query string'); 1.204 + 1.205 + equals( $('#testForm').serialize(), 1.206 + 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=', 1.207 + 'Check form serialization as query string'); 1.208 + 1.209 + equals( $('#testForm :input').serialize(), 1.210 + 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=', 1.211 + 'Check input serialization as query string'); 1.212 + 1.213 + equals( $('#form, #testForm').serialize(), 1.214 + "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=", 1.215 + 'Multiple form serialization as query string'); 1.216 + 1.217 + equals( $('#form, #testForm :input').serialize(), 1.218 + "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=", 1.219 + 'Mixed form/input serialization as query string'); 1.220 +}); 1.221 + 1.222 +test("$.param()", function() { 1.223 + expect(4); 1.224 + var params = {foo:"bar", baz:42, quux:"All your base are belong to us"}; 1.225 + equals( $.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" ); 1.226 + 1.227 + params = {someName: [1, 2, 3], regularThing: "blah" }; 1.228 + equals( $.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" ); 1.229 + 1.230 + params = {"foo[]":["baz", 42, "All your base are belong to us"]}; 1.231 + equals( $.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" ); 1.232 + 1.233 + params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"}; 1.234 + equals( $.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" ); 1.235 +}); 1.236 + 1.237 +test("synchronous request", function() { 1.238 + expect(1); 1.239 + ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" ); 1.240 +}); 1.241 + 1.242 +test("synchronous request with callbacks", function() { 1.243 + expect(2); 1.244 + var result; 1.245 + $.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "success callback executed"); result = data; } }); 1.246 + ok( /^{ "data"/.test( result ), "check returned text" ); 1.247 +}); 1.248 + 1.249 +test("pass-through request object", function() { 1.250 + expect(8); 1.251 + stop(true); 1.252 + 1.253 + var target = "data/name.html"; 1.254 + var successCount = 0; 1.255 + var errorCount = 0; 1.256 + var errorEx = ""; 1.257 + var success = function() { 1.258 + successCount++; 1.259 + }; 1.260 + $("#foo").ajaxError(function (e, xml, s, ex) { 1.261 + errorCount++; 1.262 + errorEx += ": " + xml.status; 1.263 + }); 1.264 + $("#foo").one('ajaxStop', function () { 1.265 + equals(successCount, 5, "Check all ajax calls successful"); 1.266 + equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")"); 1.267 + $("#foo").unbind('ajaxError'); 1.268 + start(); 1.269 + }); 1.270 + 1.271 + ok( $.get(url(target), success), "get" ); 1.272 + ok( $.post(url(target), success), "post" ); 1.273 + ok( $.getScript(url("data/test.js"), success), "script" ); 1.274 + ok( $.getJSON(url("data/json_obj.js"), success), "json" ); 1.275 + ok( $.ajax({url: url(target), success: success}), "generic" ); 1.276 +}); 1.277 + 1.278 +/* mozilla: php not currently supported in mochitest (08/08/2008) 1.279 +test("ajax cache", function () { 1.280 + expect(18); 1.281 + stop(); 1.282 + 1.283 + var count = 0; 1.284 + 1.285 + $("#firstp").bind("ajaxSuccess", function (e, xml, s) { 1.286 + var re = /_=(.*?)(&|$)/g; 1.287 + var oldOne = null; 1.288 + for (var i = 0; i < 6; i++) { 1.289 + var ret = re.exec(s.url); 1.290 + if (!ret) { 1.291 + break; 1.292 + } 1.293 + oldOne = ret[1]; 1.294 + } 1.295 + equals(i, 1, "Test to make sure only one 'no-cache' parameter is there"); 1.296 + ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced"); 1.297 + if(++count == 6) 1.298 + start(); 1.299 + }); 1.300 + 1.301 + ok( $.ajax({url: "data/text.php", cache:false}), "test with no parameters" ); 1.302 + ok( $.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" ); 1.303 + ok( $.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" ); 1.304 + ok( $.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" ); 1.305 + ok( $.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" ); 1.306 + ok( $.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" ); 1.307 +}); 1.308 +*/ 1.309 + 1.310 +test("global ajaxSettings", function() { 1.311 + expect(2); 1.312 + 1.313 + var tmp = jQuery.extend({}, jQuery.ajaxSettings); 1.314 + var orig = { url: "data/with_fries.xml" }; 1.315 + var t; 1.316 + 1.317 + $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} }); 1.318 + 1.319 + t = jQuery.extend({}, orig); 1.320 + t.data = {}; 1.321 + $.ajax(t); 1.322 + ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" ); 1.323 + 1.324 + t = jQuery.extend({}, orig); 1.325 + t.data = { zoo: 'a', ping: 'b' }; 1.326 + $.ajax(t); 1.327 + 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' }" ); 1.328 + 1.329 + jQuery.ajaxSettings = tmp; 1.330 +}); 1.331 + 1.332 +test("load(String)", function() { 1.333 + expect(1); 1.334 + stop(true); // check if load can be called with only url 1.335 + $('#first').load("data/name.html", start); 1.336 +}); 1.337 + 1.338 +test("load('url selector')", function() { 1.339 + expect(1); 1.340 + stop(true); // check if load can be called with only url 1.341 + $('#first').load("data/test3.html div.user", function(){ 1.342 + equals( $(this).children("div").length, 2, "Verify that specific elements were injected" ); 1.343 + start(); 1.344 + }); 1.345 +}); 1.346 + 1.347 +test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() { 1.348 + expect(1); 1.349 + stop(); 1.350 + $.ajaxSetup({ dataType: "json" }); 1.351 + $("#first").ajaxComplete(function (e, xml, s) { 1.352 + equals( s.dataType, "html", "Verify the load() dataType was html" ); 1.353 + $("#first").unbind("ajaxComplete"); 1.354 + $.ajaxSetup({ dataType: "" }); 1.355 + start(); 1.356 + }); 1.357 + $('#first').load("data/test3.html"); 1.358 +}); 1.359 + 1.360 +test("load(String, Function) - simple: inject text into DOM", function() { 1.361 + expect(2); 1.362 + stop(); 1.363 + $('#first').load(url("data/name.html"), function() { 1.364 + ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' ); 1.365 + start(); 1.366 + }); 1.367 +}); 1.368 + 1.369 +test("load(String, Function) - check scripts", function() { 1.370 + expect(7); 1.371 + stop(); 1.372 + window.testFoo = undefined; 1.373 + window.foobar = null; 1.374 + var verifyEvaluation = function() { 1.375 + equals( foobar, "bar", 'Check if script src was evaluated after load' ); 1.376 + equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM'); 1.377 + start(); 1.378 + }; 1.379 + $('#first').load(url('data/test.html'), function() { 1.380 + ok( $('#first').html().match(/^html text/), 'Check content after loading html' ); 1.381 + equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM'); 1.382 + equals( testFoo, "foo", 'Check if script was evaluated after load' ); 1.383 + setTimeout(verifyEvaluation, 600); 1.384 + }); 1.385 +}); 1.386 + 1.387 +test("load(String, Function) - check file with only a script tag", function() { 1.388 + expect(3); 1.389 + stop(); 1.390 + testFoo = undefined; 1.391 + $('#first').load(url('data/test2.html'), function() { 1.392 + equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM'); 1.393 + equals( testFoo, "foo", 'Check if script was evaluated after load' ); 1.394 + start(); 1.395 + }); 1.396 +}); 1.397 + 1.398 +test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() { 1.399 + expect(2); 1.400 + stop(); 1.401 + $.get(url('data/dashboard.xml'), function(xml) { 1.402 + var content = []; 1.403 + $('tab', xml).each(function() { 1.404 + content.push($(this).text()); 1.405 + }); 1.406 + equals( content[0], 'blabla', 'Check first tab'); 1.407 + equals( content[1], 'blublu', 'Check second tab'); 1.408 + start(); 1.409 + }); 1.410 +}); 1.411 + 1.412 +test("$.getScript(String, Function) - with callback", function() { 1.413 + expect(2); 1.414 + stop(); 1.415 + window.foobar = null; 1.416 + $.getScript(url("data/test.js"), function() { 1.417 + equals( foobar, "bar", 'Check if script was evaluated' ); 1.418 + setTimeout(start, 100); 1.419 + }); 1.420 +}); 1.421 + 1.422 +test("$.getScript(String, Function) - no callback", function() { 1.423 + expect(1); 1.424 + stop(true); 1.425 + $.getScript(url("data/test.js"), start); 1.426 +}); 1.427 + 1.428 +/* mozilla: Tests using php scripts not currently supported (06/26/2008) 1.429 + 1.430 +test("$.ajax() - JSONP, Local", function() { 1.431 + expect(7); 1.432 + 1.433 + var count = 0; 1.434 + function plus(){ if ( ++count == 7 ) start(); } 1.435 + 1.436 + stop(); 1.437 + 1.438 + $.ajax({ 1.439 + url: "data/jsonp.php", 1.440 + dataType: "jsonp", 1.441 + success: function(data){ 1.442 + ok( data.data, "JSON results returned (GET, no callback)" ); 1.443 + plus(); 1.444 + }, 1.445 + error: function(data){ 1.446 + ok( false, "Ajax error JSON (GET, no callback)" ); 1.447 + plus(); 1.448 + } 1.449 + }); 1.450 + 1.451 + $.ajax({ 1.452 + url: "data/jsonp.php?callback=?", 1.453 + dataType: "jsonp", 1.454 + success: function(data){ 1.455 + ok( data.data, "JSON results returned (GET, url callback)" ); 1.456 + plus(); 1.457 + }, 1.458 + error: function(data){ 1.459 + ok( false, "Ajax error JSON (GET, url callback)" ); 1.460 + plus(); 1.461 + } 1.462 + }); 1.463 + 1.464 + $.ajax({ 1.465 + url: "data/jsonp.php", 1.466 + dataType: "jsonp", 1.467 + data: "callback=?", 1.468 + success: function(data){ 1.469 + ok( data.data, "JSON results returned (GET, data callback)" ); 1.470 + plus(); 1.471 + }, 1.472 + error: function(data){ 1.473 + ok( false, "Ajax error JSON (GET, data callback)" ); 1.474 + plus(); 1.475 + } 1.476 + }); 1.477 + 1.478 + $.ajax({ 1.479 + url: "data/jsonp.php", 1.480 + dataType: "jsonp", 1.481 + jsonp: "callback", 1.482 + success: function(data){ 1.483 + ok( data.data, "JSON results returned (GET, data obj callback)" ); 1.484 + plus(); 1.485 + }, 1.486 + error: function(data){ 1.487 + ok( false, "Ajax error JSON (GET, data obj callback)" ); 1.488 + plus(); 1.489 + } 1.490 + }); 1.491 + 1.492 + $.ajax({ 1.493 + type: "POST", 1.494 + url: "data/jsonp.php", 1.495 + dataType: "jsonp", 1.496 + success: function(data){ 1.497 + ok( data.data, "JSON results returned (POST, no callback)" ); 1.498 + plus(); 1.499 + }, 1.500 + error: function(data){ 1.501 + ok( false, "Ajax error JSON (GET, data obj callback)" ); 1.502 + plus(); 1.503 + } 1.504 + }); 1.505 + 1.506 + $.ajax({ 1.507 + type: "POST", 1.508 + url: "data/jsonp.php", 1.509 + data: "callback=?", 1.510 + dataType: "jsonp", 1.511 + success: function(data){ 1.512 + ok( data.data, "JSON results returned (POST, data callback)" ); 1.513 + plus(); 1.514 + }, 1.515 + error: function(data){ 1.516 + ok( false, "Ajax error JSON (POST, data callback)" ); 1.517 + plus(); 1.518 + } 1.519 + }); 1.520 + 1.521 + $.ajax({ 1.522 + type: "POST", 1.523 + url: "data/jsonp.php", 1.524 + jsonp: "callback", 1.525 + dataType: "jsonp", 1.526 + success: function(data){ 1.527 + ok( data.data, "JSON results returned (POST, data obj callback)" ); 1.528 + plus(); 1.529 + }, 1.530 + error: function(data){ 1.531 + ok( false, "Ajax error JSON (POST, data obj callback)" ); 1.532 + plus(); 1.533 + } 1.534 + }); 1.535 +}); 1.536 + 1.537 +test("$.ajax() - JSONP, Remote", function() { 1.538 + expect(4); 1.539 + 1.540 + var count = 0; 1.541 + function plus(){ if ( ++count == 4 ) start(); } 1.542 + 1.543 + var base = window.location.href.replace(/\?.*$/, ""); 1.544 + 1.545 + stop(); 1.546 + 1.547 + $.ajax({ 1.548 + url: base + "data/jsonp.php", 1.549 + dataType: "jsonp", 1.550 + success: function(data){ 1.551 + ok( data.data, "JSON results returned (GET, no callback)" ); 1.552 + plus(); 1.553 + }, 1.554 + error: function(data){ 1.555 + ok( false, "Ajax error JSON (GET, no callback)" ); 1.556 + plus(); 1.557 + } 1.558 + }); 1.559 + 1.560 + $.ajax({ 1.561 + url: base + "data/jsonp.php?callback=?", 1.562 + dataType: "jsonp", 1.563 + success: function(data){ 1.564 + ok( data.data, "JSON results returned (GET, url callback)" ); 1.565 + plus(); 1.566 + }, 1.567 + error: function(data){ 1.568 + ok( false, "Ajax error JSON (GET, url callback)" ); 1.569 + plus(); 1.570 + } 1.571 + }); 1.572 + 1.573 + $.ajax({ 1.574 + url: base + "data/jsonp.php", 1.575 + dataType: "jsonp", 1.576 + data: "callback=?", 1.577 + success: function(data){ 1.578 + ok( data.data, "JSON results returned (GET, data callback)" ); 1.579 + plus(); 1.580 + }, 1.581 + error: function(data){ 1.582 + ok( false, "Ajax error JSON (GET, data callback)" ); 1.583 + plus(); 1.584 + } 1.585 + }); 1.586 + 1.587 + $.ajax({ 1.588 + url: base + "data/jsonp.php", 1.589 + dataType: "jsonp", 1.590 + jsonp: "callback", 1.591 + success: function(data){ 1.592 + ok( data.data, "JSON results returned (GET, data obj callback)" ); 1.593 + plus(); 1.594 + }, 1.595 + error: function(data){ 1.596 + ok( false, "Ajax error JSON (GET, data obj callback)" ); 1.597 + plus(); 1.598 + } 1.599 + }); 1.600 +}); 1.601 + 1.602 +test("$.ajax() - script, Remote", function() { 1.603 + expect(2); 1.604 + 1.605 + var base = window.location.href.replace(/\?.*$/, ""); 1.606 + 1.607 + stop(); 1.608 + 1.609 + window.foobar = null; 1.610 + $.ajax({ 1.611 + url: base + "data/test.js", 1.612 + dataType: "script", 1.613 + success: function(data){ 1.614 + ok( foobar, "Script results returned (GET, no callback)" ); 1.615 + start(); 1.616 + } 1.617 + }); 1.618 +}); 1.619 + 1.620 +test("$.ajax() - script, Remote with POST", function() { 1.621 + expect(3); 1.622 + 1.623 + var base = window.location.href.replace(/\?.*$/, ""); 1.624 + 1.625 + stop(); 1.626 + 1.627 + window.foobar = null; 1.628 + $.ajax({ 1.629 + url: base + "data/test.js", 1.630 + type: "POST", 1.631 + dataType: "script", 1.632 + success: function(data, status){ 1.633 + ok( foobar, "Script results returned (GET, no callback)" ); 1.634 + equals( status, "success", "Script results returned (GET, no callback)" ); 1.635 + start(); 1.636 + } 1.637 + }); 1.638 +}); 1.639 + 1.640 +test("$.ajax() - script, Remote with scheme-less URL", function() { 1.641 + expect(2); 1.642 + 1.643 + var base = window.location.href.replace(/\?.*$/, ""); 1.644 + base = base.replace(/^.*?\/\//, "//"); 1.645 + 1.646 + stop(); 1.647 + 1.648 + window.foobar = null; 1.649 + $.ajax({ 1.650 + url: base + "data/test.js", 1.651 + dataType: "script", 1.652 + success: function(data){ 1.653 + ok( foobar, "Script results returned (GET, no callback)" ); 1.654 + start(); 1.655 + } 1.656 + }); 1.657 +}); 1.658 + 1.659 +test("$.getJSON(String, Hash, Function) - JSON array", function() { 1.660 + expect(4); 1.661 + stop(); 1.662 + $.getJSON(url("data/json.php"), {json: "array"}, function(json) { 1.663 + equals( json[0].name, 'John', 'Check JSON: first, name' ); 1.664 + equals( json[0].age, 21, 'Check JSON: first, age' ); 1.665 + equals( json[1].name, 'Peter', 'Check JSON: second, name' ); 1.666 + equals( json[1].age, 25, 'Check JSON: second, age' ); 1.667 + start(); 1.668 + }); 1.669 +}); 1.670 + 1.671 +test("$.getJSON(String, Function) - JSON object", function() { 1.672 + expect(2); 1.673 + stop(); 1.674 + $.getJSON(url("data/json.php"), function(json) { 1.675 + equals( json.data.lang, 'en', 'Check JSON: lang' ); 1.676 + equals( json.data.length, 25, 'Check JSON: length' ); 1.677 + start(); 1.678 + }); 1.679 +}); 1.680 + 1.681 +test("$.getJSON(String, Function) - JSON object with absolute url to local content", function() { 1.682 + expect(2); 1.683 + 1.684 + var base = window.location.href.replace(/\?.*$/, ""); 1.685 + 1.686 + stop(); 1.687 + $.getJSON(url(base + "data/json.php"), function(json) { 1.688 + equals( json.data.lang, 'en', 'Check JSON: lang' ); 1.689 + equals( json.data.length, 25, 'Check JSON: length' ); 1.690 + start(); 1.691 + }); 1.692 +}); 1.693 + 1.694 +test("$.post(String, Hash, Function) - simple with xml", function() { 1.695 + expect(4); 1.696 + stop(); 1.697 + $.post(url("data/name.php"), {xml: "5-2"}, function(xml){ 1.698 + $('math', xml).each(function() { 1.699 + equals( $('calculation', this).text(), '5-2', 'Check for XML' ); 1.700 + equals( $('result', this).text(), '3', 'Check for XML' ); 1.701 + }); 1.702 + }); 1.703 + 1.704 + $.post(url("data/name.php?xml=5-2"), {}, function(xml){ 1.705 + $('math', xml).each(function() { 1.706 + equals( $('calculation', this).text(), '5-2', 'Check for XML' ); 1.707 + equals( $('result', this).text(), '3', 'Check for XML' ); 1.708 + }); 1.709 + start(); 1.710 + }); 1.711 +}); 1.712 + 1.713 +test("$.ajaxSetup({timeout: Number}) - with global timeout", function() { 1.714 + stop(); 1.715 + 1.716 + var passed = 0; 1.717 + 1.718 + $.ajaxSetup({timeout: 1000}); 1.719 + 1.720 + var pass = function() { 1.721 + passed++; 1.722 + if ( passed == 2 ) { 1.723 + ok( true, 'Check local and global callbacks after timeout' ); 1.724 + $('#main').unbind("ajaxError"); 1.725 + start(); 1.726 + } 1.727 + }; 1.728 + 1.729 + var fail = function(a,b,c) { 1.730 + ok( false, 'Check for timeout failed ' + a + ' ' + b ); 1.731 + start(); 1.732 + }; 1.733 + 1.734 + $('#main').ajaxError(pass); 1.735 + 1.736 + $.ajax({ 1.737 + type: "GET", 1.738 + url: url("data/name.php?wait=5"), 1.739 + error: pass, 1.740 + success: fail 1.741 + }); 1.742 + 1.743 + // reset timeout 1.744 + $.ajaxSetup({timeout: 0}); 1.745 +}); 1.746 + 1.747 +test("$.ajaxSetup({timeout: Number}) with localtimeout", function() { 1.748 + stop(); 1.749 + $.ajaxSetup({timeout: 50}); 1.750 + 1.751 + $.ajax({ 1.752 + type: "GET", 1.753 + timeout: 5000, 1.754 + url: url("data/name.php?wait=1"), 1.755 + error: function() { 1.756 + ok( false, 'Check for local timeout failed' ); 1.757 + start(); 1.758 + }, 1.759 + success: function() { 1.760 + ok( true, 'Check for local timeout' ); 1.761 + start(); 1.762 + } 1.763 + }); 1.764 + 1.765 + // reset timeout 1.766 + $.ajaxSetup({timeout: 0}); 1.767 +}); 1.768 + 1.769 +test("$.ajax - simple get", function() { 1.770 + expect(1); 1.771 + stop(); 1.772 + $.ajax({ 1.773 + type: "GET", 1.774 + url: url("data/name.php?name=foo"), 1.775 + success: function(msg){ 1.776 + equals( msg, 'bar', 'Check for GET' ); 1.777 + start(); 1.778 + } 1.779 + }); 1.780 +}); 1.781 + 1.782 +test("$.ajax - simple post", function() { 1.783 + expect(1); 1.784 + stop(); 1.785 + $.ajax({ 1.786 + type: "POST", 1.787 + url: url("data/name.php"), 1.788 + data: "name=peter", 1.789 + success: function(msg){ 1.790 + equals( msg, 'pan', 'Check for POST' ); 1.791 + start(); 1.792 + } 1.793 + }); 1.794 +}); 1.795 + 1.796 +test("ajaxSetup()", function() { 1.797 + expect(1); 1.798 + stop(); 1.799 + $.ajaxSetup({ 1.800 + url: url("data/name.php?name=foo"), 1.801 + success: function(msg){ 1.802 + equals( msg, 'bar', 'Check for GET' ); 1.803 + start(); 1.804 + } 1.805 + }); 1.806 + $.ajax(); 1.807 +}); 1.808 + 1.809 +test("custom timeout does not set error message when timeout occurs, see #970", function() { 1.810 + stop(); 1.811 + $.ajax({ 1.812 + url: "data/name.php?wait=10", 1.813 + timeout: 500, 1.814 + error: function(request, status) { 1.815 + ok( status != null, "status shouldn't be null in error handler" ); 1.816 + equals( "timeout", status ); 1.817 + start(); 1.818 + } 1.819 + }); 1.820 +}); 1.821 + 1.822 +test("data option: evaluate function values (#2806)", function() { 1.823 + stop(); 1.824 + $.ajax({ 1.825 + url: "data/echoQuery.php", 1.826 + data: { 1.827 + key: function() { 1.828 + return "value"; 1.829 + } 1.830 + }, 1.831 + success: function(result) { 1.832 + equals( result, "key=value" ); 1.833 + start(); 1.834 + } 1.835 + }) 1.836 +}); 1.837 +*/ 1.838 +} 1.839 + 1.840 +//}