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("Ajax events with context", function() {
80 equals( this, context, e.type );
84 equals( this, context, "context is preserved on callback" );
87 jQuery('#foo').add(context)
94 url: url("data/name.html"),
100 setTimeout(proceed, 300);
106 jQuery('#foo').add(context).unbind();
111 test("jQuery.ajax() - disabled globals", function() {
115 jQuery('#foo').ajaxStart(function(){
116 ok( false, "ajaxStart" );
117 }).ajaxStop(function(){
118 ok( false, "ajaxStop" );
119 }).ajaxSend(function(){
120 ok( false, "ajaxSend" );
121 }).ajaxComplete(function(){
122 ok( false, "ajaxComplete" );
123 }).ajaxError(function(){
124 ok( false, "ajaxError" );
125 }).ajaxSuccess(function(){
126 ok( false, "ajaxSuccess" );
131 url: url("data/name.html"),
132 beforeSend: function(){ ok(true, "beforeSend"); },
133 success: function(){ ok(true, "success"); },
134 error: function(){ ok(false, "error"); },
135 complete: function(){
136 ok(true, "complete");
137 setTimeout(function(){ start(); }, 13);
142 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
146 url: url("data/with_fries.xml"),
148 success: function(resp) {
149 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
150 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
151 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
157 test("jQuery.ajax - beforeSend", function() {
163 jQuery.ajaxSetup({ timeout: 0 });
166 url: url("data/name.html"),
167 beforeSend: function(xml) {
170 success: function(data) {
171 ok( check, "check beforeSend was executed" );
177 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
179 var request = jQuery.ajax({
180 url: url("data/name.html"),
181 beforeSend: function() {
182 ok( true, "beforeSend got called, canceling" );
185 success: function() {
186 ok( false, "request didn't get canceled" );
188 complete: function() {
189 ok( false, "request didn't get canceled" );
192 ok( false, "request didn't get canceled" );
195 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
198 window.foobar = null;
199 window.testFoo = undefined;
201 test("jQuery.ajax - dataType html", function() {
205 var verifyEvaluation = function() {
206 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
207 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
214 url: url("data/test.html"),
215 success: function(data) {
216 jQuery("#ap").html(data);
217 ok( data.match(/^html text/), 'Check content for datatype html' );
218 setTimeout(verifyEvaluation, 600);
223 test("serialize()", function() {
226 equals( jQuery('#form').serialize(),
227 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&select1=&select2=3&select3=1&select3=2",
228 'Check form serialization as query string');
230 equals( jQuery('#form :input').serialize(),
231 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&select1=&select2=3&select3=1&select3=2",
232 'Check input serialization as query string');
234 equals( jQuery('#testForm').serialize(),
235 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
236 'Check form serialization as query string');
238 equals( jQuery('#testForm :input').serialize(),
239 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
240 'Check input serialization as query string');
242 equals( jQuery('#form, #testForm').serialize(),
243 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&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=",
244 'Multiple form serialization as query string');
246 /* Temporarily disabled. Opera 10 has problems with form serialization.
247 equals( jQuery('#form, #testForm :input').serialize(),
248 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&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=",
249 'Mixed form/input serialization as query string');
253 test("jQuery.param()", function() {
255 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
256 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
258 params = {someName: [1, 2, 3], regularThing: "blah" };
259 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
261 params = {foo: ['a', 'b', 'c']};
262 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
264 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
265 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
267 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
268 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" );
270 params = {foo: {bar: "baz", beep: 42, quux: "All your base are belong to us"}};
271 equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "nested object" );
273 params = {foo: {"bar[]": [1,2,3]}};
274 equals( jQuery.param(params), "foo%5Bbar%5D%5B%5D=1&foo%5Bbar%5D%5B%5D=2&foo%5Bbar%5D%5B%5D=3", "nested array");
276 params = {foo: [{bar: "baz", beep: 42}, {bar: "baz2", beep: 43}]};
277 equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bbar%5D=baz2&foo%5Bbeep%5D=43", "nested array of objects" );
281 test("synchronous request", function() {
283 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
286 test("synchronous request with callbacks", function() {
289 jQuery.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
290 ok( /^{ "data"/.test( result ), "check returned text" );
293 test("pass-through request object", function() {
297 var target = "data/name.html";
298 var successCount = 0;
301 var success = function() {
304 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
306 errorEx += ": " + xml.status;
308 jQuery("#foo").one('ajaxStop', function () {
309 equals(successCount, 5, "Check all ajax calls successful");
310 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
311 jQuery("#foo").unbind('ajaxError');
316 ok( jQuery.get(url(target), success), "get" );
317 ok( jQuery.post(url(target), success), "post" );
318 ok( jQuery.getScript(url("data/test.js"), success), "script" );
319 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
320 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
323 test("ajax cache", function () {
330 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
331 var re = /_=(.*?)(&|$)/g;
333 for (var i = 0; i < 6; i++) {
334 var ret = re.exec(s.url);
340 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
341 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
346 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
347 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
348 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
349 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
350 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
351 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
354 test("global ajaxSettings", function() {
357 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
358 var orig = { url: "data/with_fries.xml" };
361 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
363 t = jQuery.extend({}, orig);
366 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
368 t = jQuery.extend({}, orig);
369 t.data = { zoo: 'a', ping: 'b' };
371 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' }" );
373 jQuery.ajaxSettings = tmp;
376 test("load(String)", function() {
378 stop(); // check if load can be called with only url
379 jQuery('#first').load("data/name.html", start);
382 test("load('url selector')", function() {
384 stop(); // check if load can be called with only url
385 jQuery('#first').load("data/test3.html div.user", function(){
386 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
391 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
394 jQuery.ajaxSetup({ dataType: "json" });
395 jQuery("#first").ajaxComplete(function (e, xml, s) {
396 equals( s.dataType, "html", "Verify the load() dataType was html" );
397 jQuery("#first").unbind("ajaxComplete");
398 jQuery.ajaxSetup({ dataType: "" });
401 jQuery('#first').load("data/test3.html");
404 test("load(String, Function) - simple: inject text into DOM", function() {
407 jQuery('#first').load(url("data/name.html"), function() {
408 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
413 test("load(String, Function) - check scripts", function() {
417 var verifyEvaluation = function() {
418 equals( foobar, "bar", 'Check if script src was evaluated after load' );
419 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
423 jQuery('#first').load(url('data/test.html'), function() {
424 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
425 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
426 equals( testFoo, "foo", 'Check if script was evaluated after load' );
427 setTimeout(verifyEvaluation, 600);
431 test("load(String, Function) - check file with only a script tag", function() {
435 jQuery('#first').load(url('data/test2.html'), function() {
436 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
437 equals( testFoo, "foo", 'Check if script was evaluated after load' );
443 test("load(String, Object, Function)", function() {
447 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
448 var $post = jQuery(this).find('#post');
449 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
450 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
455 test("load(String, String, Function)", function() {
459 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
460 var $get = jQuery(this).find('#get');
461 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
462 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
467 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
470 jQuery.get(url('data/dashboard.xml'), function(xml) {
472 jQuery('tab', xml).each(function() {
473 content.push(jQuery(this).text());
475 equals( content[0], 'blabla', 'Check first tab');
476 equals( content[1], 'blublu', 'Check second tab');
481 test("jQuery.getScript(String, Function) - with callback", function() {
484 jQuery.getScript(url("data/test.js"), function() {
485 equals( foobar, "bar", 'Check if script was evaluated' );
486 setTimeout(start, 100);
490 test("jQuery.getScript(String, Function) - no callback", function() {
493 jQuery.getScript(url("data/test.js"), function(){
498 test("jQuery.ajax() - JSONP, Local", function() {
502 function plus(){ if ( ++count == 7 ) start(); }
507 url: "data/jsonp.php",
509 success: function(data){
510 ok( data.data, "JSON results returned (GET, no callback)" );
513 error: function(data){
514 ok( false, "Ajax error JSON (GET, no callback)" );
520 url: "data/jsonp.php?callback=?",
522 success: function(data){
523 ok( data.data, "JSON results returned (GET, url callback)" );
526 error: function(data){
527 ok( false, "Ajax error JSON (GET, url callback)" );
533 url: "data/jsonp.php",
536 success: function(data){
537 ok( data.data, "JSON results returned (GET, data callback)" );
540 error: function(data){
541 ok( false, "Ajax error JSON (GET, data callback)" );
547 url: "data/jsonp.php",
550 success: function(data){
551 ok( data.data, "JSON results returned (GET, data obj callback)" );
554 error: function(data){
555 ok( false, "Ajax error JSON (GET, data obj callback)" );
562 url: "data/jsonp.php",
564 success: function(data){
565 ok( data.data, "JSON results returned (POST, no callback)" );
568 error: function(data){
569 ok( false, "Ajax error JSON (GET, data obj callback)" );
576 url: "data/jsonp.php",
579 success: function(data){
580 ok( data.data, "JSON results returned (POST, data callback)" );
583 error: function(data){
584 ok( false, "Ajax error JSON (POST, data callback)" );
591 url: "data/jsonp.php",
594 success: function(data){
595 ok( data.data, "JSON results returned (POST, data obj callback)" );
598 error: function(data){
599 ok( false, "Ajax error JSON (POST, data obj callback)" );
605 test("jQuery.ajax() - JSONP, Remote", function() {
609 function plus(){ if ( ++count == 4 ) start(); }
611 var base = window.location.href.replace(/\?.*$/, "");
616 url: base + "data/jsonp.php",
618 success: function(data){
619 ok( data.data, "JSON results returned (GET, no callback)" );
622 error: function(data){
623 ok( false, "Ajax error JSON (GET, no callback)" );
629 url: base + "data/jsonp.php?callback=?",
631 success: function(data){
632 ok( data.data, "JSON results returned (GET, url callback)" );
635 error: function(data){
636 ok( false, "Ajax error JSON (GET, url callback)" );
642 url: base + "data/jsonp.php",
645 success: function(data){
646 ok( data.data, "JSON results returned (GET, data callback)" );
649 error: function(data){
650 ok( false, "Ajax error JSON (GET, data callback)" );
656 url: base + "data/jsonp.php",
659 success: function(data){
660 ok( data.data, "JSON results returned (GET, data obj callback)" );
663 error: function(data){
664 ok( false, "Ajax error JSON (GET, data obj callback)" );
670 test("jQuery.ajax() - script, Remote", function() {
673 var base = window.location.href.replace(/\?.*$/, "");
678 url: base + "data/test.js",
680 success: function(data){
681 ok( foobar, "Script results returned (GET, no callback)" );
687 test("jQuery.ajax() - script, Remote with POST", function() {
690 var base = window.location.href.replace(/\?.*$/, "");
695 url: base + "data/test.js",
698 success: function(data, status){
699 ok( foobar, "Script results returned (POST, no callback)" );
700 equals( status, "success", "Script results returned (POST, no callback)" );
703 error: function(xhr) {
704 ok( false, "ajax error, status code: " + xhr.status );
710 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
713 var base = window.location.href.replace(/\?.*$/, "");
714 base = base.replace(/^.*?\/\//, "//");
719 url: base + "data/test.js",
721 success: function(data){
722 ok( foobar, "Script results returned (GET, no callback)" );
728 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
731 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
732 ok( json.length >= 2, "Check length");
733 equals( json[0].name, 'John', 'Check JSON: first, name' );
734 equals( json[0].age, 21, 'Check JSON: first, age' );
735 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
736 equals( json[1].age, 25, 'Check JSON: second, age' );
741 test("jQuery.getJSON(String, Function) - JSON object", function() {
744 jQuery.getJSON(url("data/json.php"), function(json) {
745 if (json && json.data) {
746 equals( json.data.lang, 'en', 'Check JSON: lang' );
747 equals( json.data.length, 25, 'Check JSON: length' );
753 test("jQuery.getJSON - Using Native JSON", function() {
756 var old = window.JSON;
758 parse: function(str){
759 ok( true, "Verifying that parse method was run" );
765 jQuery.getJSON(url("data/json.php"), function(json) {
770 equals( json, true, "Verifying return value" );
775 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
778 var base = window.location.href.replace(/\?.*$/, "");
781 jQuery.getJSON(url(base + "data/json.php"), function(json) {
782 equals( json.data.lang, 'en', 'Check JSON: lang' );
783 equals( json.data.length, 25, 'Check JSON: length' );
788 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
793 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
794 jQuery('math', xml).each(function() {
795 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
796 equals( jQuery('result', this).text(), '3', 'Check for XML' );
798 if ( ++done === 2 ) start();
801 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
802 jQuery('math', xml).each(function() {
803 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
804 equals( jQuery('result', this).text(), '3', 'Check for XML' );
806 if ( ++done === 2 ) start();
810 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
815 jQuery.ajaxSetup({timeout: 1000});
817 var pass = function() {
820 ok( true, 'Check local and global callbacks after timeout' );
821 jQuery('#main').unbind("ajaxError");
826 var fail = function(a,b,c) {
827 ok( false, 'Check for timeout failed ' + a + ' ' + b );
831 jQuery('#main').ajaxError(pass);
835 url: url("data/name.php?wait=5"),
841 jQuery.ajaxSetup({timeout: 0});
844 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
846 jQuery.ajaxSetup({timeout: 50});
851 url: url("data/name.php?wait=1"),
853 ok( false, 'Check for local timeout failed' );
856 success: function() {
857 ok( true, 'Check for local timeout' );
863 jQuery.ajaxSetup({timeout: 0});
866 test("jQuery.ajax - simple get", function() {
871 url: url("data/name.php?name=foo"),
872 success: function(msg){
873 equals( msg, 'bar', 'Check for GET' );
879 test("jQuery.ajax - simple post", function() {
884 url: url("data/name.php"),
886 success: function(msg){
887 equals( msg, 'pan', 'Check for POST' );
893 test("ajaxSetup()", function() {
897 url: url("data/name.php?name=foo"),
898 success: function(msg){
899 equals( msg, 'bar', 'Check for GET' );
907 test("custom timeout does not set error message when timeout occurs, see #970", function() {
910 url: "data/name.php?wait=1",
912 error: function(request, status) {
913 ok( status != null, "status shouldn't be null in error handler" );
914 equals( "timeout", status );
921 test("data option: evaluate function values (#2806)", function() {
924 url: "data/echoQuery.php",
930 success: function(result) {
931 equals( result, "key=value" );
937 test("jQuery.ajax - If-Modified-Since support", function() {
942 var url = "data/if_modified_since.php?ts=" + new Date();
947 success: function(data, status) {
948 equals(status, "success");
953 success: function(data, status) {
954 if ( data === "FAIL" ) {
955 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
956 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
958 equals(status, "notmodified");
959 ok(data == null, "response body should be empty")
968 test("jQuery.ajax - Etag support", function() {
973 var url = "data/etag.php?ts=" + new Date();
978 success: function(data, status) {
979 equals(status, "success");
984 success: function(data, status) {
985 if ( data === "FAIL" ) {
986 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
987 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
989 equals(status, "notmodified");
990 ok(data == null, "response body should be empty")