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&search=search&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&search=search&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&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=",
206 'Multiple form serialization as query string');
208 /* Temporarily disabled. Opera 10 has problems with form serialization.
209 equals( jQuery('#form, #testForm :input').serialize(),
210 "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=",
211 'Mixed form/input serialization as query string');
215 test("jQuery.param()", function() {
217 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
218 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
220 params = {someName: [1, 2, 3], regularThing: "blah" };
221 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
223 params = {foo: ['a', 'b', 'c']};
224 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
226 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
227 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
229 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
230 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 params = {foo: {bar: "baz", beep: 42, quux: "All your base are belong to us"}};
233 equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "nested object" );
235 params = {foo: {"bar[]": [1,2,3]}};
236 equals( jQuery.param(params), "foo%5Bbar%5D%5B%5D=1&foo%5Bbar%5D%5B%5D=2&foo%5Bbar%5D%5B%5D=3", "nested array");
238 params = {foo: [{bar: "baz", beep: 42}, {bar: "baz2", beep: 43}]};
239 equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bbar%5D=baz2&foo%5Bbeep%5D=43", "nested array of objects" );
243 test("synchronous request", function() {
245 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
248 test("synchronous request with callbacks", function() {
251 jQuery.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
252 ok( /^{ "data"/.test( result ), "check returned text" );
255 test("pass-through request object", function() {
259 var target = "data/name.html";
260 var successCount = 0;
263 var success = function() {
266 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
268 errorEx += ": " + xml.status;
270 jQuery("#foo").one('ajaxStop', function () {
271 equals(successCount, 5, "Check all ajax calls successful");
272 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
273 jQuery("#foo").unbind('ajaxError');
278 ok( jQuery.get(url(target), success), "get" );
279 ok( jQuery.post(url(target), success), "post" );
280 ok( jQuery.getScript(url("data/test.js"), success), "script" );
281 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
282 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
285 test("ajax cache", function () {
291 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
292 var re = /_=(.*?)(&|$)/g;
294 for (var i = 0; i < 6; i++) {
295 var ret = re.exec(s.url);
301 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
302 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
307 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
308 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
309 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
310 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
311 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
312 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
315 test("global ajaxSettings", function() {
318 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
319 var orig = { url: "data/with_fries.xml" };
322 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
324 t = jQuery.extend({}, orig);
327 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
329 t = jQuery.extend({}, orig);
330 t.data = { zoo: 'a', ping: 'b' };
332 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' }" );
334 jQuery.ajaxSettings = tmp;
337 test("load(String)", function() {
339 stop(); // check if load can be called with only url
340 jQuery('#first').load("data/name.html", start);
343 test("load('url selector')", function() {
345 stop(); // check if load can be called with only url
346 jQuery('#first').load("data/test3.html div.user", function(){
347 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
352 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
355 jQuery.ajaxSetup({ dataType: "json" });
356 jQuery("#first").ajaxComplete(function (e, xml, s) {
357 equals( s.dataType, "html", "Verify the load() dataType was html" );
358 jQuery("#first").unbind("ajaxComplete");
359 jQuery.ajaxSetup({ dataType: "" });
362 jQuery('#first').load("data/test3.html");
365 test("load(String, Function) - simple: inject text into DOM", function() {
368 jQuery('#first').load(url("data/name.html"), function() {
369 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
374 test("load(String, Function) - check scripts", function() {
378 var verifyEvaluation = function() {
379 equals( foobar, "bar", 'Check if script src was evaluated after load' );
380 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
384 jQuery('#first').load(url('data/test.html'), function() {
385 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
386 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
387 equals( testFoo, "foo", 'Check if script was evaluated after load' );
388 setTimeout(verifyEvaluation, 600);
392 test("load(String, Function) - check file with only a script tag", function() {
396 jQuery('#first').load(url('data/test2.html'), function() {
397 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
398 equals( testFoo, "foo", 'Check if script was evaluated after load' );
404 test("load(String, Object, Function)", function() {
408 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
409 var $post = jQuery(this).find('#post');
410 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
411 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
416 test("load(String, String, Function)", function() {
420 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
421 var $get = jQuery(this).find('#get');
422 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
423 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
428 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
431 jQuery.get(url('data/dashboard.xml'), function(xml) {
433 jQuery('tab', xml).each(function() {
434 content.push(jQuery(this).text());
436 equals( content[0], 'blabla', 'Check first tab');
437 equals( content[1], 'blublu', 'Check second tab');
442 test("jQuery.getScript(String, Function) - with callback", function() {
445 jQuery.getScript(url("data/test.js"), function() {
446 equals( foobar, "bar", 'Check if script was evaluated' );
447 setTimeout(start, 100);
451 test("jQuery.getScript(String, Function) - no callback", function() {
454 jQuery.getScript(url("data/test.js"), function(){
459 test("jQuery.ajax() - JSONP, Local", function() {
463 function plus(){ if ( ++count == 7 ) start(); }
468 url: "data/jsonp.php",
470 success: function(data){
471 ok( data.data, "JSON results returned (GET, no callback)" );
474 error: function(data){
475 ok( false, "Ajax error JSON (GET, no callback)" );
481 url: "data/jsonp.php?callback=?",
483 success: function(data){
484 ok( data.data, "JSON results returned (GET, url callback)" );
487 error: function(data){
488 ok( false, "Ajax error JSON (GET, url callback)" );
494 url: "data/jsonp.php",
497 success: function(data){
498 ok( data.data, "JSON results returned (GET, data callback)" );
501 error: function(data){
502 ok( false, "Ajax error JSON (GET, data callback)" );
508 url: "data/jsonp.php",
511 success: function(data){
512 ok( data.data, "JSON results returned (GET, data obj callback)" );
515 error: function(data){
516 ok( false, "Ajax error JSON (GET, data obj callback)" );
523 url: "data/jsonp.php",
525 success: function(data){
526 ok( data.data, "JSON results returned (POST, no callback)" );
529 error: function(data){
530 ok( false, "Ajax error JSON (GET, data obj callback)" );
537 url: "data/jsonp.php",
540 success: function(data){
541 ok( data.data, "JSON results returned (POST, data callback)" );
544 error: function(data){
545 ok( false, "Ajax error JSON (POST, data callback)" );
552 url: "data/jsonp.php",
555 success: function(data){
556 ok( data.data, "JSON results returned (POST, data obj callback)" );
559 error: function(data){
560 ok( false, "Ajax error JSON (POST, data obj callback)" );
566 test("jQuery.ajax() - JSONP, Remote", function() {
570 function plus(){ if ( ++count == 4 ) start(); }
572 var base = window.location.href.replace(/\?.*$/, "");
577 url: base + "data/jsonp.php",
579 success: function(data){
580 ok( data.data, "JSON results returned (GET, no callback)" );
583 error: function(data){
584 ok( false, "Ajax error JSON (GET, no callback)" );
590 url: base + "data/jsonp.php?callback=?",
592 success: function(data){
593 ok( data.data, "JSON results returned (GET, url callback)" );
596 error: function(data){
597 ok( false, "Ajax error JSON (GET, url callback)" );
603 url: base + "data/jsonp.php",
606 success: function(data){
607 ok( data.data, "JSON results returned (GET, data callback)" );
610 error: function(data){
611 ok( false, "Ajax error JSON (GET, data callback)" );
617 url: base + "data/jsonp.php",
620 success: function(data){
621 ok( data.data, "JSON results returned (GET, data obj callback)" );
624 error: function(data){
625 ok( false, "Ajax error JSON (GET, data obj callback)" );
631 test("jQuery.ajax() - script, Remote", function() {
634 var base = window.location.href.replace(/\?.*$/, "");
639 url: base + "data/test.js",
641 success: function(data){
642 ok( foobar, "Script results returned (GET, no callback)" );
648 test("jQuery.ajax() - script, Remote with POST", function() {
651 var base = window.location.href.replace(/\?.*$/, "");
656 url: base + "data/test.js",
659 success: function(data, status){
660 ok( foobar, "Script results returned (POST, no callback)" );
661 equals( status, "success", "Script results returned (POST, no callback)" );
664 error: function(xhr) {
665 ok( false, "ajax error, status code: " + xhr.status );
671 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
674 var base = window.location.href.replace(/\?.*$/, "");
675 base = base.replace(/^.*?\/\//, "//");
680 url: base + "data/test.js",
682 success: function(data){
683 ok( foobar, "Script results returned (GET, no callback)" );
689 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
692 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
693 equals( json[0].name, 'John', 'Check JSON: first, name' );
694 equals( json[0].age, 21, 'Check JSON: first, age' );
695 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
696 equals( json[1].age, 25, 'Check JSON: second, age' );
701 test("jQuery.getJSON(String, Function) - JSON object", function() {
704 jQuery.getJSON(url("data/json.php"), function(json) {
705 equals( json.data.lang, 'en', 'Check JSON: lang' );
706 equals( json.data.length, 25, 'Check JSON: length' );
711 test("jQuery.getJSON - Using Native JSON", function() {
714 var old = window.JSON;
716 parse: function(str){
717 ok( true, "Verifying that parse method was run" );
723 jQuery.getJSON(url("data/json.php"), function(json) {
725 equals( json, true, "Verifying return value" );
730 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
733 var base = window.location.href.replace(/\?.*$/, "");
736 jQuery.getJSON(url(base + "data/json.php"), function(json) {
737 equals( json.data.lang, 'en', 'Check JSON: lang' );
738 equals( json.data.length, 25, 'Check JSON: length' );
743 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
748 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
749 jQuery('math', xml).each(function() {
750 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
751 equals( jQuery('result', this).text(), '3', 'Check for XML' );
753 if ( ++done === 2 ) start();
756 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
757 jQuery('math', xml).each(function() {
758 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
759 equals( jQuery('result', this).text(), '3', 'Check for XML' );
761 if ( ++done === 2 ) start();
765 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
770 jQuery.ajaxSetup({timeout: 1000});
772 var pass = function() {
775 ok( true, 'Check local and global callbacks after timeout' );
776 jQuery('#main').unbind("ajaxError");
781 var fail = function(a,b,c) {
782 ok( false, 'Check for timeout failed ' + a + ' ' + b );
786 jQuery('#main').ajaxError(pass);
790 url: url("data/name.php?wait=5"),
796 jQuery.ajaxSetup({timeout: 0});
799 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
801 jQuery.ajaxSetup({timeout: 50});
806 url: url("data/name.php?wait=1"),
808 ok( false, 'Check for local timeout failed' );
811 success: function() {
812 ok( true, 'Check for local timeout' );
818 jQuery.ajaxSetup({timeout: 0});
821 test("jQuery.ajax - simple get", function() {
826 url: url("data/name.php?name=foo"),
827 success: function(msg){
828 equals( msg, 'bar', 'Check for GET' );
834 test("jQuery.ajax - simple post", function() {
839 url: url("data/name.php"),
841 success: function(msg){
842 equals( msg, 'pan', 'Check for POST' );
848 test("ajaxSetup()", function() {
852 url: url("data/name.php?name=foo"),
853 success: function(msg){
854 equals( msg, 'bar', 'Check for GET' );
862 test("custom timeout does not set error message when timeout occurs, see #970", function() {
865 url: "data/name.php?wait=1",
867 error: function(request, status) {
868 ok( status != null, "status shouldn't be null in error handler" );
869 equals( "timeout", status );
876 test("data option: evaluate function values (#2806)", function() {
879 url: "data/echoQuery.php",
885 success: function(result) {
886 equals( result, "key=value" );
892 test("jQuery.ajax - If-Modified-Since support", function() {
897 var url = "data/if_modified_since.php?ts=" + new Date();
902 success: function(data, status) {
903 equals(status, "success");
908 success: function(data, status) {
909 if ( data === "FAIL" ) {
910 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
911 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
913 equals(status, "notmodified");
914 ok(data == null, "response body should be empty")
923 test("jQuery.ajax - Etag support", function() {
928 var url = "data/etag.php?ts=" + new Date();
933 success: function(data, status) {
934 equals(status, "success");
939 success: function(data, status) {
940 if ( data === "FAIL" ) {
941 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
942 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
944 equals(status, "notmodified");
945 ok(data == null, "response body should be empty")