3 // Safari 3 randomly crashes when running these tests,
4 // but only in the full suite - you can run just the Ajax
5 // tests and they'll pass
6 if ( !jQuery.browser.safari ) {
8 test("$.ajax() - success callbacks", function() {
11 $.ajaxSetup({ timeout: 0 });
15 setTimeout(function(){
16 $('#foo').ajaxStart(function(){
17 ok( true, "ajaxStart" );
18 }).ajaxStop(function(){
19 ok( true, "ajaxStop" );
21 }).ajaxSend(function(){
22 ok( true, "ajaxSend" );
23 }).ajaxComplete(function(){
24 ok( true, "ajaxComplete" );
25 }).ajaxError(function(){
26 ok( false, "ajaxError" );
27 }).ajaxSuccess(function(){
28 ok( true, "ajaxSuccess" );
32 url: url("data/name.html"),
33 beforeSend: function(){ ok(true, "beforeSend"); },
34 success: function(){ ok(true, "success"); },
35 error: function(){ ok(false, "error"); },
36 complete: function(){ ok(true, "complete"); }
42 test("$.ajax() - error callbacks", function() {
46 $('#foo').ajaxStart(function(){
47 ok( true, "ajaxStart" );
48 }).ajaxStop(function(){
49 ok( true, "ajaxStop" );
51 }).ajaxSend(function(){
52 ok( true, "ajaxSend" );
53 }).ajaxComplete(function(){
54 ok( true, "ajaxComplete" );
55 }).ajaxError(function(){
56 ok( true, "ajaxError" );
57 }).ajaxSuccess(function(){
58 ok( false, "ajaxSuccess" );
61 $.ajaxSetup({ timeout: 500 });
64 url: url("data/name.php?wait=5"),
65 beforeSend: function(){ ok(true, "beforeSend"); },
66 success: function(){ ok(false, "success"); },
67 error: function(){ ok(true, "error"); },
68 complete: function(){ ok(true, "complete"); }
73 test("$.ajax() - disabled globals", function() {
77 $('#foo').ajaxStart(function(){
78 ok( false, "ajaxStart" );
79 }).ajaxStop(function(){
80 ok( false, "ajaxStop" );
81 }).ajaxSend(function(){
82 ok( false, "ajaxSend" );
83 }).ajaxComplete(function(){
84 ok( false, "ajaxComplete" );
85 }).ajaxError(function(){
86 ok( false, "ajaxError" );
87 }).ajaxSuccess(function(){
88 ok( false, "ajaxSuccess" );
93 url: url("data/name.html"),
94 beforeSend: function(){ ok(true, "beforeSend"); },
95 success: function(){ ok(true, "success"); },
96 error: function(){ ok(false, "error"); },
99 setTimeout(function(){ start(); }, 13);
104 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
108 url: url("data/with_fries.xml"),
110 success: function(resp) {
111 equals( $("properties", resp).length, 1, 'properties in responseXML' );
112 equals( $("jsconf", resp).length, 1, 'jsconf in responseXML' );
113 equals( $("thing", resp).length, 2, 'things in responseXML' );
119 test("$.ajax - beforeSend", function() {
125 $.ajaxSetup({ timeout: 0 });
128 url: url("data/name.html"),
129 beforeSend: function(xml) {
132 success: function(data) {
133 ok( check, "check beforeSend was executed" );
141 test("$.ajax - dataType html", function() {
148 var verifyEvaluation = function() {
149 ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
150 ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
156 url: url("data/test.html"),
157 success: function(data) {
159 ok( data.match(/^html text/), 'Check content for datatype html' );
160 setTimeout(verifyEvaluation, 600);
165 test("serialize()", function() {
167 // ignore button, IE takes text content as value, not relevant for this test
168 equals( $(':input').not('button').serialize(),
169 'action=Test&text2=Test&radio1=on&radio2=on&check=on&=on&hidden=&foo%5Bbar%5D=&name=name&=foobar&select1=&select2=3&select3=1&test=&id=',
170 'Check form serialization as query string');
173 test("$.param()", function() {
175 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
176 equals( $.param(params), "foo=bar&baz=42&quux=All%20your%20base%20are%20belong%20to%20us", "simple" );
178 params = {someName: [1, 2, 3], regularThing: "blah" };
179 equals( $.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
181 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
182 equals( $.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All%20your%20base%20are%20belong%20to%20us", "more array" );
184 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
185 equals( $.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All%20your%20base%20are%20belong%20to%20us", "even more arrays" );
188 test("synchronous request", function() {
190 ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
193 test("synchronous request with callbacks", function() {
196 $.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
197 ok( /^{ "data"/.test( result ), "check returned text" );
200 test("pass-through request object", function() {
204 var target = "data/name.html";
206 var success = function() {
212 /* Test disabled, too many simultaneous requests
213 ok( $.get(url(target), success), "get" );
214 ok( $.post(url(target), success), "post" );
215 ok( $.getScript(url("data/test.js"), success), "script" );
216 ok( $.getJSON(url("data/json_obj.js"), success), "json" );
218 ok( $.ajax({url: url(target), success: success}), "generic" );
221 test("global ajaxSettings", function() {
224 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
225 var orig = { url: "data/with_fries.xml", data: null };
228 $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
230 t = jQuery.extend({}, orig);
232 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending null" );
234 t = jQuery.extend({}, orig);
237 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
239 t = jQuery.extend({}, orig);
240 t.data = { zoo: 'a', ping: 'b' };
242 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' }" );
244 jQuery.ajaxSettings = tmp;
247 test("load(String)", function() {
249 stop(true); // check if load can be called with only url
250 $('#first').load("data/name.html", start);
253 test("load('url selector')", function() {
255 stop(true); // check if load can be called with only url
256 $('#first').load("data/test3.html div.user", function(){
257 equals( $(this).children("div").length, 2, "Verify that specific elements were injected" );
262 test("load(String, Function) - simple: inject text into DOM", function() {
265 $('#first').load(url("data/name.html"), function() {
266 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
271 test("load(String, Function) - check scripts", function() {
274 window.testFoo = undefined;
275 window.foobar = null;
276 var verifyEvaluation = function() {
277 equals( foobar, "bar", 'Check if script src was evaluated after load' );
278 equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
281 $('#first').load(url('data/test.html'), function() {
282 ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
283 equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
284 equals( testFoo, "foo", 'Check if script was evaluated after load' );
285 setTimeout(verifyEvaluation, 600);
289 test("load(String, Function) - check file with only a script tag", function() {
293 $('#first').load(url('data/test2.html'), function() {
294 ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
295 ok( testFoo == "foo", 'Check if script was evaluated after load' );
300 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
303 $.get(url('data/dashboard.xml'), function(xml) {
305 $('tab', xml).each(function() {
306 content.push($(this).text());
308 equals( content[0], 'blabla', 'Check first tab');
309 equals( content[1], 'blublu', 'Check second tab');
314 test("$.getScript(String, Function) - with callback", function() {
317 $.getScript(url("data/test.js"), function() {
318 equals( foobar, "bar", 'Check if script was evaluated' );
319 setTimeout(start, 100);
323 test("$.getScript(String, Function) - no callback", function() {
326 $.getScript(url("data/test.js"), start);
331 test("$.ajax() - JSONP, Local", function() {
335 function plus(){ if ( ++count == 7 ) start(); }
340 url: "data/jsonp.php",
342 success: function(data){
343 ok( data.data, "JSON results returned (GET, no callback)" );
349 url: "data/jsonp.php?callback=?",
351 success: function(data){
352 ok( data.data, "JSON results returned (GET, url callback)" );
358 url: "data/jsonp.php",
361 success: function(data){
362 ok( data.data, "JSON results returned (GET, data callback)" );
368 url: "data/jsonp.php",
370 data: { callback: "?" },
371 success: function(data){
372 ok( data.data, "JSON results returned (GET, data obj callback)" );
379 url: "data/jsonp.php",
381 success: function(data){
382 ok( data.data, "JSON results returned (POST, no callback)" );
389 url: "data/jsonp.php",
392 success: function(data){
393 ok( data.data, "JSON results returned (POST, data callback)" );
400 url: "data/jsonp.php",
401 data: { callback: "?" },
403 success: function(data){
404 ok( data.data, "JSON results returned (POST, data obj callback)" );
410 test("$.ajax() - JSONP, Remote", function() {
414 function plus(){ if ( ++count == 4 ) start(); }
416 var base = window.location.href.replace(/\?.*$/, "");
421 url: base + "data/jsonp.php",
423 success: function(data){
424 ok( data.data, "JSON results returned (GET, no callback)" );
430 url: base + "data/jsonp.php?callback=?",
432 success: function(data){
433 ok( data.data, "JSON results returned (GET, url callback)" );
439 url: base + "data/jsonp.php",
442 success: function(data){
443 ok( data.data, "JSON results returned (GET, data callback)" );
449 url: base + "data/jsonp.php",
451 data: { callback: "?" },
452 success: function(data){
453 ok( data.data, "JSON results returned (GET, data obj callback)" );
459 test("$.ajax() - script, Remote", function() {
462 var base = window.location.href.replace(/\?.*$/, "");
467 url: base + "data/test.js",
469 success: function(data){
470 ok( foobar, "Script results returned (GET, no callback)" );
476 test("$.getJSON(String, Hash, Function) - JSON array", function() {
479 $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
480 ok( json[0].name == 'John', 'Check JSON: first, name' );
481 ok( json[0].age == 21, 'Check JSON: first, age' );
482 ok( json[1].name == 'Peter', 'Check JSON: second, name' );
483 ok( json[1].age == 25, 'Check JSON: second, age' );
488 test("$.getJSON(String, Function) - JSON object", function() {
491 $.getJSON(url("data/json.php"), function(json) {
492 ok( json.data.lang == 'en', 'Check JSON: lang' );
493 ok( json.data.length == 25, 'Check JSON: length' );
498 test("$.post(String, Hash, Function) - simple with xml", function() {
501 $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
502 $('math', xml).each(function() {
503 ok( $('calculation', this).text() == '5-2', 'Check for XML' );
504 ok( $('result', this).text() == '3', 'Check for XML' );
510 test("$.ajaxSetup({timeout: Number}) - with global timeout", function() {
515 $.ajaxSetup({timeout: 1000});
517 var pass = function() {
520 ok( true, 'Check local and global callbacks after timeout' );
521 $('#main').unbind("ajaxError");
526 var fail = function(a,b,c) {
527 ok( false, 'Check for timeout failed ' + a + ' ' + b );
531 $('#main').ajaxError(pass);
535 url: url("data/name.php?wait=5"),
541 $.ajaxSetup({timeout: 0});
544 test("$.ajaxSetup({timeout: Number}) with localtimeout", function() {
546 $.ajaxSetup({timeout: 50});
551 url: url("data/name.php?wait=1"),
553 ok( false, 'Check for local timeout failed' );
556 success: function() {
557 ok( true, 'Check for local timeout' );
563 $.ajaxSetup({timeout: 0});
566 test("$.ajax - simple get", function() {
571 url: url("data/name.php?name=foo"),
572 success: function(msg){
573 ok( msg == 'bar', 'Check for GET' );
579 test("$.ajax - simple post", function() {
584 url: url("data/name.php"),
586 success: function(msg){
587 ok( msg == 'pan', 'Check for POST' );
593 test("ajaxSetup()", function() {
597 url: url("data/name.php?name=foo"),
598 success: function(msg){
599 ok( msg == 'bar', 'Check for GET' );
606 test("custom timeout does not set error message when timeout occurs, see #970", function() {
609 url: "data/name.php?wait=10",
611 error: function(request, status) {
612 ok( status != null, "status shouldn't be null in error handler" );
613 equals( "timeout", status );