Tests: Make Android Browser 4.0-4.3 AJAX tests green
[jquery.git] / test / unit / ajax.js
blob7bf3f686b1dddcb5059469424117c3a5bd5d3120
1 QUnit.module( "ajax", {
2 afterEach: function() {
3 jQuery( document ).off( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError ajaxSuccess" );
4 moduleTeardown.apply( this, arguments );
6 } );
8 ( function() {
9 QUnit.test( "Unit Testing Environment", function( assert ) {
10 assert.expect( 2 );
12 assert.ok( hasPHP, "Running in an environment with PHP support. The AJAX tests only run if the environment supports PHP!" );
13 assert.ok( !isLocal, "Unit tests are not ran from file:// (especially in Chrome. If you must test from file:// with Chrome, run it with the --allow-file-access-from-files flag!)" );
14 } );
16 if ( !jQuery.ajax || ( isLocal && !hasPHP ) ) {
17 return;
20 function addGlobalEvents( expected, assert ) {
21 return function() {
22 expected = expected || "";
23 jQuery( document ).on( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError ajaxSuccess", function( e ) {
24 assert.ok( expected.indexOf( e.type ) !== -1, e.type );
25 } );
29 //----------- jQuery.ajax()
31 testIframe(
32 "XMLHttpRequest - Attempt to block tests because of dangling XHR requests (IE)",
33 "ajax/unreleasedXHR.html",
34 function( assert ) {
35 assert.expect( 1 );
36 assert.ok( true, "done" );
40 ajaxTest( "jQuery.ajax() - success callbacks", 8, function( assert ) {
41 return {
42 setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess", assert ),
43 url: url( "name.html" ),
44 beforeSend: function() {
45 assert.ok( true, "beforeSend" );
47 success: function() {
48 assert.ok( true, "success" );
50 complete: function() {
51 assert.ok( true, "complete" );
54 } );
56 ajaxTest( "jQuery.ajax() - success callbacks - (url, options) syntax", 8, function( assert ) {
57 return {
58 setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess", assert ),
59 create: function( options ) {
60 return jQuery.ajax( url( "name.html" ), options );
62 beforeSend: function() {
63 assert.ok( true, "beforeSend" );
65 success: function() {
66 assert.ok( true, "success" );
68 complete: function() {
69 assert.ok( true, "complete" );
72 } );
74 ajaxTest( "jQuery.ajax() - execute js for crossOrigin when dataType option is provided", 3,
75 function( assert ) {
76 return {
77 create: function( options ) {
78 options.crossDomain = true;
79 options.dataType = "script";
80 return jQuery.ajax( url( "mock.php?action=script&header=ecma" ), options );
82 success: function() {
83 assert.ok( true, "success" );
85 complete: function() {
86 assert.ok( true, "complete" );
92 ajaxTest( "jQuery.ajax() - custom attributes for script tag", 4,
93 function( assert ) {
94 return {
95 create: function( options ) {
96 var xhr;
97 options.dataType = "script";
98 options.scriptAttrs = { id: "jquery-ajax-test", async: "async" };
99 xhr = jQuery.ajax( url( "mock.php?action=script" ), options );
100 assert.equal( jQuery( "#jquery-ajax-test" ).attr( "async" ), "async", "attr value" );
101 return xhr;
103 success: function() {
104 assert.ok( true, "success" );
106 complete: function() {
107 assert.ok( true, "complete" );
113 ajaxTest( "jQuery.ajax() - do not execute js (crossOrigin)", 2, function( assert ) {
114 return {
115 create: function( options ) {
116 options.crossDomain = true;
117 return jQuery.ajax( url( "mock.php?action=script" ), options );
119 success: function() {
120 assert.ok( true, "success" );
122 fail: function() {
123 if ( jQuery.support.cors === false ) {
124 assert.ok( true, "fail" );
127 complete: function() {
128 assert.ok( true, "complete" );
131 } );
133 ajaxTest( "jQuery.ajax() - success callbacks (late binding)", 8, function( assert ) {
134 return {
135 setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess", assert ),
136 url: url( "name.html" ),
137 beforeSend: function() {
138 assert.ok( true, "beforeSend" );
140 success: true,
141 afterSend: function( request ) {
142 request.always( function() {
143 assert.ok( true, "complete" );
144 } ).done( function() {
145 assert.ok( true, "success" );
146 } ).fail( function() {
147 assert.ok( false, "error" );
148 } );
151 } );
153 ajaxTest( "jQuery.ajax() - success callbacks (oncomplete binding)", 8, function( assert ) {
154 return {
155 setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess", assert ),
156 url: url( "name.html" ),
157 beforeSend: function() {
158 assert.ok( true, "beforeSend" );
160 success: true,
161 complete: function( xhr ) {
162 xhr.always( function() {
163 assert.ok( true, "complete" );
164 } ).done( function() {
165 assert.ok( true, "success" );
166 } ).fail( function() {
167 assert.ok( false, "error" );
168 } );
171 } );
173 ajaxTest( "jQuery.ajax() - error callbacks", 8, function( assert ) {
174 return {
175 setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError", assert ),
176 url: url( "mock.php?action=wait&wait=5" ),
177 beforeSend: function() {
178 assert.ok( true, "beforeSend" );
180 afterSend: function( request ) {
181 request.abort();
183 error: function() {
184 assert.ok( true, "error" );
186 complete: function() {
187 assert.ok( true, "complete" );
190 } );
192 ajaxTest( "jQuery.ajax() - textStatus and errorThrown values", 4, function( assert ) {
193 return [ {
194 url: url( "mock.php?action=wait&wait=5" ),
195 error: function( _, textStatus, errorThrown ) {
196 assert.strictEqual( textStatus, "abort", "textStatus is 'abort' for abort" );
197 assert.strictEqual( errorThrown, "abort", "errorThrown is 'abort' for abort" );
199 afterSend: function( request ) {
200 request.abort();
204 url: url( "mock.php?action=wait&wait=5" ),
205 error: function( _, textStatus, errorThrown ) {
206 assert.strictEqual( textStatus, "mystatus", "textStatus is 'mystatus' for abort('mystatus')" );
207 assert.strictEqual( errorThrown, "mystatus", "errorThrown is 'mystatus' for abort('mystatus')" );
209 afterSend: function( request ) {
210 request.abort( "mystatus" );
212 } ];
213 } );
215 ajaxTest( "jQuery.ajax() - responseText on error", 1, function( assert ) {
216 return {
217 url: url( "mock.php?action=error" ),
218 error: function( xhr ) {
219 assert.strictEqual( xhr.responseText, "plain text message", "Test jqXHR.responseText is filled for HTTP errors" );
222 } );
224 QUnit.test( "jQuery.ajax() - retry with jQuery.ajax( this )", function( assert ) {
225 assert.expect( 2 );
226 var previousUrl,
227 firstTime = true,
228 done = assert.async();
229 jQuery.ajax( {
230 url: url( "mock.php?action=error" ),
231 error: function() {
232 if ( firstTime ) {
233 firstTime = false;
234 jQuery.ajax( this );
235 } else {
236 assert.ok( true, "Test retrying with jQuery.ajax(this) works" );
237 jQuery.ajax( {
238 url: url( "mock.php?action=error&x=2" ),
239 beforeSend: function() {
240 if ( !previousUrl ) {
241 previousUrl = this.url;
242 } else {
243 assert.strictEqual( this.url, previousUrl, "url parameters are not re-appended" );
244 done();
245 return false;
248 error: function() {
249 jQuery.ajax( this );
251 } );
254 } );
255 } );
257 ajaxTest( "jQuery.ajax() - headers", 8, function( assert ) {
258 return {
259 setup: function() {
260 jQuery( document ).ajaxSend( function( evt, xhr ) {
261 xhr.setRequestHeader( "ajax-send", "test" );
262 } );
264 url: url( "mock.php?action=headers&keys=siMPle|SometHing-elsE|OthEr|Nullable|undefined|Empty|ajax-send" ),
265 headers: {
266 "siMPle": "value",
267 "SometHing-elsE": "other value",
268 "OthEr": "something else",
269 "Nullable": null,
270 "undefined": undefined
272 // Support: IE 9 - 11, Edge 12 - 14 only
273 // Not all browsers allow empty-string headers
274 //"Empty": ""
276 success: function( data, _, xhr ) {
277 var i, emptyHeader,
278 isAndroid = /android 4\.[0-3]/i.test( navigator.userAgent ),
279 requestHeaders = jQuery.extend( this.headers, {
280 "ajax-send": "test"
281 } ),
282 tmp = [];
283 for ( i in requestHeaders ) {
284 tmp.push( i, ": ", requestHeaders[ i ] + "", "\n" );
286 tmp = tmp.join( "" );
288 assert.strictEqual( data, tmp, "Headers were sent" );
289 assert.strictEqual( xhr.getResponseHeader( "Sample-Header" ), "Hello World", "Sample header received" );
290 assert.ok( data.indexOf( "undefined" ) < 0, "Undefined header value was not sent" );
292 emptyHeader = xhr.getResponseHeader( "Empty-Header" );
293 if ( emptyHeader === null ) {
294 assert.ok( true, "Firefox doesn't support empty headers" );
295 } else {
296 assert.strictEqual( emptyHeader, "", "Empty header received" );
298 assert.strictEqual( xhr.getResponseHeader( "Sample-Header2" ), "Hello World 2", "Second sample header received" );
300 if ( isAndroid ) {
301 // Support: Android 4.0-4.3 only
302 // Android Browser only returns the last value for each header
303 // so there's no way for jQuery get all parts.
304 assert.ok( true, "Android doesn't support repeated header names" );
305 } else {
306 assert.strictEqual( xhr.getResponseHeader( "List-Header" ), "Item 1, Item 2", "List header received" );
309 if ( isAndroid && QUnit.isSwarm ) {
310 // Support: Android 4.0-4.3 on BrowserStack only
311 // Android Browser versions provided by BrowserStack fail this test
312 // while locally fired emulators don't, even when they connect
313 // to TestSwarm. Just skip the test there to avoid a red build.
314 assert.ok( true, "BrowserStack's Android fails the \"prototype collision (constructor)\" test" );
315 } else {
316 assert.strictEqual( xhr.getResponseHeader( "constructor" ), "prototype collision (constructor)", "constructor header received" );
318 assert.strictEqual( xhr.getResponseHeader( "__proto__" ), null, "Undefined __proto__ header not received" );
321 } );
323 ajaxTest( "jQuery.ajax() - Accept header", 1, function( assert ) {
324 return {
325 url: url( "mock.php?action=headers&keys=accept" ),
326 headers: {
327 Accept: "very wrong accept value"
329 beforeSend: function( xhr ) {
330 xhr.setRequestHeader( "Accept", "*/*" );
332 success: function( data ) {
333 assert.strictEqual( data, "accept: */*\n", "Test Accept header is set to last value provided" );
336 } );
338 ajaxTest( "jQuery.ajax() - contentType", 2, function( assert ) {
339 return [
341 url: url( "mock.php?action=headers&keys=content-type" ),
342 contentType: "test",
343 success: function( data ) {
344 assert.strictEqual( data, "content-type: test\n", "Test content-type is sent when options.contentType is set" );
348 url: url( "mock.php?action=headers&keys=content-type" ),
349 contentType: false,
350 success: function( data ) {
352 // Some server/interpreter combinations always supply a Content-Type to scripts
353 data = data || "content-type: \n";
354 assert.strictEqual( data, "content-type: \n", "Test content-type is not set when options.contentType===false" );
358 } );
360 ajaxTest( "jQuery.ajax() - protocol-less urls", 1, function( assert ) {
361 return {
362 url: "//somedomain.com",
363 beforeSend: function( xhr, settings ) {
364 assert.equal( settings.url, location.protocol + "//somedomain.com", "Make sure that the protocol is added." );
365 return false;
367 error: true
369 } );
371 ajaxTest( "jQuery.ajax() - URL fragment component preservation", 4, function( assert ) {
372 return [
374 url: baseURL + "name.html#foo",
375 beforeSend: function( xhr, settings ) {
376 assert.equal( settings.url, baseURL + "name.html#foo",
377 "hash preserved for request with no query component." );
378 return false;
380 error: true
383 url: baseURL + "name.html?abc#foo",
384 beforeSend: function( xhr, settings ) {
385 assert.equal( settings.url, baseURL + "name.html?abc#foo",
386 "hash preserved for request with query component." );
387 return false;
389 error: true
392 url: baseURL + "name.html?abc#foo",
393 data: {
394 "test": 123
396 beforeSend: function( xhr, settings ) {
397 assert.equal( settings.url, baseURL + "name.html?abc&test=123#foo",
398 "hash preserved for request with query component and data." );
399 return false;
401 error: true
404 url: baseURL + "name.html?abc#brownies",
405 data: {
406 "devo": "hat"
408 cache: false,
409 beforeSend: function( xhr, settings ) {
410 // Clear the cache-buster param value
411 var url = settings.url.replace( /_=[^&#]+/, "_=" );
412 assert.equal( url, baseURL + "name.html?abc&devo=hat&_=#brownies",
413 "hash preserved for cache-busting request with query component and data." );
414 return false;
416 error: true
419 } );
421 ajaxTest( "jQuery.ajax() - traditional param encoding", 4, function( assert ) {
422 return [
424 url: "/",
425 traditional: true,
426 data: {
427 "devo": "hat",
428 "answer": 42,
429 "quux": "a space"
431 beforeSend: function( xhr, settings ) {
432 assert.equal( settings.url, "/?devo=hat&answer=42&quux=a%20space", "Simple case" );
433 return false;
435 error: true
438 url: "/",
439 traditional: true,
440 data: {
441 "a": [ 1, 2, 3 ],
442 "b[]": [ "b1", "b2" ]
444 beforeSend: function( xhr, settings ) {
445 assert.equal( settings.url, "/?a=1&a=2&a=3&b%5B%5D=b1&b%5B%5D=b2", "Arrays" );
446 return false;
448 error: true
451 url: "/",
452 traditional: true,
453 data: {
454 "a": [ [ 1, 2 ], [ 3, 4 ], 5 ]
456 beforeSend: function( xhr, settings ) {
457 assert.equal( settings.url, "/?a=1%2C2&a=3%2C4&a=5", "Nested arrays" );
458 return false;
460 error: true
463 url: "/",
464 traditional: true,
465 data: {
466 "a": [ "w", [ [ "x", "y" ], "z" ] ]
468 cache: false,
469 beforeSend: function( xhr, settings ) {
470 var url = settings.url.replace( /\d{3,}/, "" );
471 assert.equal( url, "/?a=w&a=x%2Cy%2Cz&_=", "Cache-buster" );
472 return false;
474 error: true
477 } );
479 ajaxTest( "jQuery.ajax() - cross-domain detection", 8, function( assert ) {
480 function request( url, title, crossDomainOrOptions ) {
481 return jQuery.extend( {
482 dataType: "jsonp",
483 url: url,
484 beforeSend: function( _, s ) {
485 assert.ok( crossDomainOrOptions === false ? !s.crossDomain : s.crossDomain, title );
486 return false;
488 error: true
489 }, crossDomainOrOptions );
492 var loc = document.location,
493 samePort = loc.port || ( loc.protocol === "http:" ? 80 : 443 ),
494 otherPort = loc.port === 666 ? 667 : 666,
495 otherProtocol = loc.protocol === "http:" ? "https:" : "http:";
497 return [
498 request(
499 loc.protocol + "//" + loc.hostname + ":" + samePort,
500 "Test matching ports are not detected as cross-domain",
501 false
503 request(
504 otherProtocol + "//" + loc.host,
505 "Test different protocols are detected as cross-domain"
507 request(
508 "app:/path",
509 "Adobe AIR app:/ URL detected as cross-domain"
511 request(
512 loc.protocol + "//example.invalid:" + ( loc.port || 80 ),
513 "Test different hostnames are detected as cross-domain"
515 request(
516 loc.protocol + "//" + loc.hostname + ":" + otherPort,
517 "Test different ports are detected as cross-domain"
519 request(
520 "about:blank",
521 "Test about:blank is detected as cross-domain"
523 request(
524 loc.protocol + "//" + loc.host,
525 "Test forced crossDomain is detected as cross-domain",
527 crossDomain: true
530 request(
531 " http://otherdomain.com",
532 "Cross-domain url with leading space is detected as cross-domain"
535 } );
537 ajaxTest( "jQuery.ajax() - abort", 9, function( assert ) {
538 return {
539 setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxError ajaxComplete", assert ),
540 url: url( "mock.php?action=wait&wait=5" ),
541 beforeSend: function() {
542 assert.ok( true, "beforeSend" );
544 afterSend: function( xhr ) {
545 assert.strictEqual( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
546 xhr.abort();
547 assert.strictEqual( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
549 error: true,
550 complete: function() {
551 assert.ok( true, "complete" );
554 } );
556 if ( !/android 4\.0/i.test( navigator.userAgent ) ) {
557 ajaxTest( "jQuery.ajax() - native abort", 2, function( assert ) {
558 return {
559 url: url( "mock.php?action=wait&wait=1" ),
560 xhr: function() {
561 var xhr = new window.XMLHttpRequest();
562 setTimeout( function() {
563 xhr.abort();
564 }, 100 );
565 return xhr;
567 error: function( xhr, msg ) {
568 assert.strictEqual( msg, "error", "Native abort triggers error callback" );
570 complete: function() {
571 assert.ok( true, "complete" );
574 } );
577 // Support: Android <= 4.0 - 4.3 only
578 // Android 4.0-4.3 does not have ontimeout on an xhr
579 if ( "ontimeout" in new window.XMLHttpRequest() ) {
580 ajaxTest( "jQuery.ajax() - native timeout", 2, function( assert ) {
581 return {
582 url: url( "mock.php?action=wait&wait=1" ),
583 xhr: function() {
584 var xhr = new window.XMLHttpRequest();
585 xhr.timeout = 1;
586 return xhr;
588 error: function( xhr, msg ) {
589 assert.strictEqual( msg, "error", "Native timeout triggers error callback" );
591 complete: function() {
592 assert.ok( true, "complete" );
595 } );
598 ajaxTest( "jQuery.ajax() - events with context", 12, function( assert ) {
599 var context = document.createElement( "div" );
601 function event( e ) {
602 assert.equal( this, context, e.type );
605 function callback( msg ) {
606 return function() {
607 assert.equal( this, context, "context is preserved on callback " + msg );
611 return {
612 setup: function() {
613 jQuery( context ).appendTo( "#foo" )
614 .ajaxSend( event )
615 .ajaxComplete( event )
616 .ajaxError( event )
617 .ajaxSuccess( event );
619 requests: [ {
620 url: url( "name.html" ),
621 context: context,
622 beforeSend: callback( "beforeSend" ),
623 success: callback( "success" ),
624 complete: callback( "complete" )
625 }, {
626 url: url( "404.txt" ),
627 context: context,
628 beforeSend: callback( "beforeSend" ),
629 error: callback( "error" ),
630 complete: callback( "complete" )
633 } );
635 ajaxTest( "jQuery.ajax() - events without context", 3, function( assert ) {
636 function nocallback( msg ) {
637 return function() {
638 assert.equal( typeof this.url, "string", "context is settings on callback " + msg );
641 return {
642 url: url( "404.txt" ),
643 beforeSend: nocallback( "beforeSend" ),
644 error: nocallback( "error" ),
645 complete: nocallback( "complete" )
647 } );
649 ajaxTest( "#15118 - jQuery.ajax() - function without jQuery.event", 1, function( assert ) {
650 var holder;
651 return {
652 url: url( "mock.php?action=json" ),
653 setup: function() {
654 holder = jQuery.event;
655 delete jQuery.event;
657 complete: function() {
658 assert.ok( true, "Call can be made without jQuery.event" );
659 jQuery.event = holder;
661 success: true
663 } );
665 ajaxTest( "#15160 - jQuery.ajax() - request manually aborted in ajaxSend", 3, function( assert ) {
666 return {
667 setup: function() {
668 jQuery( document ).on( "ajaxSend", function( e, jqXHR ) {
669 jqXHR.abort();
670 } );
672 jQuery( document ).on( "ajaxError ajaxComplete", function( e, jqXHR ) {
673 assert.equal( jqXHR.statusText, "abort", "jqXHR.statusText equals abort on global ajaxComplete and ajaxError events" );
674 } );
676 url: url( "name.html" ),
677 error: true,
678 complete: function() {
679 assert.ok( true, "complete" );
682 } );
684 ajaxTest( "jQuery.ajax() - context modification", 1, function( assert ) {
685 return {
686 url: url( "name.html" ),
687 context: {},
688 beforeSend: function() {
689 this.test = "foo";
691 afterSend: function() {
692 assert.strictEqual( this.context.test, "foo", "Make sure the original object is maintained." );
694 success: true
696 } );
698 ajaxTest( "jQuery.ajax() - context modification through ajaxSetup", 3, function( assert ) {
699 var obj = {};
700 return {
701 setup: function() {
702 jQuery.ajaxSetup( {
703 context: obj
704 } );
705 assert.strictEqual( jQuery.ajaxSettings.context, obj, "Make sure the context is properly set in ajaxSettings." );
707 requests: [ {
708 url: url( "name.html" ),
709 success: function() {
710 assert.strictEqual( this, obj, "Make sure the original object is maintained." );
712 }, {
713 url: url( "name.html" ),
714 context: {},
715 success: function() {
716 assert.ok( this !== obj, "Make sure overriding context is possible." );
720 } );
722 ajaxTest( "jQuery.ajax() - disabled globals", 3, function( assert ) {
723 return {
724 setup: addGlobalEvents( "", assert ),
725 global: false,
726 url: url( "name.html" ),
727 beforeSend: function() {
728 assert.ok( true, "beforeSend" );
730 success: function() {
731 assert.ok( true, "success" );
733 complete: function() {
734 assert.ok( true, "complete" );
737 } );
739 ajaxTest( "jQuery.ajax() - xml: non-namespace elements inside namespaced elements", 3, function( assert ) {
740 return {
741 url: url( "with_fries.xml" ),
742 dataType: "xml",
743 success: function( resp ) {
744 assert.equal( jQuery( "properties", resp ).length, 1, "properties in responseXML" );
745 assert.equal( jQuery( "jsconf", resp ).length, 1, "jsconf in responseXML" );
746 assert.equal( jQuery( "thing", resp ).length, 2, "things in responseXML" );
749 } );
751 ajaxTest( "jQuery.ajax() - xml: non-namespace elements inside namespaced elements (over JSONP)", 3, function( assert ) {
752 return {
753 url: url( "mock.php?action=xmlOverJsonp" ),
754 dataType: "jsonp xml",
755 success: function( resp ) {
756 assert.equal( jQuery( "properties", resp ).length, 1, "properties in responseXML" );
757 assert.equal( jQuery( "jsconf", resp ).length, 1, "jsconf in responseXML" );
758 assert.equal( jQuery( "thing", resp ).length, 2, "things in responseXML" );
761 } );
763 ajaxTest( "jQuery.ajax() - HEAD requests", 2, function( assert ) {
764 return [
766 url: url( "name.html" ),
767 type: "HEAD",
768 success: function( data, status, xhr ) {
769 assert.ok( /Date/i.test( xhr.getAllResponseHeaders() ), "No Date in HEAD response" );
773 url: url( "name.html" ),
774 data: {
775 "whip_it": "good"
777 type: "HEAD",
778 success: function( data, status, xhr ) {
779 assert.ok( /Date/i.test( xhr.getAllResponseHeaders() ), "No Date in HEAD response with data" );
783 } );
785 ajaxTest( "jQuery.ajax() - beforeSend", 1, function( assert ) {
786 return {
787 url: url( "name.html" ),
788 beforeSend: function() {
789 this.check = true;
791 success: function() {
792 assert.ok( this.check, "check beforeSend was executed" );
795 } );
797 ajaxTest( "jQuery.ajax() - beforeSend, cancel request manually", 2, function( assert ) {
798 return {
799 create: function() {
800 return jQuery.ajax( {
801 url: url( "name.html" ),
802 beforeSend: function( xhr ) {
803 assert.ok( true, "beforeSend got called, canceling" );
804 xhr.abort();
806 success: function() {
807 assert.ok( false, "request didn't get canceled" );
809 complete: function() {
810 assert.ok( false, "request didn't get canceled" );
812 error: function() {
813 assert.ok( false, "request didn't get canceled" );
815 } );
817 fail: function( _, reason ) {
818 assert.strictEqual( reason, "canceled", "canceled request must fail with 'canceled' status text" );
821 } );
823 ajaxTest( "jQuery.ajax() - dataType html", 5, function( assert ) {
824 return {
825 setup: function() {
826 Globals.register( "testFoo" );
827 Globals.register( "testBar" );
829 dataType: "html",
830 url: url( "mock.php?action=testHTML&baseURL=" + baseURL ),
831 success: function( data ) {
832 assert.ok( data.match( /^html text/ ), "Check content for datatype html" );
833 jQuery( "#ap" ).html( data );
834 assert.strictEqual( window[ "testFoo" ], "foo", "Check if script was evaluated for datatype html" );
835 assert.strictEqual( window[ "testBar" ], "bar", "Check if script src was evaluated for datatype html" );
838 } );
840 ajaxTest( "jQuery.ajax() - synchronous request", 1, function( assert ) {
841 return {
842 url: url( "json_obj.js" ),
843 dataType: "text",
844 async: false,
845 success: true,
846 afterSend: function( xhr ) {
847 assert.ok( /^\{ "data"/.test( xhr.responseText ), "check returned text" );
850 } );
852 ajaxTest( "jQuery.ajax() - synchronous request with callbacks", 2, function( assert ) {
853 return {
854 url: url( "json_obj.js" ),
855 async: false,
856 dataType: "text",
857 success: true,
858 afterSend: function( xhr ) {
859 var result;
860 xhr.done( function( data ) {
861 assert.ok( true, "success callback executed" );
862 result = data;
863 } );
864 assert.ok( /^\{ "data"/.test( result ), "check returned text" );
867 } );
869 QUnit.test( "jQuery.ajax(), jQuery.get[Script|JSON](), jQuery.post(), pass-through request object", function( assert ) {
870 assert.expect( 8 );
871 var done = assert.async();
872 var target = "name.html",
873 successCount = 0,
874 errorCount = 0,
875 errorEx = "",
876 success = function() {
877 successCount++;
879 jQuery( document ).on( "ajaxError.passthru", function( e, xml ) {
880 errorCount++;
881 errorEx += ": " + xml.status;
882 } );
883 jQuery( document ).one( "ajaxStop", function() {
884 assert.equal( successCount, 5, "Check all ajax calls successful" );
885 assert.equal( errorCount, 0, "Check no ajax errors (status" + errorEx + ")" );
886 jQuery( document ).off( "ajaxError.passthru" );
887 done();
888 } );
889 Globals.register( "testBar" );
891 assert.ok( jQuery.get( url( target ), success ), "get" );
892 assert.ok( jQuery.post( url( target ), success ), "post" );
893 assert.ok( jQuery.getScript( url( "mock.php?action=testbar" ), success ), "script" );
894 assert.ok( jQuery.getJSON( url( "json_obj.js" ), success ), "json" );
895 assert.ok( jQuery.ajax( {
896 url: url( target ),
897 success: success
898 } ), "generic" );
899 } );
901 ajaxTest( "jQuery.ajax() - cache", 28, function( assert ) {
902 var re = /_=(.*?)(&|$)/g,
903 rootUrl = baseURL + "text.txt";
905 function request( url, title ) {
906 return {
907 url: url,
908 cache: false,
909 beforeSend: function() {
910 var parameter, tmp;
912 // URL sanity check
913 assert.equal( this.url.indexOf( rootUrl ), 0, "root url not mangled: " + this.url );
914 assert.equal( /\&.*\?/.test( this.url ), false, "parameter delimiters in order" );
916 while ( ( tmp = re.exec( this.url ) ) ) {
917 assert.strictEqual( parameter, undefined, title + ": only one 'no-cache' parameter" );
918 parameter = tmp[ 1 ];
919 assert.notStrictEqual( parameter, "tobereplaced555", title + ": parameter (if it was there) was replaced" );
921 return false;
923 error: true
927 return [
928 request(
929 rootUrl,
930 "no query"
932 request(
933 rootUrl + "?",
934 "empty query"
936 request(
937 rootUrl + "?pizza=true",
938 "1 parameter"
940 request(
941 rootUrl + "?_=tobereplaced555",
942 "_= parameter"
944 request(
945 rootUrl + "?pizza=true&_=tobereplaced555",
946 "1 parameter and _="
948 request(
949 rootUrl + "?_=tobereplaced555&tv=false",
950 "_= and 1 parameter"
952 request(
953 rootUrl + "?name=David&_=tobereplaced555&washere=true",
954 "2 parameters surrounding _="
957 } );
959 jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
961 ajaxTest( "jQuery.ajax() - JSONP - Query String (?n)" + label, 4, function( assert ) {
962 return [
964 url: baseURL + "mock.php?action=jsonp&callback=?",
965 dataType: "jsonp",
966 crossDomain: crossDomain,
967 success: function( data ) {
968 assert.ok( data.data, "JSON results returned (GET, url callback)" );
972 url: baseURL + "mock.php?action=jsonp&callback=??",
973 dataType: "jsonp",
974 crossDomain: crossDomain,
975 success: function( data ) {
976 assert.ok( data.data, "JSON results returned (GET, url context-free callback)" );
980 url: baseURL + "mock.php/???action=jsonp",
981 dataType: "jsonp",
982 crossDomain: crossDomain,
983 success: function( data ) {
984 assert.ok( data.data, "JSON results returned (GET, REST-like)" );
988 url: baseURL + "mock.php/???action=jsonp&array=1",
989 dataType: "jsonp",
990 crossDomain: crossDomain,
991 success: function( data ) {
992 assert.ok( Array.isArray( data ), "JSON results returned (GET, REST-like with param)" );
996 } );
998 ajaxTest( "jQuery.ajax() - JSONP - Explicit callback param" + label, 10, function( assert ) {
999 return {
1000 setup: function() {
1001 Globals.register( "functionToCleanUp" );
1002 Globals.register( "XXX" );
1003 Globals.register( "jsonpResults" );
1004 window[ "jsonpResults" ] = function( data ) {
1005 assert.ok( data[ "data" ], "JSON results returned (GET, custom callback function)" );
1008 requests: [ {
1009 url: baseURL + "mock.php?action=jsonp",
1010 dataType: "jsonp",
1011 crossDomain: crossDomain,
1012 jsonp: "callback",
1013 success: function( data ) {
1014 assert.ok( data[ "data" ], "JSON results returned (GET, data obj callback)" );
1016 }, {
1017 url: baseURL + "mock.php?action=jsonp",
1018 dataType: "jsonp",
1019 crossDomain: crossDomain,
1020 jsonpCallback: "jsonpResults",
1021 success: function( data ) {
1022 assert.strictEqual(
1023 typeof window[ "jsonpResults" ],
1024 "function",
1025 "should not rewrite original function"
1027 assert.ok( data.data, "JSON results returned (GET, custom callback name)" );
1029 }, {
1030 url: baseURL + "mock.php?action=jsonp",
1031 dataType: "jsonp",
1032 crossDomain: crossDomain,
1033 jsonpCallback: "functionToCleanUp",
1034 success: function( data ) {
1035 assert.ok( data[ "data" ], "JSON results returned (GET, custom callback name to be cleaned up)" );
1036 assert.strictEqual( window[ "functionToCleanUp" ], true, "Callback was removed (GET, custom callback name to be cleaned up)" );
1037 var xhr;
1038 jQuery.ajax( {
1039 url: baseURL + "mock.php?action=jsonp",
1040 dataType: "jsonp",
1041 crossDomain: crossDomain,
1042 jsonpCallback: "functionToCleanUp",
1043 beforeSend: function( jqXHR ) {
1044 xhr = jqXHR;
1045 return false;
1047 } );
1048 xhr.fail( function() {
1049 assert.ok( true, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1050 assert.strictEqual( window[ "functionToCleanUp" ], true, "Callback was removed after early abort (GET, custom callback name to be cleaned up)" );
1051 } );
1053 }, {
1054 url: baseURL + "mock.php?action=jsonp&callback=XXX",
1055 dataType: "jsonp",
1056 jsonp: false,
1057 jsonpCallback: "XXX",
1058 crossDomain: crossDomain,
1059 beforeSend: function() {
1060 assert.ok( /action=jsonp&callback=XXX&_=\d+$/.test( this.url ), "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
1062 success: function( data ) {
1063 assert.ok( data[ "data" ], "JSON results returned (GET, custom callback name with no url manipulation)" );
1067 } );
1069 ajaxTest( "jQuery.ajax() - JSONP - Callback in data" + label, 2, function( assert ) {
1070 return [
1072 url: baseURL + "mock.php?action=jsonp",
1073 dataType: "jsonp",
1074 crossDomain: crossDomain,
1075 data: "callback=?",
1076 success: function( data ) {
1077 assert.ok( data.data, "JSON results returned (GET, data callback)" );
1081 url: baseURL + "mock.php?action=jsonp",
1082 dataType: "jsonp",
1083 crossDomain: crossDomain,
1084 data: "callback=??",
1085 success: function( data ) {
1086 assert.ok( data.data, "JSON results returned (GET, data context-free callback)" );
1090 } );
1092 ajaxTest( "jQuery.ajax() - JSONP - POST" + label, 3, function( assert ) {
1093 return [
1095 type: "POST",
1096 url: baseURL + "mock.php?action=jsonp",
1097 dataType: "jsonp",
1098 crossDomain: crossDomain,
1099 success: function( data ) {
1100 assert.ok( data[ "data" ], "JSON results returned (POST, no callback)" );
1104 type: "POST",
1105 url: baseURL + "mock.php?action=jsonp",
1106 data: "callback=?",
1107 dataType: "jsonp",
1108 crossDomain: crossDomain,
1109 success: function( data ) {
1110 assert.ok( data[ "data" ], "JSON results returned (POST, data callback)" );
1114 type: "POST",
1115 url: baseURL + "mock.php?action=jsonp",
1116 jsonp: "callback",
1117 dataType: "jsonp",
1118 crossDomain: crossDomain,
1119 success: function( data ) {
1120 assert.ok( data[ "data" ], "JSON results returned (POST, data obj callback)" );
1124 } );
1126 ajaxTest( "jQuery.ajax() - JSONP" + label, 3, function( assert ) {
1127 return [
1129 url: baseURL + "mock.php?action=jsonp",
1130 dataType: "jsonp",
1131 crossDomain: crossDomain,
1132 success: function( data ) {
1133 assert.ok( data.data, "JSON results returned (GET, no callback)" );
1137 create: function( options ) {
1138 var request = jQuery.ajax( options ),
1139 promise = request.then( function( data ) {
1140 assert.ok( data.data, "first request: JSON results returned (GET, no callback)" );
1141 request = jQuery.ajax( this ).done( function( data ) {
1142 assert.ok( data.data, "this re-used: JSON results returned (GET, no callback)" );
1143 } );
1144 promise.abort = request.abort;
1145 return request;
1146 } );
1147 promise.abort = request.abort;
1148 return promise;
1150 url: baseURL + "mock.php?action=jsonp",
1151 dataType: "jsonp",
1152 crossDomain: crossDomain,
1153 success: true
1156 } );
1158 } );
1160 ajaxTest( "jQuery.ajax() - script, Remote", 2, function( assert ) {
1161 return {
1162 setup: function() {
1163 Globals.register( "testBar" );
1165 url: url( "mock.php?action=testbar" ),
1166 dataType: "script",
1167 success: function() {
1168 assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (GET, no callback)" );
1171 } );
1173 ajaxTest( "jQuery.ajax() - script, Remote with POST", 3, function( assert ) {
1174 return {
1175 setup: function() {
1176 Globals.register( "testBar" );
1178 url: url( "mock.php?action=testbar" ),
1179 type: "POST",
1180 dataType: "script",
1181 success: function( data, status ) {
1182 assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (POST, no callback)" );
1183 assert.strictEqual( status, "success", "Script results returned (POST, no callback)" );
1186 } );
1188 ajaxTest( "jQuery.ajax() - script, Remote with scheme-less URL", 2, function( assert ) {
1189 return {
1190 setup: function() {
1191 Globals.register( "testBar" );
1193 url: url( "mock.php?action=testbar" ),
1194 dataType: "script",
1195 success: function() {
1196 assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (GET, no callback)" );
1199 } );
1201 ajaxTest( "jQuery.ajax() - malformed JSON", 2, function( assert ) {
1202 return {
1203 url: baseURL + "badjson.js",
1204 dataType: "json",
1205 error: function( xhr, msg, detailedMsg ) {
1206 assert.strictEqual( msg, "parsererror", "A parse error occurred." );
1207 assert.ok( /(invalid|error|exception)/i.test( detailedMsg ), "Detailed parsererror message provided" );
1210 } );
1212 ajaxTest( "jQuery.ajax() - script by content-type", 2, function() {
1213 return [
1215 url: baseURL + "mock.php?action=script",
1216 data: {
1217 "header": "script"
1219 success: true
1222 url: baseURL + "mock.php?action=script",
1223 data: {
1224 "header": "ecma"
1226 success: true
1229 } );
1231 ajaxTest( "jQuery.ajax() - JSON by content-type", 5, function( assert ) {
1232 return {
1233 url: baseURL + "mock.php?action=json",
1234 data: {
1235 "header": "json",
1236 "array": "1"
1238 success: function( json ) {
1239 assert.ok( json.length >= 2, "Check length" );
1240 assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" );
1241 assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" );
1242 assert.strictEqual( json[ 1 ][ "name" ], "Peter", "Check JSON: second, name" );
1243 assert.strictEqual( json[ 1 ][ "age" ], 25, "Check JSON: second, age" );
1246 } );
1248 ajaxTest( "jQuery.ajax() - JSON by content-type disabled with options", 6, function( assert ) {
1249 return {
1250 url: url( "mock.php?action=json" ),
1251 data: {
1252 "header": "json",
1253 "array": "1"
1255 contents: {
1256 "json": false
1258 success: function( text ) {
1259 assert.strictEqual( typeof text, "string", "json wasn't auto-determined" );
1260 var json = JSON.parse( text );
1261 assert.ok( json.length >= 2, "Check length" );
1262 assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" );
1263 assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" );
1264 assert.strictEqual( json[ 1 ][ "name" ], "Peter", "Check JSON: second, name" );
1265 assert.strictEqual( json[ 1 ][ "age" ], 25, "Check JSON: second, age" );
1268 } );
1270 ajaxTest( "jQuery.ajax() - simple get", 1, function( assert ) {
1271 return {
1272 type: "GET",
1273 url: url( "mock.php?action=name&name=foo" ),
1274 success: function( msg ) {
1275 assert.strictEqual( msg, "bar", "Check for GET" );
1278 } );
1280 ajaxTest( "jQuery.ajax() - simple post", 1, function( assert ) {
1281 return {
1282 type: "POST",
1283 url: url( "mock.php?action=name" ),
1284 data: "name=peter",
1285 success: function( msg ) {
1286 assert.strictEqual( msg, "pan", "Check for POST" );
1289 } );
1291 ajaxTest( "jQuery.ajax() - data option - empty bodies for non-GET requests", 1, function( assert ) {
1292 return {
1293 url: baseURL + "mock.php?action=echoData",
1294 data: undefined,
1295 type: "post",
1296 success: function( result ) {
1297 assert.strictEqual( result, "" );
1300 } );
1302 ajaxTest( "jQuery.ajax() - data - x-www-form-urlencoded (gh-2658)", 1, function( assert ) {
1303 return {
1304 url: "bogus.html",
1305 data: { devo: "A Beautiful World" },
1306 type: "post",
1307 beforeSend: function( _, s ) {
1308 assert.strictEqual( s.data, "devo=A+Beautiful+World", "data is '+'-encoded" );
1309 return false;
1311 error: true
1313 } );
1315 ajaxTest( "jQuery.ajax() - data - text/plain (gh-2658)", 1, function( assert ) {
1316 return {
1317 url: "bogus.html",
1318 data: { devo: "A Beautiful World" },
1319 type: "post",
1320 contentType: "text/plain",
1321 beforeSend: function( _, s ) {
1322 assert.strictEqual( s.data, "devo=A%20Beautiful%20World", "data is %20-encoded" );
1323 return false;
1325 error: true
1327 } );
1329 ajaxTest( "jQuery.ajax() - data - no processing POST", 1, function( assert ) {
1330 return {
1331 url: "bogus.html",
1332 data: { devo: "A Beautiful World" },
1333 type: "post",
1334 contentType: "x-special-sauce",
1335 processData: false,
1336 beforeSend: function( _, s ) {
1337 assert.deepEqual( s.data, { devo: "A Beautiful World" }, "data is not processed" );
1338 return false;
1340 error: true
1342 } );
1344 ajaxTest( "jQuery.ajax() - data - no processing GET", 1, function( assert ) {
1345 return {
1346 url: "bogus.html",
1347 data: { devo: "A Beautiful World" },
1348 type: "get",
1349 contentType: "x-something-else",
1350 processData: false,
1351 beforeSend: function( _, s ) {
1352 assert.deepEqual( s.data, { devo: "A Beautiful World" }, "data is not processed" );
1353 return false;
1355 error: true
1357 } );
1359 ajaxTest( "jQuery.ajax() - data - process string with GET", 2, function( assert ) {
1360 return {
1361 url: "bogus.html",
1362 data: "a=1&b=2",
1363 type: "get",
1364 contentType: "x-something-else",
1365 processData: false,
1366 beforeSend: function( _, s ) {
1367 assert.equal( s.url, "bogus.html?a=1&b=2", "added data to url" );
1368 assert.equal( s.data, undefined, "removed data from settings" );
1369 return false;
1371 error: true
1373 } );
1375 var ifModifiedNow = new Date();
1377 jQuery.each(
1378 /* jQuery.each arguments start */
1380 " (cache)": true,
1381 " (no cache)": false
1383 function( label, cache ) {
1384 jQuery.each(
1386 "If-Modified-Since": "mock.php?action=ims",
1387 "Etag": "mock.php?action=etag"
1389 function( type, url ) {
1390 url = baseURL + url + "&ts=" + ifModifiedNow++;
1391 QUnit.test( "jQuery.ajax() - " + type + " support" + label, function( assert ) {
1392 assert.expect( 4 );
1393 var done = assert.async();
1394 jQuery.ajax( {
1395 url: url,
1396 ifModified: true,
1397 cache: cache,
1398 success: function( _, status ) {
1399 assert.strictEqual( status, "success", "Initial status is 'success'" );
1400 jQuery.ajax( {
1401 url: url,
1402 ifModified: true,
1403 cache: cache,
1404 success: function( data, status, jqXHR ) {
1405 assert.strictEqual( status, "notmodified", "Following status is 'notmodified'" );
1406 assert.strictEqual( jqXHR.status, 304, "XHR status is 304" );
1407 assert.equal( data, null, "no response body is given" );
1409 complete: function() {
1410 done();
1412 } );
1414 } );
1415 } );
1419 /* jQuery.each arguments end */
1422 ajaxTest( "jQuery.ajax() - failing cross-domain (non-existing)", 1, function( assert ) {
1423 return {
1425 // see RFC 2606
1426 url: "http://example.invalid",
1427 error: function( xhr, _, e ) {
1428 assert.ok( true, "file not found: " + xhr.status + " => " + e );
1431 } );
1433 ajaxTest( "jQuery.ajax() - failing cross-domain", 1, function( assert ) {
1434 return {
1435 url: "http://" + externalHost,
1436 error: function( xhr, _, e ) {
1437 assert.ok( true, "access denied: " + xhr.status + " => " + e );
1440 } );
1442 ajaxTest( "jQuery.ajax() - atom+xml", 1, function( assert ) {
1443 return {
1444 url: url( "mock.php?action=atom" ),
1445 success: function() {
1446 assert.ok( true, "success" );
1449 } );
1451 QUnit.test( "jQuery.ajax() - statusText", function( assert ) {
1452 assert.expect( 3 );
1453 var done = assert.async();
1454 jQuery.ajax( url( "mock.php?action=status&code=200&text=Hello" ) ).done( function( _, statusText, jqXHR ) {
1455 assert.strictEqual( statusText, "success", "callback status text ok for success" );
1456 assert.ok( jqXHR.statusText === "Hello" || jqXHR.statusText === "OK", "jqXHR status text ok for success (" + jqXHR.statusText + ")" );
1457 jQuery.ajax( url( "mock.php?action=status&code=404&text=World" ) ).fail( function( jqXHR, statusText ) {
1458 assert.strictEqual( statusText, "error", "callback status text ok for error" );
1459 done();
1460 } );
1461 } );
1462 } );
1464 QUnit.test( "jQuery.ajax() - statusCode", function( assert ) {
1465 assert.expect( 20 );
1466 var done = assert.async(),
1467 count = 12;
1469 function countComplete() {
1470 if ( !--count ) {
1471 done();
1475 function createStatusCodes( name, isSuccess ) {
1476 name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
1477 return {
1478 200: function() {
1479 assert.ok( isSuccess, name );
1481 404: function() {
1482 assert.ok( !isSuccess, name );
1487 jQuery.each(
1488 /* jQuery.each arguments start */
1490 "name.html": true,
1491 "404.txt": false
1493 function( uri, isSuccess ) {
1494 jQuery.ajax( url( uri ), {
1495 statusCode: createStatusCodes( "in options", isSuccess ),
1496 complete: countComplete
1497 } );
1499 jQuery.ajax( url( uri ), {
1500 complete: countComplete
1501 } ).statusCode( createStatusCodes( "immediately with method", isSuccess ) );
1503 jQuery.ajax( url( uri ), {
1504 complete: function( jqXHR ) {
1505 jqXHR.statusCode( createStatusCodes( "on complete", isSuccess ) );
1506 countComplete();
1508 } );
1510 jQuery.ajax( url( uri ), {
1511 complete: function( jqXHR ) {
1512 setTimeout( function() {
1513 jqXHR.statusCode( createStatusCodes( "very late binding", isSuccess ) );
1514 countComplete();
1515 }, 100 );
1517 } );
1519 jQuery.ajax( url( uri ), {
1520 statusCode: createStatusCodes( "all (options)", isSuccess ),
1521 complete: function( jqXHR ) {
1522 jqXHR.statusCode( createStatusCodes( "all (on complete)", isSuccess ) );
1523 setTimeout( function() {
1524 jqXHR.statusCode( createStatusCodes( "all (very late binding)", isSuccess ) );
1525 countComplete();
1526 }, 100 );
1528 } ).statusCode( createStatusCodes( "all (immediately with method)", isSuccess ) );
1530 var testString = "";
1532 jQuery.ajax( url( uri ), {
1533 success: function( a, b, jqXHR ) {
1534 assert.ok( isSuccess, "success" );
1535 var statusCode = {};
1536 statusCode[ jqXHR.status ] = function() {
1537 testString += "B";
1539 jqXHR.statusCode( statusCode );
1540 testString += "A";
1542 error: function( jqXHR ) {
1543 assert.ok( !isSuccess, "error" );
1544 var statusCode = {};
1545 statusCode[ jqXHR.status ] = function() {
1546 testString += "B";
1548 jqXHR.statusCode( statusCode );
1549 testString += "A";
1551 complete: function() {
1552 assert.strictEqual(
1553 testString,
1554 "AB",
1555 "Test statusCode callbacks are ordered like " + ( isSuccess ? "success" : "error" ) + " callbacks"
1557 countComplete();
1559 } );
1562 /* jQuery.each arguments end*/
1564 } );
1566 ajaxTest( "jQuery.ajax() - transitive conversions", 8, function( assert ) {
1567 return [
1569 url: url( "mock.php?action=json" ),
1570 converters: {
1571 "json myJson": function( data ) {
1572 assert.ok( true, "converter called" );
1573 return data;
1576 dataType: "myJson",
1577 success: function() {
1578 assert.ok( true, "Transitive conversion worked" );
1579 assert.strictEqual( this.dataTypes[ 0 ], "text", "response was retrieved as text" );
1580 assert.strictEqual( this.dataTypes[ 1 ], "myjson", "request expected myjson dataType" );
1584 url: url( "mock.php?action=json" ),
1585 converters: {
1586 "json myJson": function( data ) {
1587 assert.ok( true, "converter called (*)" );
1588 return data;
1591 contents: false, /* headers are wrong so we ignore them */
1592 dataType: "* myJson",
1593 success: function() {
1594 assert.ok( true, "Transitive conversion worked (*)" );
1595 assert.strictEqual( this.dataTypes[ 0 ], "text", "response was retrieved as text (*)" );
1596 assert.strictEqual( this.dataTypes[ 1 ], "myjson", "request expected myjson dataType (*)" );
1600 } );
1602 ajaxTest( "jQuery.ajax() - overrideMimeType", 2, function( assert ) {
1603 return [
1605 url: url( "mock.php?action=json" ),
1606 beforeSend: function( xhr ) {
1607 xhr.overrideMimeType( "application/json" );
1609 success: function( json ) {
1610 assert.ok( json.data, "Mimetype overridden using beforeSend" );
1614 url: url( "mock.php?action=json" ),
1615 mimeType: "application/json",
1616 success: function( json ) {
1617 assert.ok( json.data, "Mimetype overridden using mimeType option" );
1621 } );
1623 ajaxTest( "jQuery.ajax() - empty json gets to error callback instead of success callback.", 1, function( assert ) {
1624 return {
1625 url: url( "mock.php?action=echoData" ),
1626 error: function( _, __, error ) {
1627 assert.equal( typeof error === "object", true, "Didn't get back error object for empty json response" );
1629 dataType: "json"
1631 } );
1633 ajaxTest( "#2688 - jQuery.ajax() - beforeSend, cancel request", 2, function( assert ) {
1634 return {
1635 create: function() {
1636 return jQuery.ajax( {
1637 url: url( "name.html" ),
1638 beforeSend: function() {
1639 assert.ok( true, "beforeSend got called, canceling" );
1640 return false;
1642 success: function() {
1643 assert.ok( false, "request didn't get canceled" );
1645 complete: function() {
1646 assert.ok( false, "request didn't get canceled" );
1648 error: function() {
1649 assert.ok( false, "request didn't get canceled" );
1651 } );
1653 fail: function( _, reason ) {
1654 assert.strictEqual( reason, "canceled", "canceled request must fail with 'canceled' status text" );
1657 } );
1659 ajaxTest( "#2806 - jQuery.ajax() - data option - evaluate function values", 1, function( assert ) {
1660 return {
1661 url: baseURL + "mock.php?action=echoQuery",
1662 data: {
1663 key: function() {
1664 return "value";
1667 success: function( result ) {
1668 assert.strictEqual( result, "action=echoQuery&key=value" );
1671 } );
1673 QUnit.test( "#7531 - jQuery.ajax() - Location object as url", function( assert ) {
1674 assert.expect( 1 );
1676 var xhr,
1677 success = false;
1678 try {
1679 xhr = jQuery.ajax( {
1680 url: window.location
1681 } );
1682 success = true;
1683 xhr.abort();
1684 } catch ( e ) {
1687 assert.ok( success, "document.location did not generate exception" );
1688 } );
1690 jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
1691 ajaxTest( "#7578 - jQuery.ajax() - JSONP - default for cache option" + label, 1, function( assert ) {
1692 return {
1693 url: baseURL + "mock.php?action=jsonp",
1694 dataType: "jsonp",
1695 crossDomain: crossDomain,
1696 beforeSend: function() {
1697 assert.strictEqual( this.cache, false, "cache must be false on JSON request" );
1698 return false;
1700 error: true
1702 } );
1703 } );
1705 ajaxTest( "#8107 - jQuery.ajax() - multiple method signatures introduced in 1.5", 4, function( assert ) {
1706 return [
1708 create: function() {
1709 return jQuery.ajax();
1711 done: function() {
1712 assert.ok( true, "With no arguments" );
1716 create: function() {
1717 return jQuery.ajax( baseURL + "name.html" );
1719 done: function() {
1720 assert.ok( true, "With only string URL argument" );
1724 create: function() {
1725 return jQuery.ajax( baseURL + "name.html", {} );
1727 done: function() {
1728 assert.ok( true, "With string URL param and map" );
1732 create: function( options ) {
1733 return jQuery.ajax( options );
1735 url: baseURL + "name.html",
1736 success: function() {
1737 assert.ok( true, "With only map" );
1741 } );
1743 jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
1744 ajaxTest( "#8205 - jQuery.ajax() - JSONP - re-use callbacks name" + label, 4, function( assert ) {
1745 return {
1746 url: baseURL + "mock.php?action=jsonp",
1747 dataType: "jsonp",
1748 crossDomain: crossDomain,
1749 beforeSend: function( jqXHR, s ) {
1750 s.callback = s.jsonpCallback;
1752 assert.ok( this.callback in window, "JSONP callback name is in the window" );
1754 success: function() {
1755 var previous = this;
1757 assert.strictEqual(
1758 previous.jsonpCallback,
1759 undefined,
1760 "jsonpCallback option is set back to default in callbacks"
1763 assert.ok(
1764 !( this.callback in window ),
1765 "JSONP callback name was removed from the window"
1768 jQuery.ajax( {
1769 url: baseURL + "mock.php?action=jsonp",
1770 dataType: "jsonp",
1771 crossDomain: crossDomain,
1772 beforeSend: function() {
1773 assert.strictEqual( this.jsonpCallback, previous.callback, "JSONP callback name is re-used" );
1774 return false;
1776 } );
1779 } );
1780 } );
1782 QUnit.test( "#9887 - jQuery.ajax() - Context with circular references (#9887)", function( assert ) {
1783 assert.expect( 2 );
1785 var success = false,
1786 context = {};
1787 context.field = context;
1788 try {
1789 jQuery.ajax( "non-existing", {
1790 context: context,
1791 beforeSend: function() {
1792 assert.ok( this === context, "context was not deep extended" );
1793 return false;
1795 } );
1796 success = true;
1797 } catch ( e ) {
1798 console.log( e );
1800 assert.ok( success, "context with circular reference did not generate an exception" );
1801 } );
1803 jQuery.each( [ "as argument", "in settings object" ], function( inSetting, title ) {
1805 function request( assert, url, test ) {
1806 return {
1807 create: function() {
1808 return jQuery.ajax( inSetting ? { url: url } : url );
1810 done: function() {
1811 assert.ok( true, ( test || url ) + " " + title );
1816 ajaxTest( "#10093 - jQuery.ajax() - falsy url " + title, 4, function( assert ) {
1817 return [
1818 request( assert, "", "empty string" ),
1819 request( assert, false ),
1820 request( assert, null ),
1821 request( assert, undefined )
1823 } );
1824 } );
1826 ajaxTest( "#11151 - jQuery.ajax() - parse error body", 2, function( assert ) {
1827 return {
1828 url: url( "mock.php?action=error&json=1" ),
1829 dataFilter: function( string ) {
1830 assert.ok( false, "dataFilter called" );
1831 return string;
1833 error: function( jqXHR ) {
1834 assert.strictEqual( jqXHR.responseText, "{ \"code\": 40, \"message\": \"Bad Request\" }", "Error body properly set" );
1835 assert.deepEqual( jqXHR.responseJSON, { code: 40, message: "Bad Request" }, "Error body properly parsed" );
1838 } );
1840 ajaxTest( "#11426 - jQuery.ajax() - loading binary data shouldn't throw an exception in IE", 1, function( assert ) {
1841 return {
1842 url: url( "1x1.jpg" ),
1843 success: function( data ) {
1844 assert.ok( data === undefined || /JFIF/.test( data ), "success callback reached" );
1847 } );
1849 if ( typeof window.ArrayBuffer === "undefined" || typeof new XMLHttpRequest().responseType !== "string" ) {
1851 QUnit.skip( "No ArrayBuffer support in XHR", jQuery.noop );
1852 } else {
1854 // No built-in support for binary data, but it's easy to add via a prefilter
1855 jQuery.ajaxPrefilter( "arraybuffer", function( s ) {
1856 s.xhrFields = { responseType: "arraybuffer" };
1857 s.responseFields.arraybuffer = "response";
1858 s.converters[ "binary arraybuffer" ] = true;
1859 } );
1861 ajaxTest( "gh-2498 - jQuery.ajax() - binary data shouldn't throw an exception", 2, function( assert ) {
1862 return {
1863 url: url( "1x1.jpg" ),
1864 dataType: "arraybuffer",
1865 success: function( data, s, jqxhr ) {
1866 assert.ok( data instanceof window.ArrayBuffer, "correct data type" );
1867 assert.ok( jqxhr.response instanceof window.ArrayBuffer, "data in jQXHR" );
1870 } );
1873 QUnit.test( "#11743 - jQuery.ajax() - script, throws exception", function( assert ) {
1874 assert.expect( 1 );
1875 var done = assert.async();
1876 var onerror = window.onerror;
1877 window.onerror = function() {
1878 assert.ok( true, "Exception thrown" );
1879 window.onerror = onerror;
1880 done();
1882 jQuery.ajax( {
1883 url: baseURL + "badjson.js",
1884 dataType: "script",
1885 throws: true
1886 } );
1887 } );
1889 jQuery.each( [ "method", "type" ], function( _, globalOption ) {
1890 function request( assert, option ) {
1891 var options = {
1892 url: url( "mock.php?action=echoData" ),
1893 data: "hello",
1894 success: function( msg ) {
1895 assert.strictEqual( msg, "hello", "Check for POST (no override)" );
1898 if ( option ) {
1899 options[ option ] = "GET";
1900 options.success = function( msg ) {
1901 assert.strictEqual( msg, "", "Check for no POST (overriding with " + option + ")" );
1904 return options;
1907 ajaxTest(
1908 "#12004 - jQuery.ajax() - method is an alias of type - " +
1909 globalOption + " set globally", 3,
1910 function( assert ) {
1911 return {
1912 setup: function() {
1913 var options = {};
1914 options[ globalOption ] = "POST";
1915 jQuery.ajaxSetup( options );
1917 requests: [
1918 request( assert, "type" ),
1919 request( assert, "method" ),
1920 request( assert )
1925 } );
1927 ajaxTest( "#13276 - jQuery.ajax() - compatibility between XML documents from ajax requests and parsed string", 1, function( assert ) {
1928 return {
1929 url: baseURL + "dashboard.xml",
1930 dataType: "xml",
1931 success: function( ajaxXML ) {
1932 var parsedXML = jQuery( jQuery.parseXML( "<tab title=\"Added\">blibli</tab>" ) ).find( "tab" );
1933 ajaxXML = jQuery( ajaxXML );
1934 try {
1935 ajaxXML.find( "infowindowtab" ).append( parsedXML );
1936 } catch ( e ) {
1937 assert.strictEqual( e, undefined, "error" );
1938 return;
1940 assert.strictEqual( ajaxXML.find( "tab" ).length, 3, "Parsed node was added properly" );
1943 } );
1945 ajaxTest( "#13292 - jQuery.ajax() - converter is bypassed for 204 requests", 3, function( assert ) {
1946 return {
1947 url: baseURL + "mock.php?action=status&code=204&text=No+Content",
1948 dataType: "testing",
1949 converters: {
1950 "* testing": function() {
1951 throw "converter was called";
1954 success: function( data, status, jqXHR ) {
1955 assert.strictEqual( jqXHR.status, 204, "status code is 204" );
1956 assert.strictEqual( status, "nocontent", "status text is 'nocontent'" );
1957 assert.strictEqual( data, undefined, "data is undefined" );
1959 error: function( _, status, error ) {
1960 assert.ok( false, "error" );
1961 assert.strictEqual( status, "parsererror", "Parser Error" );
1962 assert.strictEqual( error, "converter was called", "Converter was called" );
1965 } );
1967 ajaxTest( "#13388 - jQuery.ajax() - responseXML", 3, function( assert ) {
1968 return {
1969 url: url( "with_fries.xml" ),
1970 dataType: "xml",
1971 success: function( resp, _, jqXHR ) {
1972 assert.notStrictEqual( resp, undefined, "XML document exists" );
1973 assert.ok( "responseXML" in jqXHR, "jqXHR.responseXML exists" );
1974 assert.strictEqual( resp, jqXHR.responseXML, "jqXHR.responseXML is set correctly" );
1977 } );
1979 ajaxTest( "#13922 - jQuery.ajax() - converter is bypassed for HEAD requests", 3, function( assert ) {
1980 return {
1981 url: baseURL + "mock.php?action=json",
1982 method: "HEAD",
1983 data: {
1984 header: "yes"
1986 converters: {
1987 "text json": function() {
1988 throw "converter was called";
1991 success: function( data, status ) {
1992 assert.ok( true, "success" );
1993 assert.strictEqual( status, "nocontent", "data is undefined" );
1994 assert.strictEqual( data, undefined, "data is undefined" );
1996 error: function( _, status, error ) {
1997 assert.ok( false, "error" );
1998 assert.strictEqual( status, "parsererror", "Parser Error" );
1999 assert.strictEqual( error, "converter was called", "Converter was called" );
2002 } );
2004 testIframe(
2005 "#14379 - jQuery.ajax() on unload",
2006 "ajax/onunload.html",
2007 function( assert, jQuery, window, document, status ) {
2008 assert.expect( 1 );
2009 assert.strictEqual( status, "success", "Request completed" );
2013 ajaxTest( "#14683 - jQuery.ajax() - Exceptions thrown synchronously by xhr.send should be caught", 4, function( assert ) {
2014 return [ {
2015 url: baseURL + "mock.php?action=echoData",
2016 method: "POST",
2017 data: {
2018 toString: function() {
2019 throw "Can't parse";
2022 processData: false,
2023 done: function( data ) {
2024 assert.ok( false, "done: " + data );
2026 fail: function( jqXHR, status, error ) {
2027 assert.ok( true, "exception caught: " + error );
2028 assert.strictEqual( jqXHR.status, 0, "proper status code" );
2029 assert.strictEqual( status, "error", "proper status" );
2031 }, {
2032 url: "http://" + externalHost + ":80q",
2033 done: function( data ) {
2034 assert.ok( false, "done: " + data );
2036 fail: function( _, status, error ) {
2037 assert.ok( true, "fail: " + status + " - " + error );
2039 } ];
2040 } );
2042 ajaxTest( "gh-2587 - when content-type not xml, but looks like one", 1, function( assert ) {
2043 return {
2044 url: url( "mock.php?action=contentType" ),
2045 data: {
2046 contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
2047 "response": "<test/>"
2049 success: function( result ) {
2050 assert.strictEqual(
2051 typeof result,
2052 "string",
2053 "Should handle it as a string, not xml"
2057 } );
2059 ajaxTest( "gh-2587 - when content-type not xml, but looks like one", 1, function( assert ) {
2060 return {
2061 url: url( "mock.php?action=contentType" ),
2062 data: {
2063 contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
2064 "response": "<test/>"
2066 success: function( result ) {
2067 assert.strictEqual(
2068 typeof result,
2069 "string",
2070 "Should handle it as a string, not xml"
2074 } );
2076 ajaxTest( "gh-2587 - when content-type not json, but looks like one", 1, function( assert ) {
2077 return {
2078 url: url( "mock.php?action=contentType" ),
2079 data: {
2080 contentType: "test/jsontest",
2081 "response": JSON.stringify( { test: "test" } )
2083 success: function( result ) {
2084 assert.strictEqual(
2085 typeof result,
2086 "string",
2087 "Should handle it as a string, not json"
2091 } );
2093 ajaxTest( "gh-2587 - when content-type not html, but looks like one", 1, function( assert ) {
2094 return {
2095 url: url( "mock.php?action=contentType" ),
2096 data: {
2097 contentType: "test/htmltest",
2098 "response": "<p>test</p>"
2100 success: function( result ) {
2101 assert.strictEqual(
2102 typeof result,
2103 "string",
2104 "Should handle it as a string, not html"
2108 } );
2110 ajaxTest( "gh-2587 - when content-type not javascript, but looks like one", 1, function( assert ) {
2111 return {
2112 url: url( "mock.php?action=contentType" ),
2113 data: {
2114 contentType: "test/testjavascript",
2115 "response": "alert(1)"
2117 success: function( result ) {
2118 assert.strictEqual(
2119 typeof result,
2120 "string",
2121 "Should handle it as a string, not javascript"
2125 } );
2127 ajaxTest( "gh-2587 - when content-type not ecmascript, but looks like one", 1, function( assert ) {
2128 return {
2129 url: url( "mock.php?action=contentType" ),
2130 data: {
2131 contentType: "test/testjavascript",
2132 "response": "alert(1)"
2134 success: function( result ) {
2135 assert.strictEqual(
2136 typeof result,
2137 "string",
2138 "Should handle it as a string, not ecmascript"
2142 } );
2144 //----------- jQuery.ajaxPrefilter()
2146 ajaxTest( "jQuery.ajaxPrefilter() - abort", 1, function( assert ) {
2147 return {
2148 dataType: "prefix",
2149 setup: function() {
2151 // Ensure prefix does not throw an error
2152 jQuery.ajaxPrefilter( "+prefix", function( options, _, jqXHR ) {
2153 if ( options.abortInPrefilter ) {
2154 jqXHR.abort();
2156 } );
2158 abortInPrefilter: true,
2159 error: function() {
2160 assert.ok( false, "error callback called" );
2162 fail: function( _, reason ) {
2163 assert.strictEqual( reason, "canceled", "Request aborted by the prefilter must fail with 'canceled' status text" );
2166 } );
2168 //----------- jQuery.ajaxSetup()
2170 QUnit.test( "jQuery.ajaxSetup()", function( assert ) {
2171 assert.expect( 1 );
2172 var done = assert.async();
2173 jQuery.ajaxSetup( {
2174 url: url( "mock.php?action=name&name=foo" ),
2175 success: function( msg ) {
2176 assert.strictEqual( msg, "bar", "Check for GET" );
2177 done();
2179 } );
2180 jQuery.ajax();
2181 } );
2183 QUnit.test( "jQuery.ajaxSetup({ timeout: Number }) - with global timeout", function( assert ) {
2184 assert.expect( 2 );
2185 var done = assert.async();
2186 var passed = 0,
2187 pass = function() {
2188 assert.ok( passed++ < 2, "Error callback executed" );
2189 if ( passed === 2 ) {
2190 jQuery( document ).off( "ajaxError.setupTest" );
2191 done();
2194 fail = function( a, b ) {
2195 assert.ok( false, "Check for timeout failed " + a + " " + b );
2196 done();
2199 jQuery( document ).on( "ajaxError.setupTest", pass );
2201 jQuery.ajaxSetup( {
2202 timeout: 1000
2203 } );
2205 jQuery.ajax( {
2206 type: "GET",
2207 url: url( "mock.php?action=wait&wait=5" ),
2208 error: pass,
2209 success: fail
2210 } );
2211 } );
2213 QUnit.test( "jQuery.ajaxSetup({ timeout: Number }) with localtimeout", function( assert ) {
2214 assert.expect( 1 );
2215 var done = assert.async();
2216 jQuery.ajaxSetup( {
2217 timeout: 50
2218 } );
2219 jQuery.ajax( {
2220 type: "GET",
2221 timeout: 15000,
2222 url: url( "mock.php?action=wait&wait=1" ),
2223 error: function() {
2224 assert.ok( false, "Check for local timeout failed" );
2225 done();
2227 success: function() {
2228 assert.ok( true, "Check for local timeout" );
2229 done();
2231 } );
2232 } );
2234 //----------- jQuery.domManip()
2236 QUnit.test( "#11264 - jQuery.domManip() - no side effect because of ajaxSetup or global events", function( assert ) {
2237 assert.expect( 1 );
2239 jQuery.ajaxSetup( {
2240 type: "POST"
2241 } );
2243 jQuery( document ).on( "ajaxStart ajaxStop", function() {
2244 assert.ok( false, "Global event triggered" );
2245 } );
2247 jQuery( "#qunit-fixture" ).append( "<script src='" + baseURL + "mock.php?action=script'></script>" );
2249 jQuery( document ).off( "ajaxStart ajaxStop" );
2250 } );
2252 QUnit.test(
2253 "jQuery#load() - always use GET method even if it overrided through ajaxSetup (#11264)",
2254 function( assert ) {
2255 assert.expect( 1 );
2256 var done = assert.async();
2258 jQuery.ajaxSetup( {
2259 type: "POST"
2260 } );
2262 jQuery( "#qunit-fixture" ).load( baseURL + "mock.php?action=echoMethod", function( method ) {
2263 assert.equal( method, "GET" );
2264 done();
2265 } );
2269 QUnit.test(
2270 "jQuery#load() - should resolve with correct context",
2271 function( assert ) {
2272 assert.expect( 2 );
2273 var done = assert.async();
2274 var ps = jQuery( "<p></p><p></p>" );
2275 var i = 0;
2277 ps.appendTo( "#qunit-fixture" );
2279 ps.load( baseURL + "mock.php?action=echoMethod", function() {
2280 assert.strictEqual( this, ps[ i++ ] );
2282 if ( i === 2 ) {
2283 done();
2285 } );
2289 QUnit.test(
2290 "#11402 - jQuery.domManip() - script in comments are properly evaluated",
2291 function( assert ) {
2292 assert.expect( 2 );
2293 jQuery( "#qunit-fixture" ).load( baseURL + "cleanScript.html", assert.async() );
2297 //----------- jQuery.get()
2299 QUnit.test( "jQuery.get( String, Hash, Function ) - parse xml and use text() on nodes", function( assert ) {
2300 assert.expect( 2 );
2301 var done = assert.async();
2302 jQuery.get( url( "dashboard.xml" ), function( xml ) {
2303 var content = [];
2304 jQuery( "tab", xml ).each( function() {
2305 content.push( jQuery( this ).text() );
2306 } );
2307 assert.strictEqual( content[ 0 ], "blabla", "Check first tab" );
2308 assert.strictEqual( content[ 1 ], "blublu", "Check second tab" );
2309 done();
2310 } );
2311 } );
2313 QUnit.test( "#8277 - jQuery.get( String, Function ) - data in ajaxSettings", function( assert ) {
2314 assert.expect( 1 );
2315 var done = assert.async();
2316 jQuery.ajaxSetup( {
2317 data: "helloworld"
2318 } );
2319 jQuery.get( url( "mock.php?action=echoQuery" ), function( data ) {
2320 assert.ok( /helloworld$/.test( data ), "Data from ajaxSettings was used" );
2321 done();
2322 } );
2323 } );
2325 //----------- jQuery.getJSON()
2327 QUnit.test( "jQuery.getJSON( String, Hash, Function ) - JSON array", function( assert ) {
2328 assert.expect( 5 );
2329 var done = assert.async();
2330 jQuery.getJSON(
2331 url( "mock.php?action=json" ),
2333 "array": "1"
2335 function( json ) {
2336 assert.ok( json.length >= 2, "Check length" );
2337 assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" );
2338 assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" );
2339 assert.strictEqual( json[ 1 ][ "name" ], "Peter", "Check JSON: second, name" );
2340 assert.strictEqual( json[ 1 ][ "age" ], 25, "Check JSON: second, age" );
2341 done();
2344 } );
2346 QUnit.test( "jQuery.getJSON( String, Function ) - JSON object", function( assert ) {
2347 assert.expect( 2 );
2348 var done = assert.async();
2349 jQuery.getJSON( url( "mock.php?action=json" ), function( json ) {
2350 if ( json && json[ "data" ] ) {
2351 assert.strictEqual( json[ "data" ][ "lang" ], "en", "Check JSON: lang" );
2352 assert.strictEqual( json[ "data" ].length, 25, "Check JSON: length" );
2353 done();
2355 } );
2356 } );
2358 QUnit.test( "jQuery.getJSON( String, Function ) - JSON object with absolute url to local content", function( assert ) {
2359 assert.expect( 2 );
2360 var done = assert.async();
2361 var absoluteUrl = url( "mock.php?action=json" );
2363 // Make a relative URL absolute relative to the document location
2364 if ( !/^[a-z][a-z0-9+.-]*:/i.test( absoluteUrl ) ) {
2366 // An absolute path replaces everything after the host
2367 if ( absoluteUrl.charAt( 0 ) === "/" ) {
2368 absoluteUrl = window.location.href.replace( /(:\/*[^/]*).*$/, "$1" ) + absoluteUrl;
2370 // A relative path replaces the last slash-separated path segment
2371 } else {
2372 absoluteUrl = window.location.href.replace( /[^/]*$/, "" ) + absoluteUrl;
2376 jQuery.getJSON( absoluteUrl, function( json ) {
2377 assert.strictEqual( json.data.lang, "en", "Check JSON: lang" );
2378 assert.strictEqual( json.data.length, 25, "Check JSON: length" );
2379 done();
2380 } );
2381 } );
2383 //----------- jQuery.getScript()
2385 QUnit.test( "jQuery.getScript( String, Function ) - with callback",
2386 function( assert ) {
2387 assert.expect( 2 );
2388 var done = assert.async();
2390 Globals.register( "testBar" );
2391 jQuery.getScript( url( "mock.php?action=testbar" ), function() {
2392 assert.strictEqual( window[ "testBar" ], "bar", "Check if script was evaluated" );
2393 done();
2394 } );
2398 QUnit.test( "jQuery.getScript( String, Function ) - no callback", function( assert ) {
2399 assert.expect( 1 );
2400 Globals.register( "testBar" );
2401 jQuery.getScript( url( "mock.php?action=testbar" ) ).done( assert.async() );
2402 } );
2404 QUnit.test( "#8082 - jQuery.getScript( String, Function ) - source as responseText", function( assert ) {
2405 assert.expect( 2 );
2406 var done = assert.async();
2408 Globals.register( "testBar" );
2409 jQuery.getScript( url( "mock.php?action=testbar" ), function( data, _, jqXHR ) {
2410 assert.strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script" );
2411 done();
2412 } );
2413 } );
2415 QUnit.test( "jQuery.getScript( Object ) - with callback", function( assert ) {
2416 assert.expect( 2 );
2417 var done = assert.async();
2419 Globals.register( "testBar" );
2420 jQuery.getScript( {
2421 url: url( "mock.php?action=testbar" ),
2422 success: function() {
2423 assert.strictEqual( window[ "testBar" ], "bar", "Check if script was evaluated" );
2424 done();
2426 } );
2427 } );
2429 QUnit.test( "jQuery.getScript( Object ) - no callback", function( assert ) {
2430 assert.expect( 1 );
2431 Globals.register( "testBar" );
2432 jQuery.getScript( { url: url( "mock.php?action=testbar" ) } ).done( assert.async() );
2433 } );
2435 // //----------- jQuery.fn.load()
2437 // check if load can be called with only url
2438 QUnit.test( "jQuery.fn.load( String )", function( assert ) {
2439 assert.expect( 2 );
2440 jQuery.ajaxSetup( {
2441 beforeSend: function() {
2442 assert.strictEqual( this.type, "GET", "no data means GET request" );
2444 } );
2445 jQuery( "#first" ).load( baseURL + "name.html", assert.async() );
2446 } );
2448 QUnit.test( "jQuery.fn.load() - 404 error callbacks", function( assert ) {
2449 assert.expect( 6 );
2450 var done = assert.async();
2452 addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError", assert )();
2453 jQuery( document ).ajaxStop( done );
2454 jQuery( "<div/>" ).load( baseURL + "404.txt", function() {
2455 assert.ok( true, "complete" );
2456 } );
2457 } );
2459 // check if load can be called with url and null data
2460 QUnit.test( "jQuery.fn.load( String, null )", function( assert ) {
2461 assert.expect( 2 );
2462 jQuery.ajaxSetup( {
2463 beforeSend: function() {
2464 assert.strictEqual( this.type, "GET", "no data means GET request" );
2466 } );
2467 jQuery( "#first" ).load( baseURL + "name.html", null, assert.async() );
2468 } );
2470 // check if load can be called with url and undefined data
2471 QUnit.test( "jQuery.fn.load( String, undefined )", function( assert ) {
2472 assert.expect( 2 );
2473 jQuery.ajaxSetup( {
2474 beforeSend: function() {
2475 assert.strictEqual( this.type, "GET", "no data means GET request" );
2477 } );
2478 jQuery( "#first" ).load( baseURL + "name.html", undefined, assert.async() );
2479 } );
2481 // check if load can be called with only url
2482 QUnit.test( "jQuery.fn.load( URL_SELECTOR )", function( assert ) {
2483 assert.expect( 1 );
2484 var done = assert.async();
2485 jQuery( "#first" ).load( baseURL + "test3.html div.user", function() {
2486 assert.strictEqual( jQuery( this ).children( "div" ).length, 2, "Verify that specific elements were injected" );
2487 done();
2488 } );
2489 } );
2491 // Selector should be trimmed to avoid leading spaces (#14773)
2492 QUnit.test( "jQuery.fn.load( URL_SELECTOR with spaces )", function( assert ) {
2493 assert.expect( 1 );
2494 var done = assert.async();
2495 jQuery( "#first" ).load( baseURL + "test3.html #superuser ", function() {
2496 assert.strictEqual( jQuery( this ).children( "div" ).length, 1, "Verify that specific elements were injected" );
2497 done();
2498 } );
2499 } );
2501 // Selector should be trimmed to avoid leading spaces (#14773)
2502 // Selector should include any valid non-HTML whitespace (#3003)
2503 QUnit.test( "jQuery.fn.load( URL_SELECTOR with non-HTML whitespace(#3003) )", function( assert ) {
2504 assert.expect( 1 );
2505 var done = assert.async();
2506 jQuery( "#first" ).load( baseURL + "test3.html #whitespace\\\\xA0 ", function() {
2507 assert.strictEqual( jQuery( this ).children( "div" ).length, 1, "Verify that specific elements were injected" );
2508 done();
2509 } );
2510 } );
2512 QUnit.test( "jQuery.fn.load( String, Function ) - simple: inject text into DOM", function( assert ) {
2513 assert.expect( 2 );
2514 var done = assert.async();
2515 jQuery( "#first" ).load( url( "name.html" ), function() {
2516 assert.ok( /^ERROR/.test( jQuery( "#first" ).text() ), "Check if content was injected into the DOM" );
2517 done();
2518 } );
2519 } );
2521 QUnit.test( "jQuery.fn.load( String, Function ) - check scripts", function( assert ) {
2522 assert.expect( 7 );
2523 var done = assert.async();
2524 var verifyEvaluation = function() {
2525 assert.strictEqual( window[ "testBar" ], "bar", "Check if script src was evaluated after load" );
2526 assert.strictEqual( jQuery( "#ap" ).html(), "bar", "Check if script evaluation has modified DOM" );
2527 done();
2530 Globals.register( "testFoo" );
2531 Globals.register( "testBar" );
2533 jQuery( "#first" ).load( url( "mock.php?action=testHTML&baseURL=" + baseURL ), function() {
2534 assert.ok( jQuery( "#first" ).html().match( /^html text/ ), "Check content after loading html" );
2535 assert.strictEqual( jQuery( "#foo" ).html(), "foo", "Check if script evaluation has modified DOM" );
2536 assert.strictEqual( window[ "testFoo" ], "foo", "Check if script was evaluated after load" );
2537 setTimeout( verifyEvaluation, 600 );
2538 } );
2539 } );
2541 QUnit.test( "jQuery.fn.load( String, Function ) - check file with only a script tag", function( assert ) {
2542 assert.expect( 3 );
2543 var done = assert.async();
2544 Globals.register( "testFoo" );
2546 jQuery( "#first" ).load( url( "test2.html" ), function() {
2547 assert.strictEqual( jQuery( "#foo" ).html(), "foo", "Check if script evaluation has modified DOM" );
2548 assert.strictEqual( window[ "testFoo" ], "foo", "Check if script was evaluated after load" );
2549 done();
2550 } );
2551 } );
2553 QUnit.test( "jQuery.fn.load( String, Function ) - dataFilter in ajaxSettings", function( assert ) {
2554 assert.expect( 2 );
2555 var done = assert.async();
2556 jQuery.ajaxSetup( {
2557 dataFilter: function() {
2558 return "Hello World";
2560 } );
2561 jQuery( "<div/>" ).load( url( "name.html" ), function( responseText ) {
2562 assert.strictEqual( jQuery( this ).html(), "Hello World", "Test div was filled with filtered data" );
2563 assert.strictEqual( responseText, "Hello World", "Test callback receives filtered data" );
2564 done();
2565 } );
2566 } );
2568 QUnit.test( "jQuery.fn.load( String, Object, Function )", function( assert ) {
2569 assert.expect( 2 );
2570 var done = assert.async();
2571 jQuery( "<div />" ).load( url( "mock.php?action=echoHtml" ), {
2572 "bar": "ok"
2573 }, function() {
2574 var $node = jQuery( this );
2575 assert.strictEqual( $node.find( "#method" ).text(), "POST", "Check method" );
2576 assert.strictEqual( $node.find( "#data" ).text(), "bar=ok", "Check if data is passed correctly" );
2577 done();
2578 } );
2579 } );
2581 QUnit.test( "jQuery.fn.load( String, String, Function )", function( assert ) {
2582 assert.expect( 2 );
2583 var done = assert.async();
2585 jQuery( "<div />" ).load( url( "mock.php?action=echoHtml" ), "foo=3&bar=ok", function() {
2586 var $node = jQuery( this );
2587 assert.strictEqual( $node.find( "#method" ).text(), "GET", "Check method" );
2588 assert.ok( $node.find( "#query" ).text().match( /foo=3&bar=ok/ ), "Check if a string of data is passed correctly" );
2589 done();
2590 } );
2591 } );
2593 QUnit.test( "jQuery.fn.load() - callbacks get the correct parameters", function( assert ) {
2594 assert.expect( 8 );
2595 var completeArgs = {},
2596 done = assert.async();
2598 jQuery.ajaxSetup( {
2599 success: function( _, status, jqXHR ) {
2600 completeArgs[ this.url ] = [ jqXHR.responseText, status, jqXHR ];
2602 error: function( jqXHR, status ) {
2603 completeArgs[ this.url ] = [ jqXHR.responseText, status, jqXHR ];
2605 } );
2607 jQuery.when.apply(
2608 jQuery,
2609 jQuery.map( [
2611 type: "success",
2612 url: baseURL + "mock.php?action=echoQuery&arg=pop"
2615 type: "error",
2616 url: baseURL + "404.txt"
2619 function( options ) {
2620 return jQuery.Deferred( function( defer ) {
2621 jQuery( "#foo" ).load( options.url, function() {
2622 var args = arguments;
2623 assert.strictEqual( completeArgs[ options.url ].length, args.length, "same number of arguments (" + options.type + ")" );
2624 jQuery.each( completeArgs[ options.url ], function( i, value ) {
2625 assert.strictEqual( args[ i ], value, "argument #" + i + " is the same (" + options.type + ")" );
2626 } );
2627 defer.resolve();
2628 } );
2629 } );
2631 ).always( done );
2632 } );
2634 QUnit.test( "#2046 - jQuery.fn.load( String, Function ) with ajaxSetup on dataType json", function( assert ) {
2635 assert.expect( 1 );
2636 var done = assert.async();
2638 jQuery.ajaxSetup( {
2639 dataType: "json"
2640 } );
2641 jQuery( document ).ajaxComplete( function( e, xml, s ) {
2642 assert.strictEqual( s.dataType, "html", "Verify the load() dataType was html" );
2643 jQuery( document ).off( "ajaxComplete" );
2644 done();
2645 } );
2646 jQuery( "#first" ).load( baseURL + "test3.html" );
2647 } );
2649 QUnit.test( "#10524 - jQuery.fn.load() - data specified in ajaxSettings is merged in", function( assert ) {
2650 assert.expect( 1 );
2651 var done = assert.async();
2653 var data = {
2654 "baz": 1
2656 jQuery.ajaxSetup( {
2657 data: {
2658 "foo": "bar"
2660 } );
2661 jQuery( "#foo" ).load( baseURL + "mock.php?action=echoQuery", data );
2662 jQuery( document ).ajaxComplete( function( event, jqXHR, options ) {
2663 assert.ok( ~options.data.indexOf( "foo=bar" ), "Data from ajaxSettings was used" );
2664 done();
2665 } );
2666 } );
2668 // //----------- jQuery.post()
2670 QUnit.test( "jQuery.post() - data", function( assert ) {
2671 assert.expect( 3 );
2672 var done = assert.async();
2674 jQuery.when(
2675 jQuery.post(
2676 url( "mock.php?action=xml" ),
2678 cal: "5-2"
2680 function( xml ) {
2681 jQuery( "math", xml ).each( function() {
2682 assert.strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
2683 assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
2684 } );
2687 jQuery.ajax( {
2688 url: url( "mock.php?action=echoData" ),
2689 type: "POST",
2690 data: {
2691 "test": {
2692 "length": 7,
2693 "foo": "bar"
2696 success: function( data ) {
2697 assert.strictEqual( data, "test%5Blength%5D=7&test%5Bfoo%5D=bar", "Check if a sub-object with a length param is serialized correctly" );
2700 ).always( done );
2701 } );
2703 QUnit.test( "jQuery.post( String, Hash, Function ) - simple with xml", function( assert ) {
2704 assert.expect( 4 );
2705 var done = assert.async();
2707 jQuery.when(
2708 jQuery.post(
2709 url( "mock.php?action=xml" ),
2711 cal: "5-2"
2713 function( xml ) {
2714 jQuery( "math", xml ).each( function() {
2715 assert.strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
2716 assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
2717 } );
2720 jQuery.post( url( "mock.php?action=xml&cal=5-2" ), {}, function( xml ) {
2721 jQuery( "math", xml ).each( function() {
2722 assert.strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
2723 assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
2724 } );
2726 ).always( function() {
2727 done();
2728 } );
2729 } );
2731 QUnit.test( "jQuery[get|post]( options ) - simple with xml", function( assert ) {
2732 assert.expect( 2 );
2733 var done = assert.async();
2735 jQuery.when.apply( jQuery,
2736 jQuery.map( [ "get", "post" ], function( method ) {
2737 return jQuery[ method ]( {
2738 url: url( "mock.php?action=xml" ),
2739 data: {
2740 cal: "5-2"
2742 success: function( xml ) {
2743 jQuery( "math", xml ).each( function() {
2744 assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
2745 } );
2747 } );
2749 ).always( function() {
2750 done();
2751 } );
2752 } );
2754 //----------- jQuery.active
2756 QUnit.test( "jQuery.active", function( assert ) {
2757 assert.expect( 1 );
2758 assert.ok( jQuery.active === 0, "ajax active counter should be zero: " + jQuery.active );
2759 } );
2761 } )();