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 ) {
10 test("$.ajax() - success callbacks", function() {
13 $.ajaxSetup({ timeout: 0 });
17 setTimeout(function(){
18 $('#foo').ajaxStart(function(){
19 ok( true, "ajaxStart" );
20 }).ajaxStop(function(){
21 ok( true, "ajaxStop" );
23 }).ajaxSend(function(){
24 ok( true, "ajaxSend" );
25 }).ajaxComplete(function(){
26 ok( true, "ajaxComplete" );
27 }).ajaxError(function(){
28 ok( false, "ajaxError" );
29 }).ajaxSuccess(function(){
30 ok( true, "ajaxSuccess" );
34 url: url("data/name.html"),
35 beforeSend: function(){ ok(true, "beforeSend"); },
36 success: function(){ ok(true, "success"); },
37 error: function(){ ok(false, "error"); },
38 complete: function(){ ok(true, "complete"); }
43 test("$.ajax() - error callbacks", function() {
47 $('#foo').ajaxStart(function(){
48 ok( true, "ajaxStart" );
49 }).ajaxStop(function(){
50 ok( true, "ajaxStop" );
52 }).ajaxSend(function(){
53 ok( true, "ajaxSend" );
54 }).ajaxComplete(function(){
55 ok( true, "ajaxComplete" );
56 }).ajaxError(function(){
57 ok( true, "ajaxError" );
58 }).ajaxSuccess(function(){
59 ok( false, "ajaxSuccess" );
62 $.ajaxSetup({ timeout: 500 });
65 url: url("data/name.php?wait=5"),
66 beforeSend: function(){ ok(true, "beforeSend"); },
67 success: function(){ ok(false, "success"); },
68 error: function(){ ok(true, "error"); },
69 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" );
139 test("$.ajax - beforeSend, cancel request (#2688)", function() {
141 var request = $.ajax({
142 url: url("data/name.html"),
143 beforeSend: function() {
144 ok( true, "beforeSend got called, canceling" );
147 success: function() {
148 ok( false, "request didn't get canceled" );
150 complete: function() {
151 ok( false, "request didn't get canceled" );
154 ok( false, "request didn't get canceled" );
157 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
162 test("$.ajax - dataType html", function() {
169 var verifyEvaluation = function() {
170 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
171 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
177 url: url("data/test.html"),
178 success: function(data) {
180 ok( data.match(/^html text/), 'Check content for datatype html' );
181 setTimeout(verifyEvaluation, 600);
186 test("serialize()", function() {
189 equals( $('#form').serialize(),
190 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2",
191 'Check form serialization as query string');
193 equals( $('#form :input').serialize(),
194 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2",
195 'Check input serialization as query string');
197 equals( $('#testForm').serialize(),
198 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
199 'Check form serialization as query string');
201 equals( $('#testForm :input').serialize(),
202 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
203 'Check input serialization as query string');
205 equals( $('#form, #testForm').serialize(),
206 "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=",
207 'Multiple form serialization as query string');
209 equals( $('#form, #testForm :input').serialize(),
210 "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=",
211 'Mixed form/input serialization as query string');
214 test("$.param()", function() {
216 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
217 equals( $.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
219 params = {someName: [1, 2, 3], regularThing: "blah" };
220 equals( $.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
222 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
223 equals( $.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
225 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
226 equals( $.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" );
229 test("synchronous request", function() {
231 ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
234 test("synchronous request with callbacks", function() {
237 $.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
238 ok( /^{ "data"/.test( result ), "check returned text" );
241 test("pass-through request object", function() {
245 var target = "data/name.html";
246 var successCount = 0;
249 var success = function() {
252 $("#foo").ajaxError(function (e, xml, s, ex) {
254 errorEx += ": " + xml.status;
256 $("#foo").one('ajaxStop', function () {
257 equals(successCount, 5, "Check all ajax calls successful");
258 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
259 $("#foo").unbind('ajaxError');
263 ok( $.get(url(target), success), "get" );
264 ok( $.post(url(target), success), "post" );
265 ok( $.getScript(url("data/test.js"), success), "script" );
266 ok( $.getJSON(url("data/json_obj.js"), success), "json" );
267 ok( $.ajax({url: url(target), success: success}), "generic" );
270 test("ajax cache", function () {
276 $("#firstp").bind("ajaxSuccess", function (e, xml, s) {
277 var re = /_=(.*?)(&|$)/g;
279 for (var i = 0; i < 6; i++) {
280 var ret = re.exec(s.url);
286 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
287 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
292 ok( $.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
293 ok( $.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
294 ok( $.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
295 ok( $.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
296 ok( $.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
297 ok( $.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
300 test("global ajaxSettings", function() {
303 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
304 var orig = { url: "data/with_fries.xml" };
307 $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
309 t = jQuery.extend({}, orig);
312 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
314 t = jQuery.extend({}, orig);
315 t.data = { zoo: 'a', ping: 'b' };
317 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' }" );
319 jQuery.ajaxSettings = tmp;
322 test("load(String)", function() {
324 stop(true); // check if load can be called with only url
325 $('#first').load("data/name.html", start);
328 test("load('url selector')", function() {
330 stop(true); // check if load can be called with only url
331 $('#first').load("data/test3.html div.user", function(){
332 equals( $(this).children("div").length, 2, "Verify that specific elements were injected" );
337 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
340 $.ajaxSetup({ dataType: "json" });
341 $("#first").ajaxComplete(function (e, xml, s) {
342 equals( s.dataType, "html", "Verify the load() dataType was html" );
343 $("#first").unbind("ajaxComplete");
344 $.ajaxSetup({ dataType: "" });
347 $('#first').load("data/test3.html");
350 test("load(String, Function) - simple: inject text into DOM", function() {
353 $('#first').load(url("data/name.html"), function() {
354 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
359 test("load(String, Function) - check scripts", function() {
362 window.testFoo = undefined;
363 window.foobar = null;
364 var verifyEvaluation = function() {
365 equals( foobar, "bar", 'Check if script src was evaluated after load' );
366 equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
369 $('#first').load(url('data/test.html'), function() {
370 ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
371 equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
372 equals( testFoo, "foo", 'Check if script was evaluated after load' );
373 setTimeout(verifyEvaluation, 600);
377 test("load(String, Function) - check file with only a script tag", function() {
381 $('#first').load(url('data/test2.html'), function() {
382 equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
383 equals( testFoo, "foo", 'Check if script was evaluated after load' );
388 test("load(String, Object, Function)", function() {
392 $('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
393 var $post = $(this).find('#post');
394 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
395 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
400 test("load(String, String, Function)", function() {
404 $('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
405 var $get = $(this).find('#get');
406 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
407 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
412 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
415 $.get(url('data/dashboard.xml'), function(xml) {
417 $('tab', xml).each(function() {
418 content.push($(this).text());
420 equals( content[0], 'blabla', 'Check first tab');
421 equals( content[1], 'blublu', 'Check second tab');
426 test("$.getScript(String, Function) - with callback", function() {
429 window.foobar = null;
430 $.getScript(url("data/test.js"), function() {
431 equals( foobar, "bar", 'Check if script was evaluated' );
432 setTimeout(start, 100);
436 test("$.getScript(String, Function) - no callback", function() {
439 $.getScript(url("data/test.js"), start);
442 test("$.ajax() - JSONP, Local", function() {
446 function plus(){ if ( ++count == 7 ) start(); }
451 url: "data/jsonp.php",
453 success: function(data){
454 ok( data.data, "JSON results returned (GET, no callback)" );
457 error: function(data){
458 ok( false, "Ajax error JSON (GET, no callback)" );
464 url: "data/jsonp.php?callback=?",
466 success: function(data){
467 ok( data.data, "JSON results returned (GET, url callback)" );
470 error: function(data){
471 ok( false, "Ajax error JSON (GET, url callback)" );
477 url: "data/jsonp.php",
480 success: function(data){
481 ok( data.data, "JSON results returned (GET, data callback)" );
484 error: function(data){
485 ok( false, "Ajax error JSON (GET, data callback)" );
491 url: "data/jsonp.php",
494 success: function(data){
495 ok( data.data, "JSON results returned (GET, data obj callback)" );
498 error: function(data){
499 ok( false, "Ajax error JSON (GET, data obj callback)" );
506 url: "data/jsonp.php",
508 success: function(data){
509 ok( data.data, "JSON results returned (POST, no callback)" );
512 error: function(data){
513 ok( false, "Ajax error JSON (GET, data obj callback)" );
520 url: "data/jsonp.php",
523 success: function(data){
524 ok( data.data, "JSON results returned (POST, data callback)" );
527 error: function(data){
528 ok( false, "Ajax error JSON (POST, data callback)" );
535 url: "data/jsonp.php",
538 success: function(data){
539 ok( data.data, "JSON results returned (POST, data obj callback)" );
542 error: function(data){
543 ok( false, "Ajax error JSON (POST, data obj callback)" );
549 test("$.ajax() - JSONP, Remote", function() {
553 function plus(){ if ( ++count == 4 ) start(); }
555 var base = window.location.href.replace(/\?.*$/, "");
560 url: base + "data/jsonp.php",
562 success: function(data){
563 ok( data.data, "JSON results returned (GET, no callback)" );
566 error: function(data){
567 ok( false, "Ajax error JSON (GET, no callback)" );
573 url: base + "data/jsonp.php?callback=?",
575 success: function(data){
576 ok( data.data, "JSON results returned (GET, url callback)" );
579 error: function(data){
580 ok( false, "Ajax error JSON (GET, url callback)" );
586 url: base + "data/jsonp.php",
589 success: function(data){
590 ok( data.data, "JSON results returned (GET, data callback)" );
593 error: function(data){
594 ok( false, "Ajax error JSON (GET, data callback)" );
600 url: base + "data/jsonp.php",
603 success: function(data){
604 ok( data.data, "JSON results returned (GET, data obj callback)" );
607 error: function(data){
608 ok( false, "Ajax error JSON (GET, data obj callback)" );
614 test("$.ajax() - script, Remote", function() {
617 var base = window.location.href.replace(/\?.*$/, "");
621 window.foobar = null;
623 url: base + "data/test.js",
625 success: function(data){
626 ok( foobar, "Script results returned (GET, no callback)" );
632 test("$.ajax() - script, Remote with POST", function() {
635 var base = window.location.href.replace(/\?.*$/, "");
639 window.foobar = null;
641 url: base + "data/test.js",
644 success: function(data, status){
645 ok( foobar, "Script results returned (GET, no callback)" );
646 equals( status, "success", "Script results returned (GET, no callback)" );
652 test("$.ajax() - script, Remote with scheme-less URL", function() {
655 var base = window.location.href.replace(/\?.*$/, "");
656 base = base.replace(/^.*?\/\//, "//");
660 window.foobar = null;
662 url: base + "data/test.js",
664 success: function(data){
665 ok( foobar, "Script results returned (GET, no callback)" );
671 test("$.getJSON(String, Hash, Function) - JSON array", function() {
674 $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
675 equals( json[0].name, 'John', 'Check JSON: first, name' );
676 equals( json[0].age, 21, 'Check JSON: first, age' );
677 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
678 equals( json[1].age, 25, 'Check JSON: second, age' );
683 test("$.getJSON(String, Function) - JSON object", function() {
686 $.getJSON(url("data/json.php"), function(json) {
687 equals( json.data.lang, 'en', 'Check JSON: lang' );
688 equals( json.data.length, 25, 'Check JSON: length' );
693 test("$.getJSON(String, Function) - JSON object with absolute url to local content", function() {
696 var base = window.location.href.replace(/\?.*$/, "");
699 $.getJSON(url(base + "data/json.php"), function(json) {
700 equals( json.data.lang, 'en', 'Check JSON: lang' );
701 equals( json.data.length, 25, 'Check JSON: length' );
706 test("$.post(String, Hash, Function) - simple with xml", function() {
709 $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
710 $('math', xml).each(function() {
711 equals( $('calculation', this).text(), '5-2', 'Check for XML' );
712 equals( $('result', this).text(), '3', 'Check for XML' );
716 $.post(url("data/name.php?xml=5-2"), {}, function(xml){
717 $('math', xml).each(function() {
718 equals( $('calculation', this).text(), '5-2', 'Check for XML' );
719 equals( $('result', this).text(), '3', 'Check for XML' );
725 test("$.ajaxSetup({timeout: Number}) - with global timeout", function() {
730 $.ajaxSetup({timeout: 1000});
732 var pass = function() {
735 ok( true, 'Check local and global callbacks after timeout' );
736 $('#main').unbind("ajaxError");
741 var fail = function(a,b,c) {
742 ok( false, 'Check for timeout failed ' + a + ' ' + b );
746 $('#main').ajaxError(pass);
750 url: url("data/name.php?wait=5"),
756 $.ajaxSetup({timeout: 0});
759 test("$.ajaxSetup({timeout: Number}) with localtimeout", function() {
761 $.ajaxSetup({timeout: 50});
766 url: url("data/name.php?wait=1"),
768 ok( false, 'Check for local timeout failed' );
771 success: function() {
772 ok( true, 'Check for local timeout' );
778 $.ajaxSetup({timeout: 0});
781 test("$.ajax - simple get", function() {
786 url: url("data/name.php?name=foo"),
787 success: function(msg){
788 equals( msg, 'bar', 'Check for GET' );
794 test("$.ajax - simple post", function() {
799 url: url("data/name.php"),
801 success: function(msg){
802 equals( msg, 'pan', 'Check for POST' );
808 test("ajaxSetup()", function() {
812 url: url("data/name.php?name=foo"),
813 success: function(msg){
814 equals( msg, 'bar', 'Check for GET' );
821 test("custom timeout does not set error message when timeout occurs, see #970", function() {
824 url: "data/name.php?wait=10",
826 error: function(request, status) {
827 ok( status != null, "status shouldn't be null in error handler" );
828 equals( "timeout", status );
834 test("data option: evaluate function values (#2806)", function() {
837 url: "data/echoQuery.php",
843 success: function(result) {
844 equals( result, "key=value" );