dom/tests/mochitest/ajax/jquery/test/unit/ajax.js

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 module("ajax");
michael@0 2
michael@0 3 // Safari 3 randomly crashes when running these tests,
michael@0 4 // but only in the full suite - you can run just the Ajax
michael@0 5 // tests and they'll pass
michael@0 6 //if ( !jQuery.browser.safari ) {
michael@0 7
michael@0 8 if ( !isLocal ) {
michael@0 9
michael@0 10 test("$.ajax() - success callbacks", function() {
michael@0 11 expect( 8 );
michael@0 12
michael@0 13 $.ajaxSetup({ timeout: 0 });
michael@0 14
michael@0 15 stop();
michael@0 16
michael@0 17 setTimeout(function(){
michael@0 18 $('#foo').ajaxStart(function(){
michael@0 19 ok( true, "ajaxStart" );
michael@0 20 }).ajaxStop(function(){
michael@0 21 ok( true, "ajaxStop" );
michael@0 22 start();
michael@0 23 }).ajaxSend(function(){
michael@0 24 ok( true, "ajaxSend" );
michael@0 25 }).ajaxComplete(function(){
michael@0 26 ok( true, "ajaxComplete" );
michael@0 27 }).ajaxError(function(){
michael@0 28 ok( false, "ajaxError" );
michael@0 29 }).ajaxSuccess(function(){
michael@0 30 ok( true, "ajaxSuccess" );
michael@0 31 });
michael@0 32
michael@0 33 $.ajax({
michael@0 34 url: url("data/name.html"),
michael@0 35 beforeSend: function(){ ok(true, "beforeSend"); },
michael@0 36 success: function(){ ok(true, "success"); },
michael@0 37 error: function(){ ok(false, "error"); },
michael@0 38 complete: function(){ ok(true, "complete"); }
michael@0 39 });
michael@0 40 }, 13);
michael@0 41 });
michael@0 42
michael@0 43 /* mozilla: the ajaxSuccess part fails intermittently on MacOSX
michael@0 44
michael@0 45 test("$.ajax() - error callbacks", function() {
michael@0 46 expect( 7 );
michael@0 47 stop();
michael@0 48
michael@0 49 $('#foo').ajaxStart(function(){
michael@0 50 ok( true, "ajaxStart" );
michael@0 51 }).ajaxStop(function(){
michael@0 52 ok( true, "ajaxStop" );
michael@0 53 start();
michael@0 54 }).ajaxSend(function(){
michael@0 55 ok( true, "ajaxSend" );
michael@0 56 }).ajaxComplete(function(){
michael@0 57 ok( true, "ajaxComplete" );
michael@0 58 }).ajaxError(function(){
michael@0 59 ok( true, "ajaxError" );
michael@0 60 }).ajaxSuccess(function(){
michael@0 61 ok( false, "ajaxSuccess" );
michael@0 62 })
michael@0 63 ;
michael@0 64
michael@0 65 $.ajaxSetup({ timeout: 500 });
michael@0 66
michael@0 67 $.ajax({
michael@0 68 url: url("data/name.php?wait=5"),
michael@0 69 beforeSend: function(){ ok(true, "beforeSend"); },
michael@0 70 success: function(){ ok(false, "success"); },
michael@0 71 error: function(){ ok(true, "error"); },
michael@0 72 complete: function(){ ok(true, "complete"); }
michael@0 73 });
michael@0 74 });
michael@0 75
michael@0 76 */
michael@0 77
michael@0 78 test("$.ajax() - disabled globals", function() {
michael@0 79 expect( 3 );
michael@0 80 stop();
michael@0 81
michael@0 82 $('#foo').ajaxStart(function(){
michael@0 83 ok( false, "ajaxStart" );
michael@0 84 }).ajaxStop(function(){
michael@0 85 ok( false, "ajaxStop" );
michael@0 86 }).ajaxSend(function(){
michael@0 87 ok( false, "ajaxSend" );
michael@0 88 }).ajaxComplete(function(){
michael@0 89 ok( false, "ajaxComplete" );
michael@0 90 }).ajaxError(function(){
michael@0 91 ok( false, "ajaxError" );
michael@0 92 }).ajaxSuccess(function(){
michael@0 93 ok( false, "ajaxSuccess" );
michael@0 94 });
michael@0 95
michael@0 96 $.ajax({
michael@0 97 global: false,
michael@0 98 url: url("data/name.html"),
michael@0 99 beforeSend: function(){ ok(true, "beforeSend"); },
michael@0 100 success: function(){ ok(true, "success"); },
michael@0 101 error: function(){ ok(false, "error"); },
michael@0 102 complete: function(){
michael@0 103 ok(true, "complete");
michael@0 104 setTimeout(function(){ start(); }, 13);
michael@0 105 }
michael@0 106 });
michael@0 107 });
michael@0 108
michael@0 109 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
michael@0 110 expect(3);
michael@0 111 stop();
michael@0 112 $.ajax({
michael@0 113 url: url("data/with_fries.xml"),
michael@0 114 dataType: "xml",
michael@0 115 success: function(resp) {
michael@0 116 equals( $("properties", resp).length, 1, 'properties in responseXML' );
michael@0 117 equals( $("jsconf", resp).length, 1, 'jsconf in responseXML' );
michael@0 118 equals( $("thing", resp).length, 2, 'things in responseXML' );
michael@0 119 start();
michael@0 120 }
michael@0 121 });
michael@0 122 });
michael@0 123
michael@0 124 test("$.ajax - beforeSend", function() {
michael@0 125 expect(1);
michael@0 126 stop();
michael@0 127
michael@0 128 var check = false;
michael@0 129
michael@0 130 $.ajaxSetup({ timeout: 0 });
michael@0 131
michael@0 132 $.ajax({
michael@0 133 url: url("data/name.html"),
michael@0 134 beforeSend: function(xml) {
michael@0 135 check = true;
michael@0 136 },
michael@0 137 success: function(data) {
michael@0 138 ok( check, "check beforeSend was executed" );
michael@0 139 start();
michael@0 140 }
michael@0 141 });
michael@0 142 });
michael@0 143
michael@0 144 test("$.ajax - beforeSend, cancel request (#2688)", function() {
michael@0 145 expect(2);
michael@0 146 var request = $.ajax({
michael@0 147 url: url("data/name.html"),
michael@0 148 beforeSend: function() {
michael@0 149 ok( true, "beforeSend got called, canceling" );
michael@0 150 return false;
michael@0 151 },
michael@0 152 success: function() {
michael@0 153 ok( false, "request didn't get canceled" );
michael@0 154 },
michael@0 155 complete: function() {
michael@0 156 ok( false, "request didn't get canceled" );
michael@0 157 },
michael@0 158 error: function() {
michael@0 159 ok( false, "request didn't get canceled" );
michael@0 160 }
michael@0 161 });
michael@0 162 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
michael@0 163 });
michael@0 164
michael@0 165 var foobar;
michael@0 166
michael@0 167 test("$.ajax - dataType html", function() {
michael@0 168 expect(5);
michael@0 169 stop();
michael@0 170
michael@0 171 foobar = null;
michael@0 172 testFoo = undefined;
michael@0 173
michael@0 174 var verifyEvaluation = function() {
michael@0 175 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
michael@0 176 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
michael@0 177 start();
michael@0 178 };
michael@0 179
michael@0 180 $.ajax({
michael@0 181 dataType: "html",
michael@0 182 url: url("data/test.html"),
michael@0 183 success: function(data) {
michael@0 184 $("#ap").html(data);
michael@0 185 ok( data.match(/^html text/), 'Check content for datatype html' );
michael@0 186 setTimeout(verifyEvaluation, 600);
michael@0 187 }
michael@0 188 });
michael@0 189 });
michael@0 190
michael@0 191 test("serialize()", function() {
michael@0 192 expect(6);
michael@0 193
michael@0 194 equals( $('#form').serialize(),
michael@0 195 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2",
michael@0 196 'Check form serialization as query string');
michael@0 197
michael@0 198 equals( $('#form :input').serialize(),
michael@0 199 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2",
michael@0 200 'Check input serialization as query string');
michael@0 201
michael@0 202 equals( $('#testForm').serialize(),
michael@0 203 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
michael@0 204 'Check form serialization as query string');
michael@0 205
michael@0 206 equals( $('#testForm :input').serialize(),
michael@0 207 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
michael@0 208 'Check input serialization as query string');
michael@0 209
michael@0 210 equals( $('#form, #testForm').serialize(),
michael@0 211 "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 212 'Multiple form serialization as query string');
michael@0 213
michael@0 214 equals( $('#form, #testForm :input').serialize(),
michael@0 215 "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 216 'Mixed form/input serialization as query string');
michael@0 217 });
michael@0 218
michael@0 219 test("$.param()", function() {
michael@0 220 expect(4);
michael@0 221 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
michael@0 222 equals( $.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
michael@0 223
michael@0 224 params = {someName: [1, 2, 3], regularThing: "blah" };
michael@0 225 equals( $.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
michael@0 226
michael@0 227 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
michael@0 228 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 229
michael@0 230 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
michael@0 231 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 232 });
michael@0 233
michael@0 234 test("synchronous request", function() {
michael@0 235 expect(1);
michael@0 236 ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
michael@0 237 });
michael@0 238
michael@0 239 test("synchronous request with callbacks", function() {
michael@0 240 expect(2);
michael@0 241 var result;
michael@0 242 $.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "success callback executed"); result = data; } });
michael@0 243 ok( /^{ "data"/.test( result ), "check returned text" );
michael@0 244 });
michael@0 245
michael@0 246 test("pass-through request object", function() {
michael@0 247 expect(8);
michael@0 248 stop(true);
michael@0 249
michael@0 250 var target = "data/name.html";
michael@0 251 var successCount = 0;
michael@0 252 var errorCount = 0;
michael@0 253 var errorEx = "";
michael@0 254 var success = function() {
michael@0 255 successCount++;
michael@0 256 };
michael@0 257 $("#foo").ajaxError(function (e, xml, s, ex) {
michael@0 258 errorCount++;
michael@0 259 errorEx += ": " + xml.status;
michael@0 260 });
michael@0 261 $("#foo").one('ajaxStop', function () {
michael@0 262 equals(successCount, 5, "Check all ajax calls successful");
michael@0 263 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
michael@0 264 $("#foo").unbind('ajaxError');
michael@0 265 start();
michael@0 266 });
michael@0 267
michael@0 268 ok( $.get(url(target), success), "get" );
michael@0 269 ok( $.post(url(target), success), "post" );
michael@0 270 ok( $.getScript(url("data/test.js"), success), "script" );
michael@0 271 ok( $.getJSON(url("data/json_obj.js"), success), "json" );
michael@0 272 ok( $.ajax({url: url(target), success: success}), "generic" );
michael@0 273 });
michael@0 274
michael@0 275 /* mozilla: php not currently supported in mochitest (08/08/2008)
michael@0 276 test("ajax cache", function () {
michael@0 277 expect(18);
michael@0 278 stop();
michael@0 279
michael@0 280 var count = 0;
michael@0 281
michael@0 282 $("#firstp").bind("ajaxSuccess", function (e, xml, s) {
michael@0 283 var re = /_=(.*?)(&|$)/g;
michael@0 284 var oldOne = null;
michael@0 285 for (var i = 0; i < 6; i++) {
michael@0 286 var ret = re.exec(s.url);
michael@0 287 if (!ret) {
michael@0 288 break;
michael@0 289 }
michael@0 290 oldOne = ret[1];
michael@0 291 }
michael@0 292 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
michael@0 293 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
michael@0 294 if(++count == 6)
michael@0 295 start();
michael@0 296 });
michael@0 297
michael@0 298 ok( $.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
michael@0 299 ok( $.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
michael@0 300 ok( $.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
michael@0 301 ok( $.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
michael@0 302 ok( $.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
michael@0 303 ok( $.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
michael@0 304 });
michael@0 305 */
michael@0 306
michael@0 307 test("global ajaxSettings", function() {
michael@0 308 expect(2);
michael@0 309
michael@0 310 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
michael@0 311 var orig = { url: "data/with_fries.xml" };
michael@0 312 var t;
michael@0 313
michael@0 314 $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
michael@0 315
michael@0 316 t = jQuery.extend({}, orig);
michael@0 317 t.data = {};
michael@0 318 $.ajax(t);
michael@0 319 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
michael@0 320
michael@0 321 t = jQuery.extend({}, orig);
michael@0 322 t.data = { zoo: 'a', ping: 'b' };
michael@0 323 $.ajax(t);
michael@0 324 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 325
michael@0 326 jQuery.ajaxSettings = tmp;
michael@0 327 });
michael@0 328
michael@0 329 test("load(String)", function() {
michael@0 330 expect(1);
michael@0 331 stop(true); // check if load can be called with only url
michael@0 332 $('#first').load("data/name.html", start);
michael@0 333 });
michael@0 334
michael@0 335 test("load('url selector')", function() {
michael@0 336 expect(1);
michael@0 337 stop(true); // check if load can be called with only url
michael@0 338 $('#first').load("data/test3.html div.user", function(){
michael@0 339 equals( $(this).children("div").length, 2, "Verify that specific elements were injected" );
michael@0 340 start();
michael@0 341 });
michael@0 342 });
michael@0 343
michael@0 344 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
michael@0 345 expect(1);
michael@0 346 stop();
michael@0 347 $.ajaxSetup({ dataType: "json" });
michael@0 348 $("#first").ajaxComplete(function (e, xml, s) {
michael@0 349 equals( s.dataType, "html", "Verify the load() dataType was html" );
michael@0 350 $("#first").unbind("ajaxComplete");
michael@0 351 $.ajaxSetup({ dataType: "" });
michael@0 352 start();
michael@0 353 });
michael@0 354 $('#first').load("data/test3.html");
michael@0 355 });
michael@0 356
michael@0 357 test("load(String, Function) - simple: inject text into DOM", function() {
michael@0 358 expect(2);
michael@0 359 stop();
michael@0 360 $('#first').load(url("data/name.html"), function() {
michael@0 361 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
michael@0 362 start();
michael@0 363 });
michael@0 364 });
michael@0 365
michael@0 366 test("load(String, Function) - check scripts", function() {
michael@0 367 expect(7);
michael@0 368 stop();
michael@0 369 window.testFoo = undefined;
michael@0 370 window.foobar = null;
michael@0 371 var verifyEvaluation = function() {
michael@0 372 equals( foobar, "bar", 'Check if script src was evaluated after load' );
michael@0 373 equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
michael@0 374 start();
michael@0 375 };
michael@0 376 $('#first').load(url('data/test.html'), function() {
michael@0 377 ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
michael@0 378 equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
michael@0 379 equals( testFoo, "foo", 'Check if script was evaluated after load' );
michael@0 380 setTimeout(verifyEvaluation, 600);
michael@0 381 });
michael@0 382 });
michael@0 383
michael@0 384 test("load(String, Function) - check file with only a script tag", function() {
michael@0 385 expect(3);
michael@0 386 stop();
michael@0 387 testFoo = undefined;
michael@0 388 $('#first').load(url('data/test2.html'), function() {
michael@0 389 equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
michael@0 390 equals( testFoo, "foo", 'Check if script was evaluated after load' );
michael@0 391 start();
michael@0 392 });
michael@0 393 });
michael@0 394
michael@0 395 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
michael@0 396 expect(2);
michael@0 397 stop();
michael@0 398 $.get(url('data/dashboard.xml'), function(xml) {
michael@0 399 var content = [];
michael@0 400 $('tab', xml).each(function() {
michael@0 401 content.push($(this).text());
michael@0 402 });
michael@0 403 equals( content[0], 'blabla', 'Check first tab');
michael@0 404 equals( content[1], 'blublu', 'Check second tab');
michael@0 405 start();
michael@0 406 });
michael@0 407 });
michael@0 408
michael@0 409 test("$.getScript(String, Function) - with callback", function() {
michael@0 410 expect(2);
michael@0 411 stop();
michael@0 412 window.foobar = null;
michael@0 413 $.getScript(url("data/test.js"), function() {
michael@0 414 equals( foobar, "bar", 'Check if script was evaluated' );
michael@0 415 setTimeout(start, 100);
michael@0 416 });
michael@0 417 });
michael@0 418
michael@0 419 test("$.getScript(String, Function) - no callback", function() {
michael@0 420 expect(1);
michael@0 421 stop(true);
michael@0 422 $.getScript(url("data/test.js"), start);
michael@0 423 });
michael@0 424
michael@0 425 /* mozilla: Tests using php scripts not currently supported (06/26/2008)
michael@0 426
michael@0 427 test("$.ajax() - JSONP, Local", function() {
michael@0 428 expect(7);
michael@0 429
michael@0 430 var count = 0;
michael@0 431 function plus(){ if ( ++count == 7 ) start(); }
michael@0 432
michael@0 433 stop();
michael@0 434
michael@0 435 $.ajax({
michael@0 436 url: "data/jsonp.php",
michael@0 437 dataType: "jsonp",
michael@0 438 success: function(data){
michael@0 439 ok( data.data, "JSON results returned (GET, no callback)" );
michael@0 440 plus();
michael@0 441 },
michael@0 442 error: function(data){
michael@0 443 ok( false, "Ajax error JSON (GET, no callback)" );
michael@0 444 plus();
michael@0 445 }
michael@0 446 });
michael@0 447
michael@0 448 $.ajax({
michael@0 449 url: "data/jsonp.php?callback=?",
michael@0 450 dataType: "jsonp",
michael@0 451 success: function(data){
michael@0 452 ok( data.data, "JSON results returned (GET, url callback)" );
michael@0 453 plus();
michael@0 454 },
michael@0 455 error: function(data){
michael@0 456 ok( false, "Ajax error JSON (GET, url callback)" );
michael@0 457 plus();
michael@0 458 }
michael@0 459 });
michael@0 460
michael@0 461 $.ajax({
michael@0 462 url: "data/jsonp.php",
michael@0 463 dataType: "jsonp",
michael@0 464 data: "callback=?",
michael@0 465 success: function(data){
michael@0 466 ok( data.data, "JSON results returned (GET, data callback)" );
michael@0 467 plus();
michael@0 468 },
michael@0 469 error: function(data){
michael@0 470 ok( false, "Ajax error JSON (GET, data callback)" );
michael@0 471 plus();
michael@0 472 }
michael@0 473 });
michael@0 474
michael@0 475 $.ajax({
michael@0 476 url: "data/jsonp.php",
michael@0 477 dataType: "jsonp",
michael@0 478 jsonp: "callback",
michael@0 479 success: function(data){
michael@0 480 ok( data.data, "JSON results returned (GET, data obj callback)" );
michael@0 481 plus();
michael@0 482 },
michael@0 483 error: function(data){
michael@0 484 ok( false, "Ajax error JSON (GET, data obj callback)" );
michael@0 485 plus();
michael@0 486 }
michael@0 487 });
michael@0 488
michael@0 489 $.ajax({
michael@0 490 type: "POST",
michael@0 491 url: "data/jsonp.php",
michael@0 492 dataType: "jsonp",
michael@0 493 success: function(data){
michael@0 494 ok( data.data, "JSON results returned (POST, no callback)" );
michael@0 495 plus();
michael@0 496 },
michael@0 497 error: function(data){
michael@0 498 ok( false, "Ajax error JSON (GET, data obj callback)" );
michael@0 499 plus();
michael@0 500 }
michael@0 501 });
michael@0 502
michael@0 503 $.ajax({
michael@0 504 type: "POST",
michael@0 505 url: "data/jsonp.php",
michael@0 506 data: "callback=?",
michael@0 507 dataType: "jsonp",
michael@0 508 success: function(data){
michael@0 509 ok( data.data, "JSON results returned (POST, data callback)" );
michael@0 510 plus();
michael@0 511 },
michael@0 512 error: function(data){
michael@0 513 ok( false, "Ajax error JSON (POST, data callback)" );
michael@0 514 plus();
michael@0 515 }
michael@0 516 });
michael@0 517
michael@0 518 $.ajax({
michael@0 519 type: "POST",
michael@0 520 url: "data/jsonp.php",
michael@0 521 jsonp: "callback",
michael@0 522 dataType: "jsonp",
michael@0 523 success: function(data){
michael@0 524 ok( data.data, "JSON results returned (POST, data obj callback)" );
michael@0 525 plus();
michael@0 526 },
michael@0 527 error: function(data){
michael@0 528 ok( false, "Ajax error JSON (POST, data obj callback)" );
michael@0 529 plus();
michael@0 530 }
michael@0 531 });
michael@0 532 });
michael@0 533
michael@0 534 test("$.ajax() - JSONP, Remote", function() {
michael@0 535 expect(4);
michael@0 536
michael@0 537 var count = 0;
michael@0 538 function plus(){ if ( ++count == 4 ) start(); }
michael@0 539
michael@0 540 var base = window.location.href.replace(/\?.*$/, "");
michael@0 541
michael@0 542 stop();
michael@0 543
michael@0 544 $.ajax({
michael@0 545 url: base + "data/jsonp.php",
michael@0 546 dataType: "jsonp",
michael@0 547 success: function(data){
michael@0 548 ok( data.data, "JSON results returned (GET, no callback)" );
michael@0 549 plus();
michael@0 550 },
michael@0 551 error: function(data){
michael@0 552 ok( false, "Ajax error JSON (GET, no callback)" );
michael@0 553 plus();
michael@0 554 }
michael@0 555 });
michael@0 556
michael@0 557 $.ajax({
michael@0 558 url: base + "data/jsonp.php?callback=?",
michael@0 559 dataType: "jsonp",
michael@0 560 success: function(data){
michael@0 561 ok( data.data, "JSON results returned (GET, url callback)" );
michael@0 562 plus();
michael@0 563 },
michael@0 564 error: function(data){
michael@0 565 ok( false, "Ajax error JSON (GET, url callback)" );
michael@0 566 plus();
michael@0 567 }
michael@0 568 });
michael@0 569
michael@0 570 $.ajax({
michael@0 571 url: base + "data/jsonp.php",
michael@0 572 dataType: "jsonp",
michael@0 573 data: "callback=?",
michael@0 574 success: function(data){
michael@0 575 ok( data.data, "JSON results returned (GET, data callback)" );
michael@0 576 plus();
michael@0 577 },
michael@0 578 error: function(data){
michael@0 579 ok( false, "Ajax error JSON (GET, data callback)" );
michael@0 580 plus();
michael@0 581 }
michael@0 582 });
michael@0 583
michael@0 584 $.ajax({
michael@0 585 url: base + "data/jsonp.php",
michael@0 586 dataType: "jsonp",
michael@0 587 jsonp: "callback",
michael@0 588 success: function(data){
michael@0 589 ok( data.data, "JSON results returned (GET, data obj callback)" );
michael@0 590 plus();
michael@0 591 },
michael@0 592 error: function(data){
michael@0 593 ok( false, "Ajax error JSON (GET, data obj callback)" );
michael@0 594 plus();
michael@0 595 }
michael@0 596 });
michael@0 597 });
michael@0 598
michael@0 599 test("$.ajax() - script, Remote", function() {
michael@0 600 expect(2);
michael@0 601
michael@0 602 var base = window.location.href.replace(/\?.*$/, "");
michael@0 603
michael@0 604 stop();
michael@0 605
michael@0 606 window.foobar = null;
michael@0 607 $.ajax({
michael@0 608 url: base + "data/test.js",
michael@0 609 dataType: "script",
michael@0 610 success: function(data){
michael@0 611 ok( foobar, "Script results returned (GET, no callback)" );
michael@0 612 start();
michael@0 613 }
michael@0 614 });
michael@0 615 });
michael@0 616
michael@0 617 test("$.ajax() - script, Remote with POST", function() {
michael@0 618 expect(3);
michael@0 619
michael@0 620 var base = window.location.href.replace(/\?.*$/, "");
michael@0 621
michael@0 622 stop();
michael@0 623
michael@0 624 window.foobar = null;
michael@0 625 $.ajax({
michael@0 626 url: base + "data/test.js",
michael@0 627 type: "POST",
michael@0 628 dataType: "script",
michael@0 629 success: function(data, status){
michael@0 630 ok( foobar, "Script results returned (GET, no callback)" );
michael@0 631 equals( status, "success", "Script results returned (GET, no callback)" );
michael@0 632 start();
michael@0 633 }
michael@0 634 });
michael@0 635 });
michael@0 636
michael@0 637 test("$.ajax() - script, Remote with scheme-less URL", function() {
michael@0 638 expect(2);
michael@0 639
michael@0 640 var base = window.location.href.replace(/\?.*$/, "");
michael@0 641 base = base.replace(/^.*?\/\//, "//");
michael@0 642
michael@0 643 stop();
michael@0 644
michael@0 645 window.foobar = null;
michael@0 646 $.ajax({
michael@0 647 url: base + "data/test.js",
michael@0 648 dataType: "script",
michael@0 649 success: function(data){
michael@0 650 ok( foobar, "Script results returned (GET, no callback)" );
michael@0 651 start();
michael@0 652 }
michael@0 653 });
michael@0 654 });
michael@0 655
michael@0 656 test("$.getJSON(String, Hash, Function) - JSON array", function() {
michael@0 657 expect(4);
michael@0 658 stop();
michael@0 659 $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
michael@0 660 equals( json[0].name, 'John', 'Check JSON: first, name' );
michael@0 661 equals( json[0].age, 21, 'Check JSON: first, age' );
michael@0 662 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
michael@0 663 equals( json[1].age, 25, 'Check JSON: second, age' );
michael@0 664 start();
michael@0 665 });
michael@0 666 });
michael@0 667
michael@0 668 test("$.getJSON(String, Function) - JSON object", function() {
michael@0 669 expect(2);
michael@0 670 stop();
michael@0 671 $.getJSON(url("data/json.php"), function(json) {
michael@0 672 equals( json.data.lang, 'en', 'Check JSON: lang' );
michael@0 673 equals( json.data.length, 25, 'Check JSON: length' );
michael@0 674 start();
michael@0 675 });
michael@0 676 });
michael@0 677
michael@0 678 test("$.getJSON(String, Function) - JSON object with absolute url to local content", function() {
michael@0 679 expect(2);
michael@0 680
michael@0 681 var base = window.location.href.replace(/\?.*$/, "");
michael@0 682
michael@0 683 stop();
michael@0 684 $.getJSON(url(base + "data/json.php"), function(json) {
michael@0 685 equals( json.data.lang, 'en', 'Check JSON: lang' );
michael@0 686 equals( json.data.length, 25, 'Check JSON: length' );
michael@0 687 start();
michael@0 688 });
michael@0 689 });
michael@0 690
michael@0 691 test("$.post(String, Hash, Function) - simple with xml", function() {
michael@0 692 expect(4);
michael@0 693 stop();
michael@0 694 $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
michael@0 695 $('math', xml).each(function() {
michael@0 696 equals( $('calculation', this).text(), '5-2', 'Check for XML' );
michael@0 697 equals( $('result', this).text(), '3', 'Check for XML' );
michael@0 698 });
michael@0 699 });
michael@0 700
michael@0 701 $.post(url("data/name.php?xml=5-2"), {}, function(xml){
michael@0 702 $('math', xml).each(function() {
michael@0 703 equals( $('calculation', this).text(), '5-2', 'Check for XML' );
michael@0 704 equals( $('result', this).text(), '3', 'Check for XML' );
michael@0 705 });
michael@0 706 start();
michael@0 707 });
michael@0 708 });
michael@0 709
michael@0 710 test("$.ajaxSetup({timeout: Number}) - with global timeout", function() {
michael@0 711 stop();
michael@0 712
michael@0 713 var passed = 0;
michael@0 714
michael@0 715 $.ajaxSetup({timeout: 1000});
michael@0 716
michael@0 717 var pass = function() {
michael@0 718 passed++;
michael@0 719 if ( passed == 2 ) {
michael@0 720 ok( true, 'Check local and global callbacks after timeout' );
michael@0 721 $('#main').unbind("ajaxError");
michael@0 722 start();
michael@0 723 }
michael@0 724 };
michael@0 725
michael@0 726 var fail = function(a,b,c) {
michael@0 727 ok( false, 'Check for timeout failed ' + a + ' ' + b );
michael@0 728 start();
michael@0 729 };
michael@0 730
michael@0 731 $('#main').ajaxError(pass);
michael@0 732
michael@0 733 $.ajax({
michael@0 734 type: "GET",
michael@0 735 url: url("data/name.php?wait=5"),
michael@0 736 error: pass,
michael@0 737 success: fail
michael@0 738 });
michael@0 739
michael@0 740 // reset timeout
michael@0 741 $.ajaxSetup({timeout: 0});
michael@0 742 });
michael@0 743
michael@0 744 test("$.ajaxSetup({timeout: Number}) with localtimeout", function() {
michael@0 745 stop();
michael@0 746 $.ajaxSetup({timeout: 50});
michael@0 747
michael@0 748 $.ajax({
michael@0 749 type: "GET",
michael@0 750 timeout: 5000,
michael@0 751 url: url("data/name.php?wait=1"),
michael@0 752 error: function() {
michael@0 753 ok( false, 'Check for local timeout failed' );
michael@0 754 start();
michael@0 755 },
michael@0 756 success: function() {
michael@0 757 ok( true, 'Check for local timeout' );
michael@0 758 start();
michael@0 759 }
michael@0 760 });
michael@0 761
michael@0 762 // reset timeout
michael@0 763 $.ajaxSetup({timeout: 0});
michael@0 764 });
michael@0 765
michael@0 766 test("$.ajax - simple get", function() {
michael@0 767 expect(1);
michael@0 768 stop();
michael@0 769 $.ajax({
michael@0 770 type: "GET",
michael@0 771 url: url("data/name.php?name=foo"),
michael@0 772 success: function(msg){
michael@0 773 equals( msg, 'bar', 'Check for GET' );
michael@0 774 start();
michael@0 775 }
michael@0 776 });
michael@0 777 });
michael@0 778
michael@0 779 test("$.ajax - simple post", function() {
michael@0 780 expect(1);
michael@0 781 stop();
michael@0 782 $.ajax({
michael@0 783 type: "POST",
michael@0 784 url: url("data/name.php"),
michael@0 785 data: "name=peter",
michael@0 786 success: function(msg){
michael@0 787 equals( msg, 'pan', 'Check for POST' );
michael@0 788 start();
michael@0 789 }
michael@0 790 });
michael@0 791 });
michael@0 792
michael@0 793 test("ajaxSetup()", function() {
michael@0 794 expect(1);
michael@0 795 stop();
michael@0 796 $.ajaxSetup({
michael@0 797 url: url("data/name.php?name=foo"),
michael@0 798 success: function(msg){
michael@0 799 equals( msg, 'bar', 'Check for GET' );
michael@0 800 start();
michael@0 801 }
michael@0 802 });
michael@0 803 $.ajax();
michael@0 804 });
michael@0 805
michael@0 806 test("custom timeout does not set error message when timeout occurs, see #970", function() {
michael@0 807 stop();
michael@0 808 $.ajax({
michael@0 809 url: "data/name.php?wait=10",
michael@0 810 timeout: 500,
michael@0 811 error: function(request, status) {
michael@0 812 ok( status != null, "status shouldn't be null in error handler" );
michael@0 813 equals( "timeout", status );
michael@0 814 start();
michael@0 815 }
michael@0 816 });
michael@0 817 });
michael@0 818
michael@0 819 test("data option: evaluate function values (#2806)", function() {
michael@0 820 stop();
michael@0 821 $.ajax({
michael@0 822 url: "data/echoQuery.php",
michael@0 823 data: {
michael@0 824 key: function() {
michael@0 825 return "value";
michael@0 826 }
michael@0 827 },
michael@0 828 success: function(result) {
michael@0 829 equals( result, "key=value" );
michael@0 830 start();
michael@0 831 }
michael@0 832 })
michael@0 833 });
michael@0 834 */
michael@0 835 }
michael@0 836
michael@0 837 //}

mercurial