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("jQuery.ajax() - success callbacks", function() {
13 jQuery.ajaxSetup({ timeout: 0 });
17 setTimeout(function(){
18 jQuery('#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("jQuery.ajax() - error callbacks", function() {
47 jQuery('#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 jQuery.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("jQuery.ajax() - disabled globals", function() {
77 jQuery('#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("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
108 url: url("data/with_fries.xml"),
110 success: function(resp) {
111 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
112 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
113 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
119 test("jQuery.ajax - beforeSend", function() {
125 jQuery.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("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
141 var request = jQuery.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" );
160 window.foobar = null;
161 window.testFoo = undefined;
163 test("jQuery.ajax - dataType html", function() {
167 var verifyEvaluation = function() {
168 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
169 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
176 url: url("data/test.html"),
177 success: function(data) {
178 jQuery("#ap").html(data);
179 ok( data.match(/^html text/), 'Check content for datatype html' );
180 setTimeout(verifyEvaluation, 600);
185 test("serialize()", function() {
188 equals( jQuery('#form').serialize(),
189 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2",
190 'Check form serialization as query string');
192 equals( jQuery('#form :input').serialize(),
193 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2",
194 'Check input serialization as query string');
196 equals( jQuery('#testForm').serialize(),
197 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
198 'Check form serialization as query string');
200 equals( jQuery('#testForm :input').serialize(),
201 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
202 'Check input serialization as query string');
204 equals( jQuery('#form, #testForm').serialize(),
205 "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=",
206 'Multiple form serialization as query string');
208 equals( jQuery('#form, #testForm :input').serialize(),
209 "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=",
210 'Mixed form/input serialization as query string');
213 test("jQuery.param()", function() {
215 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
216 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
218 params = {someName: [1, 2, 3], regularThing: "blah" };
219 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
221 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
222 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
224 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
225 equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" );
228 test("synchronous request", function() {
230 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
233 test("synchronous request with callbacks", function() {
236 jQuery.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
237 ok( /^{ "data"/.test( result ), "check returned text" );
240 test("pass-through request object", function() {
244 var target = "data/name.html";
245 var successCount = 0;
248 var success = function() {
251 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
253 errorEx += ": " + xml.status;
255 jQuery("#foo").one('ajaxStop', function () {
256 equals(successCount, 5, "Check all ajax calls successful");
257 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
258 jQuery("#foo").unbind('ajaxError');
263 ok( jQuery.get(url(target), success), "get" );
264 ok( jQuery.post(url(target), success), "post" );
265 ok( jQuery.getScript(url("data/test.js"), success), "script" );
266 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
267 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
270 test("ajax cache", function () {
276 jQuery("#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( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
293 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
294 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
295 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
296 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
297 ok( jQuery.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 jQuery.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(); // check if load can be called with only url
325 jQuery('#first').load("data/name.html", start);
328 test("load('url selector')", function() {
330 stop(); // check if load can be called with only url
331 jQuery('#first').load("data/test3.html div.user", function(){
332 equals( jQuery(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 jQuery.ajaxSetup({ dataType: "json" });
341 jQuery("#first").ajaxComplete(function (e, xml, s) {
342 equals( s.dataType, "html", "Verify the load() dataType was html" );
343 jQuery("#first").unbind("ajaxComplete");
344 jQuery.ajaxSetup({ dataType: "" });
347 jQuery('#first').load("data/test3.html");
350 test("load(String, Function) - simple: inject text into DOM", function() {
353 jQuery('#first').load(url("data/name.html"), function() {
354 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
359 test("load(String, Function) - check scripts", function() {
363 var verifyEvaluation = function() {
364 equals( foobar, "bar", 'Check if script src was evaluated after load' );
365 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
369 jQuery('#first').load(url('data/test.html'), function() {
370 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
371 equals( jQuery('#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 jQuery('#first').load(url('data/test2.html'), function() {
382 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
383 equals( testFoo, "foo", 'Check if script was evaluated after load' );
389 test("load(String, Object, Function)", function() {
393 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
394 var $post = jQuery(this).find('#post');
395 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
396 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
401 test("load(String, String, Function)", function() {
405 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
406 var $get = jQuery(this).find('#get');
407 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
408 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
413 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
416 jQuery.get(url('data/dashboard.xml'), function(xml) {
418 jQuery('tab', xml).each(function() {
419 content.push(jQuery(this).text());
421 equals( content[0], 'blabla', 'Check first tab');
422 equals( content[1], 'blublu', 'Check second tab');
427 test("jQuery.getScript(String, Function) - with callback", function() {
430 jQuery.getScript(url("data/test.js"), function() {
431 equals( foobar, "bar", 'Check if script was evaluated' );
432 setTimeout(start, 100);
436 test("jQuery.getScript(String, Function) - no callback", function() {
439 jQuery.getScript(url("data/test.js"), function(){
444 test("jQuery.ajax() - JSONP, Local", function() {
448 function plus(){ if ( ++count == 7 ) start(); }
453 url: "data/jsonp.php",
455 success: function(data){
456 ok( data.data, "JSON results returned (GET, no callback)" );
459 error: function(data){
460 ok( false, "Ajax error JSON (GET, no callback)" );
466 url: "data/jsonp.php?callback=?",
468 success: function(data){
469 ok( data.data, "JSON results returned (GET, url callback)" );
472 error: function(data){
473 ok( false, "Ajax error JSON (GET, url callback)" );
479 url: "data/jsonp.php",
482 success: function(data){
483 ok( data.data, "JSON results returned (GET, data callback)" );
486 error: function(data){
487 ok( false, "Ajax error JSON (GET, data callback)" );
493 url: "data/jsonp.php",
496 success: function(data){
497 ok( data.data, "JSON results returned (GET, data obj callback)" );
500 error: function(data){
501 ok( false, "Ajax error JSON (GET, data obj callback)" );
508 url: "data/jsonp.php",
510 success: function(data){
511 ok( data.data, "JSON results returned (POST, no callback)" );
514 error: function(data){
515 ok( false, "Ajax error JSON (GET, data obj callback)" );
522 url: "data/jsonp.php",
525 success: function(data){
526 ok( data.data, "JSON results returned (POST, data callback)" );
529 error: function(data){
530 ok( false, "Ajax error JSON (POST, data callback)" );
537 url: "data/jsonp.php",
540 success: function(data){
541 ok( data.data, "JSON results returned (POST, data obj callback)" );
544 error: function(data){
545 ok( false, "Ajax error JSON (POST, data obj callback)" );
551 test("jQuery.ajax() - JSONP, Remote", function() {
555 function plus(){ if ( ++count == 4 ) start(); }
557 var base = window.location.href.replace(/\?.*$/, "");
562 url: base + "data/jsonp.php",
564 success: function(data){
565 ok( data.data, "JSON results returned (GET, no callback)" );
568 error: function(data){
569 ok( false, "Ajax error JSON (GET, no callback)" );
575 url: base + "data/jsonp.php?callback=?",
577 success: function(data){
578 ok( data.data, "JSON results returned (GET, url callback)" );
581 error: function(data){
582 ok( false, "Ajax error JSON (GET, url callback)" );
588 url: base + "data/jsonp.php",
591 success: function(data){
592 ok( data.data, "JSON results returned (GET, data callback)" );
595 error: function(data){
596 ok( false, "Ajax error JSON (GET, data callback)" );
602 url: base + "data/jsonp.php",
605 success: function(data){
606 ok( data.data, "JSON results returned (GET, data obj callback)" );
609 error: function(data){
610 ok( false, "Ajax error JSON (GET, data obj callback)" );
616 test("jQuery.ajax() - script, Remote", function() {
619 var base = window.location.href.replace(/\?.*$/, "");
624 url: base + "data/test.js",
626 success: function(data){
627 ok( foobar, "Script results returned (GET, no callback)" );
633 test("jQuery.ajax() - script, Remote with POST", function() {
636 var base = window.location.href.replace(/\?.*$/, "");
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("jQuery.ajax() - script, Remote with scheme-less URL", function() {
655 var base = window.location.href.replace(/\?.*$/, "");
656 base = base.replace(/^.*?\/\//, "//");
661 url: base + "data/test.js",
663 success: function(data){
664 ok( foobar, "Script results returned (GET, no callback)" );
670 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
673 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
674 equals( json[0].name, 'John', 'Check JSON: first, name' );
675 equals( json[0].age, 21, 'Check JSON: first, age' );
676 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
677 equals( json[1].age, 25, 'Check JSON: second, age' );
682 test("jQuery.getJSON(String, Function) - JSON object", function() {
685 jQuery.getJSON(url("data/json.php"), function(json) {
686 equals( json.data.lang, 'en', 'Check JSON: lang' );
687 equals( json.data.length, 25, 'Check JSON: length' );
692 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
695 var base = window.location.href.replace(/\?.*$/, "");
698 jQuery.getJSON(url(base + "data/json.php"), function(json) {
699 equals( json.data.lang, 'en', 'Check JSON: lang' );
700 equals( json.data.length, 25, 'Check JSON: length' );
705 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
710 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
711 jQuery('math', xml).each(function() {
712 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
713 equals( jQuery('result', this).text(), '3', 'Check for XML' );
715 if ( ++done === 2 ) start();
718 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
719 jQuery('math', xml).each(function() {
720 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
721 equals( jQuery('result', this).text(), '3', 'Check for XML' );
723 if ( ++done === 2 ) start();
727 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
732 jQuery.ajaxSetup({timeout: 1000});
734 var pass = function() {
737 ok( true, 'Check local and global callbacks after timeout' );
738 jQuery('#main').unbind("ajaxError");
743 var fail = function(a,b,c) {
744 ok( false, 'Check for timeout failed ' + a + ' ' + b );
748 jQuery('#main').ajaxError(pass);
752 url: url("data/name.php?wait=5"),
758 jQuery.ajaxSetup({timeout: 0});
761 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
763 jQuery.ajaxSetup({timeout: 50});
768 url: url("data/name.php?wait=1"),
770 ok( false, 'Check for local timeout failed' );
773 success: function() {
774 ok( true, 'Check for local timeout' );
780 jQuery.ajaxSetup({timeout: 0});
783 test("jQuery.ajax - simple get", function() {
788 url: url("data/name.php?name=foo"),
789 success: function(msg){
790 equals( msg, 'bar', 'Check for GET' );
796 test("jQuery.ajax - simple post", function() {
801 url: url("data/name.php"),
803 success: function(msg){
804 equals( msg, 'pan', 'Check for POST' );
810 test("ajaxSetup()", function() {
814 url: url("data/name.php?name=foo"),
815 success: function(msg){
816 equals( msg, 'bar', 'Check for GET' );
824 test("custom timeout does not set error message when timeout occurs, see #970", function() {
827 url: "data/name.php?wait=1",
829 error: function(request, status) {
830 ok( status != null, "status shouldn't be null in error handler" );
831 equals( "timeout", status );
838 test("data option: evaluate function values (#2806)", function() {
841 url: "data/echoQuery.php",
847 success: function(result) {
848 equals( result, "key=value" );