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( $.getIfModified(url(target), success), "getIfModified" );
215 ok( $.post(url(target), success), "post" );
216 ok( $.getScript(url("data/test.js"), success), "script" );
217 ok( $.getJSON(url("data/json_obj.js"), success), "json" );
219 ok( $.ajax({url: url(target), success: success}), "generic" );
222 test("global ajaxSettings", function() {
225 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
226 var orig = { url: "data/with_fries.xml", data: null };
229 $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
231 t = jQuery.extend({}, orig);
233 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending null" );
235 t = jQuery.extend({}, orig);
238 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
240 t = jQuery.extend({}, orig);
241 t.data = { zoo: 'a', ping: 'b' };
243 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' }" );
245 jQuery.ajaxSettings = tmp;
248 test("load(String)", function() {
250 stop(true); // check if load can be called with only url
251 $('#first').load("data/name.html", start);
254 test("load('url selector')", function() {
256 stop(true); // check if load can be called with only url
257 $('#first').load("data/test3.html div.user", function(){
258 equals( $(this).children("div").length, 2, "Verify that specific elements were injected" );
263 test("load(String, Function) - simple: inject text into DOM", function() {
266 $('#first').load(url("data/name.html"), function() {
267 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
272 test("load(String, Function) - check scripts", function() {
275 window.testFoo = undefined;
276 window.foobar = null;
277 var verifyEvaluation = function() {
278 equals( foobar, "bar", 'Check if script src was evaluated after load' );
279 equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
282 $('#first').load(url('data/test.html'), function() {
283 ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
284 equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
285 equals( testFoo, "foo", 'Check if script was evaluated after load' );
286 setTimeout(verifyEvaluation, 600);
290 test("load(String, Function) - check file with only a script tag", function() {
294 $('#first').load(url('data/test2.html'), function() {
295 ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
296 ok( testFoo == "foo", 'Check if script was evaluated after load' );
301 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
304 $.get(url('data/dashboard.xml'), function(xml) {
306 $('tab', xml).each(function() {
307 content.push($(this).text());
309 equals( content[0], 'blabla', 'Check first tab');
310 equals( content[1], 'blublu', 'Check second tab');
315 test("$.getIfModified(String, Hash, Function)", function() {
318 $.getIfModified(url("data/name.html"), function(msg) {
319 ok( /^ERROR/.test(msg), 'Check ifModified' );
324 test("$.getScript(String, Function) - with callback", function() {
327 $.getScript(url("data/test.js"), function() {
328 equals( foobar, "bar", 'Check if script was evaluated' );
329 setTimeout(start, 100);
333 test("$.getScript(String, Function) - no callback", function() {
336 $.getScript(url("data/test.js"), start);
341 test("$.ajax() - JSONP, Local", function() {
345 function plus(){ if ( ++count == 7 ) start(); }
350 url: "data/jsonp.php",
352 success: function(data){
353 ok( data.data, "JSON results returned (GET, no callback)" );
359 url: "data/jsonp.php?callback=?",
361 success: function(data){
362 ok( data.data, "JSON results returned (GET, url callback)" );
368 url: "data/jsonp.php",
371 success: function(data){
372 ok( data.data, "JSON results returned (GET, data callback)" );
378 url: "data/jsonp.php",
380 data: { callback: "?" },
381 success: function(data){
382 ok( data.data, "JSON results returned (GET, data obj callback)" );
389 url: "data/jsonp.php",
391 success: function(data){
392 ok( data.data, "JSON results returned (POST, no callback)" );
399 url: "data/jsonp.php",
402 success: function(data){
403 ok( data.data, "JSON results returned (POST, data callback)" );
410 url: "data/jsonp.php",
411 data: { callback: "?" },
413 success: function(data){
414 ok( data.data, "JSON results returned (POST, data obj callback)" );
420 test("$.ajax() - JSONP, Remote", function() {
424 function plus(){ if ( ++count == 4 ) start(); }
426 var base = window.location.href.replace(/\?.*$/, "");
431 url: base + "data/jsonp.php",
433 success: function(data){
434 ok( data.data, "JSON results returned (GET, no callback)" );
440 url: base + "data/jsonp.php?callback=?",
442 success: function(data){
443 ok( data.data, "JSON results returned (GET, url callback)" );
449 url: base + "data/jsonp.php",
452 success: function(data){
453 ok( data.data, "JSON results returned (GET, data callback)" );
459 url: base + "data/jsonp.php",
461 data: { callback: "?" },
462 success: function(data){
463 ok( data.data, "JSON results returned (GET, data obj callback)" );
469 test("$.ajax() - script, Remote", function() {
472 var base = window.location.href.replace(/\?.*$/, "");
477 url: base + "data/test.js",
479 success: function(data){
480 ok( foobar, "Script results returned (GET, no callback)" );
486 test("$.getJSON(String, Hash, Function) - JSON array", function() {
489 $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
490 ok( json[0].name == 'John', 'Check JSON: first, name' );
491 ok( json[0].age == 21, 'Check JSON: first, age' );
492 ok( json[1].name == 'Peter', 'Check JSON: second, name' );
493 ok( json[1].age == 25, 'Check JSON: second, age' );
498 test("$.getJSON(String, Function) - JSON object", function() {
501 $.getJSON(url("data/json.php"), function(json) {
502 ok( json.data.lang == 'en', 'Check JSON: lang' );
503 ok( json.data.length == 25, 'Check JSON: length' );
508 test("$.post(String, Hash, Function) - simple with xml", function() {
511 $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
512 $('math', xml).each(function() {
513 ok( $('calculation', this).text() == '5-2', 'Check for XML' );
514 ok( $('result', this).text() == '3', 'Check for XML' );
520 test("$.ajaxTimeout(Number) - with global timeout", function() {
527 var pass = function() {
530 ok( true, 'Check local and global callbacks after timeout' );
531 $('#main').unbind("ajaxError");
536 var fail = function(a,b,c) {
537 ok( false, 'Check for timeout failed ' + a + ' ' + b );
541 $('#main').ajaxError(pass);
545 url: url("data/name.php?wait=5"),
554 test("$.ajaxTimeout(Number) with localtimeout", function() {
555 stop(); $.ajaxTimeout(50);
559 url: url("data/name.php?wait=1"),
561 ok( false, 'Check for local timeout failed' );
564 success: function() {
565 ok( true, 'Check for local timeout' );
573 test("$.ajax - simple get", function() {
578 url: url("data/name.php?name=foo"),
579 success: function(msg){
580 ok( msg == 'bar', 'Check for GET' );
586 test("$.ajax - simple post", function() {
591 url: url("data/name.php"),
593 success: function(msg){
594 ok( msg == 'pan', 'Check for POST' );
600 test("ajaxSetup()", function() {
604 url: url("data/name.php?name=foo"),
605 success: function(msg){
606 ok( msg == 'bar', 'Check for GET' );
613 test("custom timeout does not set error message when timeout occurs, see #970", function() {
616 url: "data/name.php?wait=10",
618 error: function(request, status) {
619 ok( status != null, "status shouldn't be null in error handler" );
620 equals( "timeout", status );