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[]":["baz", 42, "All your base are belong to us"]};
224 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
226 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
227 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" );
230 test("synchronous request", function() {
232 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
235 test("synchronous request with callbacks", function() {
238 jQuery.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
239 ok( /^{ "data"/.test( result ), "check returned text" );
242 test("pass-through request object", function() {
246 var target = "data/name.html";
247 var successCount = 0;
250 var success = function() {
253 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
255 errorEx += ": " + xml.status;
257 jQuery("#foo").one('ajaxStop', function () {
258 equals(successCount, 5, "Check all ajax calls successful");
259 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
260 jQuery("#foo").unbind('ajaxError');
265 ok( jQuery.get(url(target), success), "get" );
266 ok( jQuery.post(url(target), success), "post" );
267 ok( jQuery.getScript(url("data/test.js"), success), "script" );
268 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
269 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
272 test("ajax cache", function () {
278 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
279 var re = /_=(.*?)(&|$)/g;
281 for (var i = 0; i < 6; i++) {
282 var ret = re.exec(s.url);
288 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
289 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
294 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
295 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
296 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
297 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
298 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
299 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
302 test("global ajaxSettings", function() {
305 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
306 var orig = { url: "data/with_fries.xml" };
309 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
311 t = jQuery.extend({}, orig);
314 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
316 t = jQuery.extend({}, orig);
317 t.data = { zoo: 'a', ping: 'b' };
319 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' }" );
321 jQuery.ajaxSettings = tmp;
324 test("load(String)", function() {
326 stop(); // check if load can be called with only url
327 jQuery('#first').load("data/name.html", start);
330 test("load('url selector')", function() {
332 stop(); // check if load can be called with only url
333 jQuery('#first').load("data/test3.html div.user", function(){
334 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
339 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
342 jQuery.ajaxSetup({ dataType: "json" });
343 jQuery("#first").ajaxComplete(function (e, xml, s) {
344 equals( s.dataType, "html", "Verify the load() dataType was html" );
345 jQuery("#first").unbind("ajaxComplete");
346 jQuery.ajaxSetup({ dataType: "" });
349 jQuery('#first').load("data/test3.html");
352 test("load(String, Function) - simple: inject text into DOM", function() {
355 jQuery('#first').load(url("data/name.html"), function() {
356 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
361 test("load(String, Function) - check scripts", function() {
365 var verifyEvaluation = function() {
366 equals( foobar, "bar", 'Check if script src was evaluated after load' );
367 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
371 jQuery('#first').load(url('data/test.html'), function() {
372 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
373 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
374 equals( testFoo, "foo", 'Check if script was evaluated after load' );
375 setTimeout(verifyEvaluation, 600);
379 test("load(String, Function) - check file with only a script tag", function() {
383 jQuery('#first').load(url('data/test2.html'), function() {
384 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
385 equals( testFoo, "foo", 'Check if script was evaluated after load' );
391 test("load(String, Object, Function)", function() {
395 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
396 var $post = jQuery(this).find('#post');
397 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
398 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
403 test("load(String, String, Function)", function() {
407 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
408 var $get = jQuery(this).find('#get');
409 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
410 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
415 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
418 jQuery.get(url('data/dashboard.xml'), function(xml) {
420 jQuery('tab', xml).each(function() {
421 content.push(jQuery(this).text());
423 equals( content[0], 'blabla', 'Check first tab');
424 equals( content[1], 'blublu', 'Check second tab');
429 test("jQuery.getScript(String, Function) - with callback", function() {
432 jQuery.getScript(url("data/test.js"), function() {
433 equals( foobar, "bar", 'Check if script was evaluated' );
434 setTimeout(start, 100);
438 test("jQuery.getScript(String, Function) - no callback", function() {
441 jQuery.getScript(url("data/test.js"), function(){
446 test("jQuery.ajax() - JSONP, Local", function() {
450 function plus(){ if ( ++count == 7 ) start(); }
455 url: "data/jsonp.php",
457 success: function(data){
458 ok( data.data, "JSON results returned (GET, no callback)" );
461 error: function(data){
462 ok( false, "Ajax error JSON (GET, no callback)" );
468 url: "data/jsonp.php?callback=?",
470 success: function(data){
471 ok( data.data, "JSON results returned (GET, url callback)" );
474 error: function(data){
475 ok( false, "Ajax error JSON (GET, url callback)" );
481 url: "data/jsonp.php",
484 success: function(data){
485 ok( data.data, "JSON results returned (GET, data callback)" );
488 error: function(data){
489 ok( false, "Ajax error JSON (GET, data callback)" );
495 url: "data/jsonp.php",
498 success: function(data){
499 ok( data.data, "JSON results returned (GET, data obj callback)" );
502 error: function(data){
503 ok( false, "Ajax error JSON (GET, data obj callback)" );
510 url: "data/jsonp.php",
512 success: function(data){
513 ok( data.data, "JSON results returned (POST, no callback)" );
516 error: function(data){
517 ok( false, "Ajax error JSON (GET, data obj callback)" );
524 url: "data/jsonp.php",
527 success: function(data){
528 ok( data.data, "JSON results returned (POST, data callback)" );
531 error: function(data){
532 ok( false, "Ajax error JSON (POST, data callback)" );
539 url: "data/jsonp.php",
542 success: function(data){
543 ok( data.data, "JSON results returned (POST, data obj callback)" );
546 error: function(data){
547 ok( false, "Ajax error JSON (POST, data obj callback)" );
553 test("jQuery.ajax() - JSONP, Remote", function() {
557 function plus(){ if ( ++count == 4 ) start(); }
559 var base = window.location.href.replace(/\?.*$/, "");
564 url: base + "data/jsonp.php",
566 success: function(data){
567 ok( data.data, "JSON results returned (GET, no callback)" );
570 error: function(data){
571 ok( false, "Ajax error JSON (GET, no callback)" );
577 url: base + "data/jsonp.php?callback=?",
579 success: function(data){
580 ok( data.data, "JSON results returned (GET, url callback)" );
583 error: function(data){
584 ok( false, "Ajax error JSON (GET, url callback)" );
590 url: base + "data/jsonp.php",
593 success: function(data){
594 ok( data.data, "JSON results returned (GET, data callback)" );
597 error: function(data){
598 ok( false, "Ajax error JSON (GET, data callback)" );
604 url: base + "data/jsonp.php",
607 success: function(data){
608 ok( data.data, "JSON results returned (GET, data obj callback)" );
611 error: function(data){
612 ok( false, "Ajax error JSON (GET, data obj callback)" );
618 test("jQuery.ajax() - script, Remote", function() {
621 var base = window.location.href.replace(/\?.*$/, "");
626 url: base + "data/test.js",
628 success: function(data){
629 ok( foobar, "Script results returned (GET, no callback)" );
635 test("jQuery.ajax() - script, Remote with POST", function() {
638 var base = window.location.href.replace(/\?.*$/, "");
643 url: base + "data/test.js",
646 success: function(data, status){
647 ok( foobar, "Script results returned (POST, no callback)" );
648 equals( status, "success", "Script results returned (POST, no callback)" );
651 error: function(xhr) {
652 ok( false, "ajax error, status code: " + xhr.status );
658 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
661 var base = window.location.href.replace(/\?.*$/, "");
662 base = base.replace(/^.*?\/\//, "//");
667 url: base + "data/test.js",
669 success: function(data){
670 ok( foobar, "Script results returned (GET, no callback)" );
676 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
679 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
680 equals( json[0].name, 'John', 'Check JSON: first, name' );
681 equals( json[0].age, 21, 'Check JSON: first, age' );
682 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
683 equals( json[1].age, 25, 'Check JSON: second, age' );
688 test("jQuery.getJSON(String, Function) - JSON object", function() {
691 jQuery.getJSON(url("data/json.php"), function(json) {
692 equals( json.data.lang, 'en', 'Check JSON: lang' );
693 equals( json.data.length, 25, 'Check JSON: length' );
698 test("jQuery.getJSON - Using Native JSON", function() {
701 var old = window.JSON;
703 parse: function(str){
704 ok( true, "Verifying that parse method was run" );
710 jQuery.getJSON(url("data/json.php"), function(json) {
712 equals( json, true, "Verifying return value" );
717 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
720 var base = window.location.href.replace(/\?.*$/, "");
723 jQuery.getJSON(url(base + "data/json.php"), function(json) {
724 equals( json.data.lang, 'en', 'Check JSON: lang' );
725 equals( json.data.length, 25, 'Check JSON: length' );
730 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
735 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
736 jQuery('math', xml).each(function() {
737 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
738 equals( jQuery('result', this).text(), '3', 'Check for XML' );
740 if ( ++done === 2 ) start();
743 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
744 jQuery('math', xml).each(function() {
745 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
746 equals( jQuery('result', this).text(), '3', 'Check for XML' );
748 if ( ++done === 2 ) start();
752 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
757 jQuery.ajaxSetup({timeout: 1000});
759 var pass = function() {
762 ok( true, 'Check local and global callbacks after timeout' );
763 jQuery('#main').unbind("ajaxError");
768 var fail = function(a,b,c) {
769 ok( false, 'Check for timeout failed ' + a + ' ' + b );
773 jQuery('#main').ajaxError(pass);
777 url: url("data/name.php?wait=5"),
783 jQuery.ajaxSetup({timeout: 0});
786 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
788 jQuery.ajaxSetup({timeout: 50});
793 url: url("data/name.php?wait=1"),
795 ok( false, 'Check for local timeout failed' );
798 success: function() {
799 ok( true, 'Check for local timeout' );
805 jQuery.ajaxSetup({timeout: 0});
808 test("jQuery.ajax - simple get", function() {
813 url: url("data/name.php?name=foo"),
814 success: function(msg){
815 equals( msg, 'bar', 'Check for GET' );
821 test("jQuery.ajax - simple post", function() {
826 url: url("data/name.php"),
828 success: function(msg){
829 equals( msg, 'pan', 'Check for POST' );
835 test("ajaxSetup()", function() {
839 url: url("data/name.php?name=foo"),
840 success: function(msg){
841 equals( msg, 'bar', 'Check for GET' );
849 test("custom timeout does not set error message when timeout occurs, see #970", function() {
852 url: "data/name.php?wait=1",
854 error: function(request, status) {
855 ok( status != null, "status shouldn't be null in error handler" );
856 equals( "timeout", status );
863 test("data option: evaluate function values (#2806)", function() {
866 url: "data/echoQuery.php",
872 success: function(result) {
873 equals( result, "key=value" );
879 test("jQuery.ajax - If-Modified-Since support", function() {
884 var url = "data/if_modified_since.php?ts=" + new Date();
889 success: function(data, status) {
890 equals(status, "success");
895 success: function(data, status) {
896 if ( data === "FAIL" ) {
897 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
898 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
900 equals(status, "notmodified");
901 ok(data == null, "response body should be empty")
910 test("jQuery.ajax - Etag support", function() {
915 var url = "data/etag.php?ts=" + new Date();
920 success: function(data, status) {
921 equals(status, "success");
926 success: function(data, status) {
927 if ( data === "FAIL" ) {
928 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
929 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
931 equals(status, "notmodified");
932 ok(data == null, "response body should be empty")