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 test("jQuery.ajax - dataType html", function() {
164 window.foobar = null;
165 window.testFoo = undefined;
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' );
171 // Cleanup the global namespace
172 delete window.foobar;
173 delete window.testFoo;
180 url: url("data/test.html"),
181 success: function(data) {
182 jQuery("#ap").html(data);
183 ok( data.match(/^html text/), 'Check content for datatype html' );
184 setTimeout(verifyEvaluation, 600);
189 test("serialize()", function() {
192 equals( jQuery('#form').serialize(),
193 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2",
194 'Check form serialization as query string');
196 equals( jQuery('#form :input').serialize(),
197 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2",
198 'Check input serialization as query string');
200 equals( jQuery('#testForm').serialize(),
201 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
202 'Check form serialization as query string');
204 equals( jQuery('#testForm :input').serialize(),
205 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
206 'Check input serialization as query string');
208 equals( jQuery('#form, #testForm').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 'Multiple form serialization as query string');
212 equals( jQuery('#form, #testForm :input').serialize(),
213 "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=",
214 'Mixed form/input serialization as query string');
217 test("jQuery.param()", function() {
219 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
220 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
222 params = {someName: [1, 2, 3], regularThing: "blah" };
223 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
225 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
226 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
228 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
229 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" );
232 test("synchronous request", function() {
234 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
237 test("synchronous request with callbacks", function() {
240 jQuery.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
241 ok( /^{ "data"/.test( result ), "check returned text" );
244 test("pass-through request object", function() {
248 var target = "data/name.html";
249 var successCount = 0;
252 var success = function() {
255 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
257 errorEx += ": " + xml.status;
259 jQuery("#foo").one('ajaxStop', function () {
260 equals(successCount, 5, "Check all ajax calls successful");
261 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
262 jQuery("#foo").unbind('ajaxError');
264 delete window.foobar;
268 ok( jQuery.get(url(target), success), "get" );
269 ok( jQuery.post(url(target), success), "post" );
270 ok( jQuery.getScript(url("data/test.js"), success), "script" );
271 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
272 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
275 test("ajax cache", function () {
281 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
282 var re = /_=(.*?)(&|$)/g;
284 for (var i = 0; i < 6; i++) {
285 var ret = re.exec(s.url);
291 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
292 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
297 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
298 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
299 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
300 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
301 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
302 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
305 test("global ajaxSettings", function() {
308 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
309 var orig = { url: "data/with_fries.xml" };
312 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
314 t = jQuery.extend({}, orig);
317 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
319 t = jQuery.extend({}, orig);
320 t.data = { zoo: 'a', ping: 'b' };
322 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' }" );
324 jQuery.ajaxSettings = tmp;
327 test("load(String)", function() {
329 stop(); // check if load can be called with only url
330 jQuery('#first').load("data/name.html", start);
333 test("load('url selector')", function() {
335 stop(); // check if load can be called with only url
336 jQuery('#first').load("data/test3.html div.user", function(){
337 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
342 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
345 jQuery.ajaxSetup({ dataType: "json" });
346 jQuery("#first").ajaxComplete(function (e, xml, s) {
347 equals( s.dataType, "html", "Verify the load() dataType was html" );
348 jQuery("#first").unbind("ajaxComplete");
349 jQuery.ajaxSetup({ dataType: "" });
352 jQuery('#first').load("data/test3.html");
355 test("load(String, Function) - simple: inject text into DOM", function() {
358 jQuery('#first').load(url("data/name.html"), function() {
359 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
364 test("load(String, Function) - check scripts", function() {
368 window.testFoo = undefined;
369 window.foobar = null;
371 var verifyEvaluation = function() {
372 equals( foobar, "bar", 'Check if script src was evaluated after load' );
373 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
375 // Cleanup the global namespace
376 delete window.foobar;
377 delete window.testFoo;
381 jQuery('#first').load(url('data/test.html'), function() {
382 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
383 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
384 equals( testFoo, "foo", 'Check if script was evaluated after load' );
385 setTimeout(verifyEvaluation, 600);
389 test("load(String, Function) - check file with only a script tag", function() {
392 window.testFoo = undefined;
393 jQuery('#first').load(url('data/test2.html'), function() {
394 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
395 equals( testFoo, "foo", 'Check if script was evaluated after load' );
397 // Cleanup the global namespace
398 delete window.testFoo;
403 test("load(String, Object, Function)", function() {
407 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
408 var $post = jQuery(this).find('#post');
409 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
410 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
415 test("load(String, String, Function)", function() {
419 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
420 var $get = jQuery(this).find('#get');
421 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
422 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
427 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
430 jQuery.get(url('data/dashboard.xml'), function(xml) {
432 jQuery('tab', xml).each(function() {
433 content.push(jQuery(this).text());
435 equals( content[0], 'blabla', 'Check first tab');
436 equals( content[1], 'blublu', 'Check second tab');
441 test("jQuery.getScript(String, Function) - with callback", function() {
444 window.foobar = null;
445 jQuery.getScript(url("data/test.js"), function() {
446 equals( foobar, "bar", 'Check if script was evaluated' );
447 delete window.foobar;
448 setTimeout(start, 100);
452 test("jQuery.getScript(String, Function) - no callback", function() {
455 jQuery.getScript(url("data/test.js"), function(){
456 delete window.foobar;
461 test("jQuery.ajax() - JSONP, Local", function() {
465 function plus(){ if ( ++count == 7 ) start(); }
470 url: "data/jsonp.php",
472 success: function(data){
473 ok( data.data, "JSON results returned (GET, no callback)" );
476 error: function(data){
477 ok( false, "Ajax error JSON (GET, no callback)" );
483 url: "data/jsonp.php?callback=?",
485 success: function(data){
486 ok( data.data, "JSON results returned (GET, url callback)" );
489 error: function(data){
490 ok( false, "Ajax error JSON (GET, url callback)" );
496 url: "data/jsonp.php",
499 success: function(data){
500 ok( data.data, "JSON results returned (GET, data callback)" );
503 error: function(data){
504 ok( false, "Ajax error JSON (GET, data callback)" );
510 url: "data/jsonp.php",
513 success: function(data){
514 ok( data.data, "JSON results returned (GET, data obj callback)" );
517 error: function(data){
518 ok( false, "Ajax error JSON (GET, data obj callback)" );
525 url: "data/jsonp.php",
527 success: function(data){
528 ok( data.data, "JSON results returned (POST, no callback)" );
531 error: function(data){
532 ok( false, "Ajax error JSON (GET, data obj callback)" );
539 url: "data/jsonp.php",
542 success: function(data){
543 ok( data.data, "JSON results returned (POST, data callback)" );
546 error: function(data){
547 ok( false, "Ajax error JSON (POST, data callback)" );
554 url: "data/jsonp.php",
557 success: function(data){
558 ok( data.data, "JSON results returned (POST, data obj callback)" );
561 error: function(data){
562 ok( false, "Ajax error JSON (POST, data obj callback)" );
568 test("jQuery.ajax() - JSONP, Remote", function() {
572 function plus(){ if ( ++count == 4 ) start(); }
574 var base = window.location.href.replace(/\?.*$/, "");
579 url: base + "data/jsonp.php",
581 success: function(data){
582 ok( data.data, "JSON results returned (GET, no callback)" );
585 error: function(data){
586 ok( false, "Ajax error JSON (GET, no callback)" );
592 url: base + "data/jsonp.php?callback=?",
594 success: function(data){
595 ok( data.data, "JSON results returned (GET, url callback)" );
598 error: function(data){
599 ok( false, "Ajax error JSON (GET, url callback)" );
605 url: base + "data/jsonp.php",
608 success: function(data){
609 ok( data.data, "JSON results returned (GET, data callback)" );
612 error: function(data){
613 ok( false, "Ajax error JSON (GET, data callback)" );
619 url: base + "data/jsonp.php",
622 success: function(data){
623 ok( data.data, "JSON results returned (GET, data obj callback)" );
626 error: function(data){
627 ok( false, "Ajax error JSON (GET, data obj callback)" );
633 test("jQuery.ajax() - script, Remote", function() {
636 var base = window.location.href.replace(/\?.*$/, "");
640 window.foobar = null;
642 url: base + "data/test.js",
644 success: function(data){
645 ok( foobar, "Script results returned (GET, no callback)" );
646 delete window.foobar;
652 test("jQuery.ajax() - script, Remote with POST", function() {
655 var base = window.location.href.replace(/\?.*$/, "");
659 window.foobar = null;
661 url: base + "data/test.js",
664 success: function(data, status){
665 ok( foobar, "Script results returned (GET, no callback)" );
666 equals( status, "success", "Script results returned (GET, no callback)" );
667 delete window.foobar;
673 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
676 var base = window.location.href.replace(/\?.*$/, "");
677 base = base.replace(/^.*?\/\//, "//");
681 window.foobar = null;
683 url: base + "data/test.js",
685 success: function(data){
686 ok( foobar, "Script results returned (GET, no callback)" );
687 delete window.foobar;
693 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
696 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
697 equals( json[0].name, 'John', 'Check JSON: first, name' );
698 equals( json[0].age, 21, 'Check JSON: first, age' );
699 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
700 equals( json[1].age, 25, 'Check JSON: second, age' );
705 test("jQuery.getJSON(String, Function) - JSON object", function() {
708 jQuery.getJSON(url("data/json.php"), function(json) {
709 equals( json.data.lang, 'en', 'Check JSON: lang' );
710 equals( json.data.length, 25, 'Check JSON: length' );
715 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
718 var base = window.location.href.replace(/\?.*$/, "");
721 jQuery.getJSON(url(base + "data/json.php"), function(json) {
722 equals( json.data.lang, 'en', 'Check JSON: lang' );
723 equals( json.data.length, 25, 'Check JSON: length' );
728 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
731 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
732 jQuery('math', xml).each(function() {
733 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
734 equals( jQuery('result', this).text(), '3', 'Check for XML' );
738 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
739 jQuery('math', xml).each(function() {
740 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
741 equals( jQuery('result', this).text(), '3', 'Check for XML' );
747 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
752 jQuery.ajaxSetup({timeout: 1000});
754 var pass = function() {
757 ok( true, 'Check local and global callbacks after timeout' );
758 jQuery('#main').unbind("ajaxError");
763 var fail = function(a,b,c) {
764 ok( false, 'Check for timeout failed ' + a + ' ' + b );
768 jQuery('#main').ajaxError(pass);
772 url: url("data/name.php?wait=5"),
778 jQuery.ajaxSetup({timeout: 0});
781 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
783 jQuery.ajaxSetup({timeout: 50});
788 url: url("data/name.php?wait=1"),
790 ok( false, 'Check for local timeout failed' );
793 success: function() {
794 ok( true, 'Check for local timeout' );
800 jQuery.ajaxSetup({timeout: 0});
803 test("jQuery.ajax - simple get", function() {
808 url: url("data/name.php?name=foo"),
809 success: function(msg){
810 equals( msg, 'bar', 'Check for GET' );
816 test("jQuery.ajax - simple post", function() {
821 url: url("data/name.php"),
823 success: function(msg){
824 equals( msg, 'pan', 'Check for POST' );
830 test("ajaxSetup()", function() {
834 url: url("data/name.php?name=foo"),
835 success: function(msg){
836 equals( msg, 'bar', 'Check for GET' );
844 test("custom timeout does not set error message when timeout occurs, see #970", function() {
847 url: "data/name.php?wait=1",
849 error: function(request, status) {
850 ok( status != null, "status shouldn't be null in error handler" );
851 equals( "timeout", status );
858 test("data option: evaluate function values (#2806)", function() {
861 url: "data/echoQuery.php",
867 success: function(result) {
868 equals( result, "key=value" );