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("$.ajax() - success callbacks", function() {
13 $.ajaxSetup({ timeout: 0 });
17 setTimeout(function(){
18 $('#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("$.ajax() - error callbacks", function() {
47 $('#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 $.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() - disabled globals", function() {
77 $('#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("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
108 url: url("data/with_fries.xml"),
110 success: function(resp) {
111 equals( $("properties", resp).length, 1, 'properties in responseXML' );
112 equals( $("jsconf", resp).length, 1, 'jsconf in responseXML' );
113 equals( $("thing", resp).length, 2, 'things in responseXML' );
119 test("$.ajax - beforeSend", function() {
125 $.ajaxSetup({ timeout: 0 });
128 url: url("data/name.html"),
129 beforeSend: function(xml) {
132 success: function(data) {
133 ok( check, "check beforeSend was executed" );
141 test("$.ajax - dataType html", function() {
148 var verifyEvaluation = function() {
149 ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
150 ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
156 url: url("data/test.html"),
157 success: function(data) {
159 ok( data.match(/^html text/), 'Check content for datatype html' );
160 setTimeout(verifyEvaluation, 600);
165 test("serialize()", function() {
168 equals( $('#form').serialize(),
169 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2",
170 'Check form serialization as query string');
172 equals( $('#form :input').serialize(),
173 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2",
174 'Check input serialization as query string');
176 equals( $('#testForm').serialize(),
177 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
178 'Check form serialization as query string');
180 equals( $('#testForm :input').serialize(),
181 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
182 'Check input serialization as query string');
184 equals( $('#form, #testForm').serialize(),
185 "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=",
186 'Multiple form serialization as query string');
188 equals( $('#form, #testForm :input').serialize(),
189 "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=",
190 'Mixed form/input serialization as query string');
193 test("$.param()", function() {
195 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
196 equals( $.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
198 params = {someName: [1, 2, 3], regularThing: "blah" };
199 equals( $.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
201 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
202 equals( $.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
204 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
205 equals( $.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" );
208 test("synchronous request", function() {
210 ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
213 test("synchronous request with callbacks", function() {
216 $.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
217 ok( /^{ "data"/.test( result ), "check returned text" );
220 test("pass-through request object", function() {
224 var target = "data/name.html";
225 var successCount = 0;
228 var success = function() {
231 $("#foo").ajaxError(function (e, xml, s, ex) {
233 errorEx += ": " + xml.status;
235 $("#foo").one('ajaxStop', function () {
236 equals(successCount, 5, "Check all ajax calls successful");
237 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
238 $("#foo").unbind('ajaxError');
242 ok( $.get(url(target), success), "get" );
243 ok( $.post(url(target), success), "post" );
244 ok( $.getScript(url("data/test.js"), success), "script" );
245 ok( $.getJSON(url("data/json_obj.js"), success), "json" );
246 ok( $.ajax({url: url(target), success: success}), "generic" );
249 test("ajax cache", function () {
255 $("#firstp").bind("ajaxSuccess", function (e, xml, s) {
256 var re = /_=(.*?)(&|$)/g;
258 for (var i = 0; i < 6; i++) {
259 var ret = re.exec(s.url);
265 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
266 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
271 ok( $.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
272 ok( $.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
273 ok( $.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
274 ok( $.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
275 ok( $.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
276 ok( $.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
279 test("global ajaxSettings", function() {
282 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
283 var orig = { url: "data/with_fries.xml", data: null };
286 $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
288 t = jQuery.extend({}, orig);
290 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending null" );
292 t = jQuery.extend({}, orig);
295 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
297 t = jQuery.extend({}, orig);
298 t.data = { zoo: 'a', ping: 'b' };
300 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' }" );
302 jQuery.ajaxSettings = tmp;
305 test("load(String)", function() {
307 stop(true); // check if load can be called with only url
308 $('#first').load("data/name.html", start);
311 test("load('url selector')", function() {
313 stop(true); // check if load can be called with only url
314 $('#first').load("data/test3.html div.user", function(){
315 equals( $(this).children("div").length, 2, "Verify that specific elements were injected" );
320 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
323 $.ajaxSetup({ dataType: "json" });
324 $("#first").ajaxComplete(function (e, xml, s) {
325 equals( s.dataType, "html", "Verify the load() dataType was html" );
326 $("#first").unbind("ajaxComplete");
327 $.ajaxSetup({ dataType: "" });
330 $('#first').load("data/test3.html");
333 test("load(String, Function) - simple: inject text into DOM", function() {
336 $('#first').load(url("data/name.html"), function() {
337 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
342 test("load(String, Function) - check scripts", function() {
345 window.testFoo = undefined;
346 window.foobar = null;
347 var verifyEvaluation = function() {
348 equals( foobar, "bar", 'Check if script src was evaluated after load' );
349 equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
352 $('#first').load(url('data/test.html'), function() {
353 ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
354 equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
355 equals( testFoo, "foo", 'Check if script was evaluated after load' );
356 setTimeout(verifyEvaluation, 600);
360 test("load(String, Function) - check file with only a script tag", function() {
364 $('#first').load(url('data/test2.html'), function() {
365 ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
366 ok( testFoo == "foo", 'Check if script was evaluated after load' );
371 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
374 $.get(url('data/dashboard.xml'), function(xml) {
376 $('tab', xml).each(function() {
377 content.push($(this).text());
379 equals( content[0], 'blabla', 'Check first tab');
380 equals( content[1], 'blublu', 'Check second tab');
385 test("$.getScript(String, Function) - with callback", function() {
388 window.foobar = null;
389 $.getScript(url("data/test.js"), function() {
390 equals( foobar, "bar", 'Check if script was evaluated' );
391 setTimeout(start, 100);
395 test("$.getScript(String, Function) - no callback", function() {
398 $.getScript(url("data/test.js"), start);
401 test("$.ajax() - JSONP, Local", function() {
405 function plus(){ if ( ++count == 7 ) start(); }
410 url: "data/jsonp.php",
412 success: function(data){
413 ok( data.data, "JSON results returned (GET, no callback)" );
416 error: function(data){
417 ok( false, "Ajax error JSON (GET, no callback)" );
423 url: "data/jsonp.php?callback=?",
425 success: function(data){
426 ok( data.data, "JSON results returned (GET, url callback)" );
429 error: function(data){
430 ok( false, "Ajax error JSON (GET, url callback)" );
436 url: "data/jsonp.php",
439 success: function(data){
440 ok( data.data, "JSON results returned (GET, data callback)" );
443 error: function(data){
444 ok( false, "Ajax error JSON (GET, data callback)" );
450 url: "data/jsonp.php",
453 success: function(data){
454 ok( data.data, "JSON results returned (GET, data obj callback)" );
457 error: function(data){
458 ok( false, "Ajax error JSON (GET, data obj callback)" );
465 url: "data/jsonp.php",
467 success: function(data){
468 ok( data.data, "JSON results returned (POST, no callback)" );
471 error: function(data){
472 ok( false, "Ajax error JSON (GET, data obj callback)" );
479 url: "data/jsonp.php",
482 success: function(data){
483 ok( data.data, "JSON results returned (POST, data callback)" );
486 error: function(data){
487 ok( false, "Ajax error JSON (POST, data callback)" );
494 url: "data/jsonp.php",
497 success: function(data){
498 ok( data.data, "JSON results returned (POST, data obj callback)" );
501 error: function(data){
502 ok( false, "Ajax error JSON (POST, data obj callback)" );
508 test("$.ajax() - JSONP, Remote", function() {
512 function plus(){ if ( ++count == 4 ) start(); }
514 var base = window.location.href.replace(/\?.*$/, "");
519 url: base + "data/jsonp.php",
521 success: function(data){
522 ok( data.data, "JSON results returned (GET, no callback)" );
525 error: function(data){
526 ok( false, "Ajax error JSON (GET, no callback)" );
532 url: base + "data/jsonp.php?callback=?",
534 success: function(data){
535 ok( data.data, "JSON results returned (GET, url callback)" );
538 error: function(data){
539 ok( false, "Ajax error JSON (GET, url callback)" );
545 url: base + "data/jsonp.php",
548 success: function(data){
549 ok( data.data, "JSON results returned (GET, data callback)" );
552 error: function(data){
553 ok( false, "Ajax error JSON (GET, data callback)" );
559 url: base + "data/jsonp.php",
562 success: function(data){
563 ok( data.data, "JSON results returned (GET, data obj callback)" );
566 error: function(data){
567 ok( false, "Ajax error JSON (GET, data obj callback)" );
573 test("$.ajax() - script, Remote", function() {
576 var base = window.location.href.replace(/\?.*$/, "");
580 window.foobar = null;
582 url: base + "data/test.js",
584 success: function(data){
585 ok( foobar, "Script results returned (GET, no callback)" );
591 test("$.ajax() - script, Remote with POST", function() {
594 var base = window.location.href.replace(/\?.*$/, "");
598 window.foobar = null;
600 url: base + "data/test.js",
603 success: function(data, status){
604 ok( foobar, "Script results returned (GET, no callback)" );
605 equals( status, "success", "Script results returned (GET, no callback)" );
611 test("$.ajax() - script, Remote with scheme-less URL", function() {
614 var base = window.location.href.replace(/\?.*$/, "");
615 base = base.replace(/^.*?\/\//, "//");
619 window.foobar = null;
621 url: base + "data/test.js",
623 success: function(data){
624 ok( foobar, "Script results returned (GET, no callback)" );
630 test("$.getJSON(String, Hash, Function) - JSON array", function() {
633 $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
634 ok( json[0].name == 'John', 'Check JSON: first, name' );
635 ok( json[0].age == 21, 'Check JSON: first, age' );
636 ok( json[1].name == 'Peter', 'Check JSON: second, name' );
637 ok( json[1].age == 25, 'Check JSON: second, age' );
642 test("$.getJSON(String, Function) - JSON object", function() {
645 $.getJSON(url("data/json.php"), function(json) {
646 ok( json.data.lang == 'en', 'Check JSON: lang' );
647 ok( json.data.length == 25, 'Check JSON: length' );
652 test("$.getJSON(String, Function) - JSON object with absolute url to local content", function() {
655 var base = window.location.href.replace(/\?.*$/, "");
658 $.getJSON(url(base + "data/json.php"), function(json) {
659 ok( json.data.lang == 'en', 'Check JSON: lang' );
660 ok( json.data.length == 25, 'Check JSON: length' );
665 test("$.post(String, Hash, Function) - simple with xml", function() {
668 $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
669 $('math', xml).each(function() {
670 ok( $('calculation', this).text() == '5-2', 'Check for XML' );
671 ok( $('result', this).text() == '3', 'Check for XML' );
675 $.post(url("data/name.php?xml=5-2"), {}, function(xml){
676 $('math', xml).each(function() {
677 ok( $('calculation', this).text() == '5-2', 'Check for XML' );
678 ok( $('result', this).text() == '3', 'Check for XML' );
684 test("$.ajaxSetup({timeout: Number}) - with global timeout", function() {
689 $.ajaxSetup({timeout: 1000});
691 var pass = function() {
694 ok( true, 'Check local and global callbacks after timeout' );
695 $('#main').unbind("ajaxError");
700 var fail = function(a,b,c) {
701 ok( false, 'Check for timeout failed ' + a + ' ' + b );
705 $('#main').ajaxError(pass);
709 url: url("data/name.php?wait=5"),
715 $.ajaxSetup({timeout: 0});
718 test("$.ajaxSetup({timeout: Number}) with localtimeout", function() {
720 $.ajaxSetup({timeout: 50});
725 url: url("data/name.php?wait=1"),
727 ok( false, 'Check for local timeout failed' );
730 success: function() {
731 ok( true, 'Check for local timeout' );
737 $.ajaxSetup({timeout: 0});
740 test("$.ajax - simple get", function() {
745 url: url("data/name.php?name=foo"),
746 success: function(msg){
747 ok( msg == 'bar', 'Check for GET' );
753 test("$.ajax - simple post", function() {
758 url: url("data/name.php"),
760 success: function(msg){
761 ok( msg == 'pan', 'Check for POST' );
767 test("ajaxSetup()", function() {
771 url: url("data/name.php?name=foo"),
772 success: function(msg){
773 ok( msg == 'bar', 'Check for GET' );
780 test("custom timeout does not set error message when timeout occurs, see #970", function() {
783 url: "data/name.php?wait=10",
785 error: function(request, status) {
786 ok( status != null, "status shouldn't be null in error handler" );
787 equals( "timeout", status );