1 QUnit.module( "ajax", {
2 afterEach: function() {
3 jQuery( document ).off( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError ajaxSuccess" );
4 moduleTeardown.apply( this, arguments );
9 QUnit.test( "Unit Testing Environment", function( assert ) {
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!)" );
16 if ( !jQuery.ajax || ( isLocal && !hasPHP ) ) {
20 function addGlobalEvents( expected, assert ) {
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 );
29 //----------- jQuery.ajax()
32 "XMLHttpRequest - Attempt to block tests because of dangling XHR requests (IE)",
33 "ajax/unreleasedXHR.html",
36 assert.ok( true, "done" );
40 ajaxTest( "jQuery.ajax() - success callbacks", 8, function( assert ) {
42 setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess", assert ),
43 url: url( "name.html" ),
44 beforeSend: function() {
45 assert.ok( true, "beforeSend" );
48 assert.ok( true, "success" );
50 complete: function() {
51 assert.ok( true, "complete" );
56 ajaxTest( "jQuery.ajax() - success callbacks - (url, options) syntax", 8, function( assert ) {
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" );
66 assert.ok( true, "success" );
68 complete: function() {
69 assert.ok( true, "complete" );
74 ajaxTest( "jQuery.ajax() - custom attributes for script tag", 5,
77 create: function( options ) {
79 options.method = "POST";
80 options.dataType = "script";
81 options.scriptAttrs = { id: "jquery-ajax-test", async: "async" };
82 xhr = jQuery.ajax( url( "mock.php?action=script" ), options );
83 assert.equal( jQuery( "#jquery-ajax-test" ).attr( "async" ), "async", "attr value" );
86 beforeSend: function( _jqXhr, settings ) {
87 assert.strictEqual( settings.type, "GET", "Type changed to GET" );
90 assert.ok( true, "success" );
92 complete: function() {
93 assert.ok( true, "complete" );
99 ajaxTest( "jQuery.ajax() - execute JS when dataType option is provided", 3,
102 create: function( options ) {
103 options.crossDomain = true;
104 options.dataType = "script";
105 return jQuery.ajax( url( "mock.php?action=script&header=ecma" ), options );
107 success: function() {
108 assert.ok( true, "success" );
110 complete: function() {
111 assert.ok( true, "complete" );
117 jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
118 ajaxTest( "jQuery.ajax() - do not execute JS (gh-2432, gh-4822) " + label, 1, function( assert ) {
120 url: url( "mock.php?action=script&header" ),
121 crossDomain: crossDomain,
122 success: function() {
123 assert.ok( true, "success" );
129 ajaxTest( "jQuery.ajax() - success callbacks (late binding)", 8, function( assert ) {
131 setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess", assert ),
132 url: url( "name.html" ),
133 beforeSend: function() {
134 assert.ok( true, "beforeSend" );
137 afterSend: function( request ) {
138 request.always( function() {
139 assert.ok( true, "complete" );
140 } ).done( function() {
141 assert.ok( true, "success" );
142 } ).fail( function() {
143 assert.ok( false, "error" );
149 ajaxTest( "jQuery.ajax() - success callbacks (oncomplete binding)", 8, function( assert ) {
151 setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess", assert ),
152 url: url( "name.html" ),
153 beforeSend: function() {
154 assert.ok( true, "beforeSend" );
157 complete: function( xhr ) {
158 xhr.always( function() {
159 assert.ok( true, "complete" );
160 } ).done( function() {
161 assert.ok( true, "success" );
162 } ).fail( function() {
163 assert.ok( false, "error" );
169 ajaxTest( "jQuery.ajax() - error callbacks", 8, function( assert ) {
171 setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError", assert ),
172 url: url( "mock.php?action=wait&wait=5" ),
173 beforeSend: function() {
174 assert.ok( true, "beforeSend" );
176 afterSend: function( request ) {
180 assert.ok( true, "error" );
182 complete: function() {
183 assert.ok( true, "complete" );
188 ajaxTest( "jQuery.ajax() - textStatus and errorThrown values", 4, function( assert ) {
190 url: url( "mock.php?action=wait&wait=5" ),
191 error: function( _, textStatus, errorThrown ) {
192 assert.strictEqual( textStatus, "abort", "textStatus is 'abort' for abort" );
193 assert.strictEqual( errorThrown, "abort", "errorThrown is 'abort' for abort" );
195 afterSend: function( request ) {
200 url: url( "mock.php?action=wait&wait=5" ),
201 error: function( _, textStatus, errorThrown ) {
202 assert.strictEqual( textStatus, "mystatus", "textStatus is 'mystatus' for abort('mystatus')" );
203 assert.strictEqual( errorThrown, "mystatus", "errorThrown is 'mystatus' for abort('mystatus')" );
205 afterSend: function( request ) {
206 request.abort( "mystatus" );
211 ajaxTest( "jQuery.ajax() - responseText on error", 1, function( assert ) {
213 url: url( "mock.php?action=error" ),
214 error: function( xhr ) {
215 assert.strictEqual( xhr.responseText, "plain text message", "Test jqXHR.responseText is filled for HTTP errors" );
220 QUnit.test( "jQuery.ajax() - retry with jQuery.ajax( this )", function( assert ) {
224 done = assert.async();
226 url: url( "mock.php?action=error" ),
232 assert.ok( true, "Test retrying with jQuery.ajax(this) works" );
234 url: url( "mock.php?action=error&x=2" ),
235 beforeSend: function() {
236 if ( !previousUrl ) {
237 previousUrl = this.url;
239 assert.strictEqual( this.url, previousUrl, "url parameters are not re-appended" );
253 ajaxTest( "jQuery.ajax() - headers", 8, function( assert ) {
256 jQuery( document ).on( "ajaxSend", function( evt, xhr ) {
257 xhr.setRequestHeader( "ajax-send", "test" );
260 url: url( "mock.php?action=headers&keys=siMPle|SometHing-elsE|OthEr|Nullable|undefined|Empty|ajax-send" ),
261 headers: supportjQuery.extend( {
263 "SometHing-elsE": "other value",
264 "OthEr": "something else",
266 "undefined": undefined
268 // Support: IE 9 - 11+
269 // IE can receive empty headers but not send them.
270 }, QUnit.isIE ? {} : {
273 success: function( data, _, xhr ) {
275 requestHeaders = jQuery.extend( this.headers, {
279 for ( i in requestHeaders ) {
280 tmp.push( i, ": ", requestHeaders[ i ] + "", "\n" );
282 tmp = tmp.join( "" );
284 assert.strictEqual( data, tmp, "Headers were sent" );
285 assert.strictEqual( xhr.getResponseHeader( "Sample-Header" ), "Hello World", "Sample header received" );
286 assert.ok( data.indexOf( "undefined" ) < 0, "Undefined header value was not sent" );
287 assert.strictEqual( xhr.getResponseHeader( "Empty-Header" ), "", "Empty header received" );
288 assert.strictEqual( xhr.getResponseHeader( "Sample-Header2" ), "Hello World 2", "Second sample header received" );
289 assert.strictEqual( xhr.getResponseHeader( "List-Header" ), "Item 1, Item 2", "List header received" );
290 assert.strictEqual( xhr.getResponseHeader( "constructor" ), "prototype collision (constructor)", "constructor header received" );
291 assert.strictEqual( xhr.getResponseHeader( "__proto__" ), null, "Undefined __proto__ header not received" );
296 ajaxTest( "jQuery.ajax() - Accept header", 1, function( assert ) {
298 url: url( "mock.php?action=headers&keys=accept" ),
300 Accept: "very wrong accept value"
302 beforeSend: function( xhr ) {
303 xhr.setRequestHeader( "Accept", "*/*" );
305 success: function( data ) {
306 assert.strictEqual( data, "accept: */*\n", "Test Accept header is set to last value provided" );
311 ajaxTest( "jQuery.ajax() - contentType", 2, function( assert ) {
314 url: url( "mock.php?action=headers&keys=content-type" ),
316 success: function( data ) {
317 assert.strictEqual( data, "content-type: test\n", "Test content-type is sent when options.contentType is set" );
321 url: url( "mock.php?action=headers&keys=content-type" ),
323 success: function( data ) {
325 // Some server/interpreter combinations always supply a Content-Type to scripts
326 data = data || "content-type: \n";
327 assert.strictEqual( data, "content-type: \n", "Test content-type is not set when options.contentType===false" );
333 ajaxTest( "jQuery.ajax() - protocol-less urls", 1, function( assert ) {
335 url: "//somedomain.com",
336 beforeSend: function( xhr, settings ) {
337 assert.equal( settings.url, location.protocol + "//somedomain.com", "Make sure that the protocol is added." );
344 ajaxTest( "jQuery.ajax() - URL fragment component preservation", 4, function( assert ) {
347 url: baseURL + "name.html#foo",
348 beforeSend: function( xhr, settings ) {
349 assert.equal( settings.url, baseURL + "name.html#foo",
350 "hash preserved for request with no query component." );
356 url: baseURL + "name.html?abc#foo",
357 beforeSend: function( xhr, settings ) {
358 assert.equal( settings.url, baseURL + "name.html?abc#foo",
359 "hash preserved for request with query component." );
365 url: baseURL + "name.html?abc#foo",
369 beforeSend: function( xhr, settings ) {
370 assert.equal( settings.url, baseURL + "name.html?abc&test=123#foo",
371 "hash preserved for request with query component and data." );
377 url: baseURL + "name.html?abc#brownies",
382 beforeSend: function( xhr, settings ) {
383 // Clear the cache-buster param value
384 var url = settings.url.replace( /_=[^&#]+/, "_=" );
385 assert.equal( url, baseURL + "name.html?abc&devo=hat&_=#brownies",
386 "hash preserved for cache-busting request with query component and data." );
394 ajaxTest( "jQuery.ajax() - traditional param encoding", 4, function( assert ) {
404 beforeSend: function( xhr, settings ) {
405 assert.equal( settings.url, "/?devo=hat&answer=42&quux=a%20space", "Simple case" );
415 "b[]": [ "b1", "b2" ]
417 beforeSend: function( xhr, settings ) {
418 assert.equal( settings.url, "/?a=1&a=2&a=3&b%5B%5D=b1&b%5B%5D=b2", "Arrays" );
427 "a": [ [ 1, 2 ], [ 3, 4 ], 5 ]
429 beforeSend: function( xhr, settings ) {
430 assert.equal( settings.url, "/?a=1%2C2&a=3%2C4&a=5", "Nested arrays" );
439 "a": [ "w", [ [ "x", "y" ], "z" ] ]
442 beforeSend: function( xhr, settings ) {
443 var url = settings.url.replace( /\d{3,}/, "" );
444 assert.equal( url, "/?a=w&a=x%2Cy%2Cz&_=", "Cache-buster" );
452 ajaxTest( "jQuery.ajax() - cross-domain detection", 8, function( assert ) {
453 function request( url, title, crossDomainOrOptions ) {
454 return jQuery.extend( {
457 beforeSend: function( _, s ) {
458 assert.ok( crossDomainOrOptions === false ? !s.crossDomain : s.crossDomain, title );
462 }, crossDomainOrOptions );
465 var loc = document.location,
466 samePort = loc.port || ( loc.protocol === "http:" ? 80 : 443 ),
467 otherPort = loc.port === 666 ? 667 : 666,
468 otherProtocol = loc.protocol === "http:" ? "https:" : "http:";
472 loc.protocol + "//" + loc.hostname + ":" + samePort,
473 "Test matching ports are not detected as cross-domain",
477 otherProtocol + "//" + loc.host,
478 "Test different protocols are detected as cross-domain"
482 "Adobe AIR app:/ URL detected as cross-domain"
485 loc.protocol + "//example.invalid:" + ( loc.port || 80 ),
486 "Test different hostnames are detected as cross-domain"
489 loc.protocol + "//" + loc.hostname + ":" + otherPort,
490 "Test different ports are detected as cross-domain"
494 "Test about:blank is detected as cross-domain"
497 loc.protocol + "//" + loc.host,
498 "Test forced crossDomain is detected as cross-domain",
504 " https://otherdomain.com",
505 "Cross-domain url with leading space is detected as cross-domain"
510 ajaxTest( "jQuery.ajax() - abort", 9, function( assert ) {
512 setup: addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxError ajaxComplete", assert ),
513 url: url( "mock.php?action=wait&wait=5" ),
514 beforeSend: function() {
515 assert.ok( true, "beforeSend" );
517 afterSend: function( xhr ) {
518 assert.strictEqual( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
520 assert.strictEqual( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
523 complete: function() {
524 assert.ok( true, "complete" );
529 ajaxTest( "jQuery.ajax() - native abort", 2, function( assert ) {
531 url: url( "mock.php?action=wait&wait=1" ),
533 var xhr = new window.XMLHttpRequest();
534 setTimeout( function() {
539 error: function( xhr, msg ) {
540 assert.strictEqual( msg, "error", "Native abort triggers error callback" );
542 complete: function() {
543 assert.ok( true, "complete" );
548 ajaxTest( "jQuery.ajax() - native timeout", 2, function( assert ) {
550 url: url( "mock.php?action=wait&wait=1" ),
552 var xhr = new window.XMLHttpRequest();
556 error: function( xhr, msg ) {
557 assert.strictEqual( msg, "error", "Native timeout triggers error callback" );
559 complete: function() {
560 assert.ok( true, "complete" );
565 ajaxTest( "jQuery.ajax() - events with context", 12, function( assert ) {
566 var context = document.createElement( "div" );
568 function event( e ) {
569 assert.equal( this, context, e.type );
572 function callback( msg ) {
574 assert.equal( this, context, "context is preserved on callback " + msg );
580 jQuery( context ).appendTo( "#foo" )
581 .on( "ajaxSend", event )
582 .on( "ajaxComplete", event )
583 .on( "ajaxError", event )
584 .on( "ajaxSuccess", event );
587 url: url( "name.html" ),
589 beforeSend: callback( "beforeSend" ),
590 success: callback( "success" ),
591 complete: callback( "complete" )
593 url: url( "404.txt" ),
595 beforeSend: callback( "beforeSend" ),
596 error: callback( "error" ),
597 complete: callback( "complete" )
602 ajaxTest( "jQuery.ajax() - events without context", 3, function( assert ) {
603 function nocallback( msg ) {
605 assert.equal( typeof this.url, "string", "context is settings on callback " + msg );
609 url: url( "404.txt" ),
610 beforeSend: nocallback( "beforeSend" ),
611 error: nocallback( "error" ),
612 complete: nocallback( "complete" )
616 ajaxTest( "#15118 - jQuery.ajax() - function without jQuery.event", 1, function( assert ) {
619 url: url( "mock.php?action=json" ),
621 holder = jQuery.event;
624 complete: function() {
625 assert.ok( true, "Call can be made without jQuery.event" );
626 jQuery.event = holder;
632 ajaxTest( "#15160 - jQuery.ajax() - request manually aborted in ajaxSend", 3, function( assert ) {
635 jQuery( document ).on( "ajaxSend", function( e, jqXHR ) {
639 jQuery( document ).on( "ajaxError ajaxComplete", function( e, jqXHR ) {
640 assert.equal( jqXHR.statusText, "abort", "jqXHR.statusText equals abort on global ajaxComplete and ajaxError events" );
643 url: url( "name.html" ),
645 complete: function() {
646 assert.ok( true, "complete" );
651 ajaxTest( "jQuery.ajax() - context modification", 1, function( assert ) {
653 url: url( "name.html" ),
655 beforeSend: function() {
658 afterSend: function() {
659 assert.strictEqual( this.context.test, "foo", "Make sure the original object is maintained." );
665 ajaxTest( "jQuery.ajax() - context modification through ajaxSetup", 3, function( assert ) {
672 assert.strictEqual( jQuery.ajaxSettings.context, obj, "Make sure the context is properly set in ajaxSettings." );
675 url: url( "name.html" ),
676 success: function() {
677 assert.strictEqual( this, obj, "Make sure the original object is maintained." );
680 url: url( "name.html" ),
682 success: function() {
683 assert.ok( this !== obj, "Make sure overriding context is possible." );
689 ajaxTest( "jQuery.ajax() - disabled globals", 3, function( assert ) {
691 setup: addGlobalEvents( "", assert ),
693 url: url( "name.html" ),
694 beforeSend: function() {
695 assert.ok( true, "beforeSend" );
697 success: function() {
698 assert.ok( true, "success" );
700 complete: function() {
701 assert.ok( true, "complete" );
706 ajaxTest( "jQuery.ajax() - xml: non-namespace elements inside namespaced elements", 3, function( assert ) {
708 url: url( "with_fries.xml" ),
710 success: function( resp ) {
711 assert.equal( jQuery( "properties", resp ).length, 1, "properties in responseXML" );
712 assert.equal( jQuery( "jsconf", resp ).length, 1, "jsconf in responseXML" );
713 assert.equal( jQuery( "thing", resp ).length, 2, "things in responseXML" );
718 ajaxTest( "jQuery.ajax() - xml: non-namespace elements inside namespaced elements (over JSONP)", 3, function( assert ) {
720 url: url( "mock.php?action=xmlOverJsonp" ),
721 dataType: "jsonp xml",
722 success: function( resp ) {
723 assert.equal( jQuery( "properties", resp ).length, 1, "properties in responseXML" );
724 assert.equal( jQuery( "jsconf", resp ).length, 1, "jsconf in responseXML" );
725 assert.equal( jQuery( "thing", resp ).length, 2, "things in responseXML" );
730 ajaxTest( "jQuery.ajax() - HEAD requests", 2, function( assert ) {
733 url: url( "name.html" ),
735 success: function( data, status, xhr ) {
736 assert.ok( /Date/i.test( xhr.getAllResponseHeaders() ), "No Date in HEAD response" );
740 url: url( "name.html" ),
745 success: function( data, status, xhr ) {
746 assert.ok( /Date/i.test( xhr.getAllResponseHeaders() ), "No Date in HEAD response with data" );
752 ajaxTest( "jQuery.ajax() - beforeSend", 1, function( assert ) {
754 url: url( "name.html" ),
755 beforeSend: function() {
758 success: function() {
759 assert.ok( this.check, "check beforeSend was executed" );
764 ajaxTest( "jQuery.ajax() - beforeSend, cancel request manually", 2, function( assert ) {
767 return jQuery.ajax( {
768 url: url( "name.html" ),
769 beforeSend: function( xhr ) {
770 assert.ok( true, "beforeSend got called, canceling" );
773 success: function() {
774 assert.ok( false, "request didn't get canceled" );
776 complete: function() {
777 assert.ok( false, "request didn't get canceled" );
780 assert.ok( false, "request didn't get canceled" );
784 fail: function( _, reason ) {
785 assert.strictEqual( reason, "canceled", "canceled request must fail with 'canceled' status text" );
790 ajaxTest( "jQuery.ajax() - dataType html", 5, function( assert ) {
793 Globals.register( "testFoo" );
794 Globals.register( "testBar" );
797 url: url( "mock.php?action=testHTML&baseURL=" + baseURL ),
798 success: function( data ) {
799 assert.ok( data.match( /^html text/ ), "Check content for datatype html" );
800 jQuery( "#ap" ).html( data );
801 assert.strictEqual( window[ "testFoo" ], "foo", "Check if script was evaluated for datatype html" );
802 assert.strictEqual( window[ "testBar" ], "bar", "Check if script src was evaluated for datatype html" );
807 ajaxTest( "jQuery.ajax() - do execute scripts if JSONP from unsuccessful responses", 1, function( assert ) {
808 var testMsg = "Unsuccessful JSONP requests should have a JSON body";
811 url: url( "mock.php?action=errorWithScript" ),
812 // error is the significant assertion
813 error: function( xhr ) {
814 var expected = { "status": 404, "msg": "Not Found" };
815 assert.deepEqual( xhr.responseJSON, expected, testMsg );
820 ajaxTest( "jQuery.ajax() - do not execute scripts from unsuccessful responses (gh-4250)", 11, function( assert ) {
821 var globalEval = jQuery.globalEval;
823 var failConverters = {
824 "text script": function() {
825 assert.ok( false, "No converter for unsuccessful response" );
829 function request( title, options ) {
830 var testMsg = title + ": expected file missing status";
831 return jQuery.extend( {
832 beforeSend: function() {
833 jQuery.globalEval = function() {
834 assert.ok( false, "Should not eval" );
837 complete: function() {
838 jQuery.globalEval = globalEval;
840 // error is the significant assertion
841 error: function( xhr ) {
842 assert.strictEqual( xhr.status, 404, testMsg );
844 success: function() {
845 assert.ok( false, "Unanticipated success" );
854 url: url( "404.txt" )
858 "HTML reply with dataType",
861 url: url( "404.txt" )
867 url: url( "mock.php?action=errorWithScript&withScriptContentType" )
873 url: url( "mock.php?action=errorWithScript" )
877 "script reply with dataType",
880 url: url( "mock.php?action=errorWithScript&withScriptContentType" )
884 "non-script reply with dataType",
887 url: url( "mock.php?action=errorWithScript" )
891 "script reply with converter",
893 converters: failConverters,
894 url: url( "mock.php?action=errorWithScript&withScriptContentType" )
898 "non-script reply with converter",
900 converters: failConverters,
901 url: url( "mock.php?action=errorWithScript" )
905 "script reply with converter and dataType",
907 converters: failConverters,
909 url: url( "mock.php?action=errorWithScript&withScriptContentType" )
913 "non-script reply with converter and dataType",
915 converters: failConverters,
917 url: url( "mock.php?action=errorWithScript" )
921 "JSONP reply with dataType",
924 url: url( "mock.php?action=errorWithScript" ),
925 beforeSend: function() {
926 jQuery.globalEval = function( response ) {
927 assert.ok( /"status": 404, "msg": "Not Found"/.test( response ), "Error object returned" );
935 ajaxTest( "jQuery.ajax() - synchronous request", 1, function( assert ) {
937 url: url( "json_obj.js" ),
941 afterSend: function( xhr ) {
942 assert.ok( /^\{ "data"/.test( xhr.responseText ), "check returned text" );
947 ajaxTest( "jQuery.ajax() - synchronous request with callbacks", 2, function( assert ) {
949 url: url( "json_obj.js" ),
953 afterSend: function( xhr ) {
955 xhr.done( function( data ) {
956 assert.ok( true, "success callback executed" );
959 assert.ok( /^\{ "data"/.test( result ), "check returned text" );
964 QUnit.test( "jQuery.ajax(), jQuery.get[Script|JSON](), jQuery.post(), pass-through request object", function( assert ) {
966 var done = assert.async();
967 var target = "name.html",
971 success = function() {
974 jQuery( document ).on( "ajaxError.passthru", function( e, xml ) {
976 errorEx += ": " + xml.status;
978 jQuery( document ).one( "ajaxStop", function() {
979 assert.equal( successCount, 5, "Check all ajax calls successful" );
980 assert.equal( errorCount, 0, "Check no ajax errors (status" + errorEx + ")" );
981 jQuery( document ).off( "ajaxError.passthru" );
984 Globals.register( "testBar" );
986 assert.ok( jQuery.get( url( target ), success ), "get" );
987 assert.ok( jQuery.post( url( target ), success ), "post" );
988 assert.ok( jQuery.getScript( url( "mock.php?action=testbar" ), success ), "script" );
989 assert.ok( jQuery.getJSON( url( "json_obj.js" ), success ), "json" );
990 assert.ok( jQuery.ajax( {
996 ajaxTest( "jQuery.ajax() - cache", 28, function( assert ) {
997 var re = /_=(.*?)(&|$)/g,
998 rootUrl = baseURL + "text.txt";
1000 function request( url, title ) {
1004 beforeSend: function() {
1008 assert.equal( this.url.indexOf( rootUrl ), 0, "root url not mangled: " + this.url );
1009 assert.equal( /\&.*\?/.test( this.url ), false, "parameter delimiters in order" );
1011 while ( ( tmp = re.exec( this.url ) ) ) {
1012 assert.strictEqual( parameter, undefined, title + ": only one 'no-cache' parameter" );
1013 parameter = tmp[ 1 ];
1014 assert.notStrictEqual( parameter, "tobereplaced555", title + ": parameter (if it was there) was replaced" );
1032 rootUrl + "?pizza=true",
1036 rootUrl + "?_=tobereplaced555",
1040 rootUrl + "?pizza=true&_=tobereplaced555",
1041 "1 parameter and _="
1044 rootUrl + "?_=tobereplaced555&tv=false",
1045 "_= and 1 parameter"
1048 rootUrl + "?name=David&_=tobereplaced555&washere=true",
1049 "2 parameters surrounding _="
1054 jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
1056 ajaxTest( "jQuery.ajax() - JSONP - Query String (?n)" + label, 4, function( assert ) {
1059 url: baseURL + "mock.php?action=jsonp&callback=?",
1061 crossDomain: crossDomain,
1062 success: function( data ) {
1063 assert.ok( data.data, "JSON results returned (GET, url callback)" );
1067 url: baseURL + "mock.php?action=jsonp&callback=??",
1069 crossDomain: crossDomain,
1070 success: function( data ) {
1071 assert.ok( data.data, "JSON results returned (GET, url context-free callback)" );
1075 url: baseURL + "mock.php/???action=jsonp",
1077 crossDomain: crossDomain,
1078 success: function( data ) {
1079 assert.ok( data.data, "JSON results returned (GET, REST-like)" );
1083 url: baseURL + "mock.php/???action=jsonp&array=1",
1085 crossDomain: crossDomain,
1086 success: function( data ) {
1087 assert.ok( Array.isArray( data ), "JSON results returned (GET, REST-like with param)" );
1093 ajaxTest( "jQuery.ajax() - JSONP - Explicit callback param" + label, 10, function( assert ) {
1096 Globals.register( "functionToCleanUp" );
1097 Globals.register( "XXX" );
1098 Globals.register( "jsonpResults" );
1099 window[ "jsonpResults" ] = function( data ) {
1100 assert.ok( data[ "data" ], "JSON results returned (GET, custom callback function)" );
1104 url: baseURL + "mock.php?action=jsonp",
1106 crossDomain: crossDomain,
1108 success: function( data ) {
1109 assert.ok( data[ "data" ], "JSON results returned (GET, data obj callback)" );
1112 url: baseURL + "mock.php?action=jsonp",
1114 crossDomain: crossDomain,
1115 jsonpCallback: "jsonpResults",
1116 success: function( data ) {
1118 typeof window[ "jsonpResults" ],
1120 "should not rewrite original function"
1122 assert.ok( data.data, "JSON results returned (GET, custom callback name)" );
1125 url: baseURL + "mock.php?action=jsonp",
1127 crossDomain: crossDomain,
1128 jsonpCallback: "functionToCleanUp",
1129 success: function( data ) {
1130 assert.ok( data[ "data" ], "JSON results returned (GET, custom callback name to be cleaned up)" );
1131 assert.strictEqual( window[ "functionToCleanUp" ], true, "Callback was removed (GET, custom callback name to be cleaned up)" );
1134 url: baseURL + "mock.php?action=jsonp",
1136 crossDomain: crossDomain,
1137 jsonpCallback: "functionToCleanUp",
1138 beforeSend: function( jqXHR ) {
1143 xhr.fail( function() {
1144 assert.ok( true, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1145 assert.strictEqual( window[ "functionToCleanUp" ], true, "Callback was removed after early abort (GET, custom callback name to be cleaned up)" );
1149 url: baseURL + "mock.php?action=jsonp&callback=XXX",
1152 jsonpCallback: "XXX",
1153 crossDomain: crossDomain,
1154 beforeSend: function() {
1155 assert.ok( /action=jsonp&callback=XXX&_=\d+$/.test( this.url ), "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
1157 success: function( data ) {
1158 assert.ok( data[ "data" ], "JSON results returned (GET, custom callback name with no url manipulation)" );
1164 ajaxTest( "jQuery.ajax() - JSONP - Callback in data" + label, 2, function( assert ) {
1167 url: baseURL + "mock.php?action=jsonp",
1169 crossDomain: crossDomain,
1171 success: function( data ) {
1172 assert.ok( data.data, "JSON results returned (GET, data callback)" );
1176 url: baseURL + "mock.php?action=jsonp",
1178 crossDomain: crossDomain,
1179 data: "callback=??",
1180 success: function( data ) {
1181 assert.ok( data.data, "JSON results returned (GET, data context-free callback)" );
1187 ajaxTest( "jQuery.ajax() - JSONP - POST" + label, 3, function( assert ) {
1191 url: baseURL + "mock.php?action=jsonp",
1193 crossDomain: crossDomain,
1194 success: function( data ) {
1195 assert.ok( data[ "data" ], "JSON results returned (POST, no callback)" );
1200 url: baseURL + "mock.php?action=jsonp",
1203 crossDomain: crossDomain,
1204 success: function( data ) {
1205 assert.ok( data[ "data" ], "JSON results returned (POST, data callback)" );
1210 url: baseURL + "mock.php?action=jsonp",
1213 crossDomain: crossDomain,
1214 success: function( data ) {
1215 assert.ok( data[ "data" ], "JSON results returned (POST, data obj callback)" );
1221 ajaxTest( "jQuery.ajax() - JSONP" + label, 3, function( assert ) {
1224 url: baseURL + "mock.php?action=jsonp",
1226 crossDomain: crossDomain,
1227 success: function( data ) {
1228 assert.ok( data.data, "JSON results returned (GET, no callback)" );
1232 create: function( options ) {
1233 var request = jQuery.ajax( options ),
1234 promise = request.then( function( data ) {
1235 assert.ok( data.data, "first request: JSON results returned (GET, no callback)" );
1236 request = jQuery.ajax( this ).done( function( data ) {
1237 assert.ok( data.data, "this re-used: JSON results returned (GET, no callback)" );
1239 promise.abort = request.abort;
1242 promise.abort = request.abort;
1245 url: baseURL + "mock.php?action=jsonp",
1247 crossDomain: crossDomain,
1253 ajaxTest( "jQuery.ajax() - no JSONP auto-promotion" + label, 4, function( assert ) {
1256 url: baseURL + "mock.php?action=jsonp",
1258 crossDomain: crossDomain,
1259 success: function() {
1260 assert.ok( false, "JSON parsing should have failed (no callback)" );
1263 assert.ok( true, "JSON parsing failed, JSONP not used (no callback)" );
1267 url: baseURL + "mock.php?action=jsonp&callback=?",
1269 crossDomain: crossDomain,
1270 success: function() {
1271 assert.ok( false, "JSON parsing should have failed (ULR callback)" );
1274 assert.ok( true, "JSON parsing failed, JSONP not used (URL callback)" );
1278 url: baseURL + "mock.php?action=jsonp",
1280 crossDomain: crossDomain,
1282 success: function() {
1283 assert.ok( false, "JSON parsing should have failed (data callback=?)" );
1286 assert.ok( true, "JSON parsing failed, JSONP not used (data callback=?)" );
1290 url: baseURL + "mock.php?action=jsonp",
1292 crossDomain: crossDomain,
1293 data: "callback=??",
1294 success: function() {
1295 assert.ok( false, "JSON parsing should have failed (data callback=??)" );
1298 assert.ok( true, "JSON parsing failed, JSONP not used (data callback=??)" );
1304 ajaxTest( "jQuery.ajax() - JSON - no ? replacement" + label, 9, function( assert ) {
1307 url: baseURL + "mock.php?action=json&callback=?",
1309 crossDomain: crossDomain,
1310 beforeSend: function( _jqXhr, settings ) {
1311 var queryString = settings.url.replace( /^[^?]*\?/, "" );
1313 queryString.indexOf( "jQuery" ) === -1,
1314 "jQuery callback not inserted into the URL (URL callback)"
1317 queryString.indexOf( "callback=?" ) > -1,
1318 "\"callback=?\" present in the URL unchanged (URL callback)"
1321 success: function( data ) {
1322 assert.ok( data.data, "JSON results returned (URL callback)" );
1326 url: baseURL + "mock.php?action=json",
1328 crossDomain: crossDomain,
1330 beforeSend: function( _jqXhr, settings ) {
1331 var queryString = settings.url.replace( /^[^?]*\?/, "" );
1333 queryString.indexOf( "jQuery" ) === -1,
1334 "jQuery callback not inserted into the URL (data callback=?)"
1337 queryString.indexOf( "callback=?" ) > -1,
1338 "\"callback=?\" present in the URL unchanged (data callback=?)"
1341 success: function( data ) {
1342 assert.ok( data.data, "JSON results returned (data callback=?)" );
1346 url: baseURL + "mock.php?action=json",
1348 crossDomain: crossDomain,
1349 data: "callback=??",
1350 beforeSend: function( _jqXhr, settings ) {
1351 var queryString = settings.url.replace( /^[^?]*\?/, "" );
1353 queryString.indexOf( "jQuery" ) === -1,
1354 "jQuery callback not inserted into the URL (data callback=??)"
1357 queryString.indexOf( "callback=??" ) > -1,
1358 "\"callback=?\" present in the URL unchanged (data callback=??)"
1361 success: function( data ) {
1362 assert.ok( data.data, "JSON results returned (data callback=??)" );
1371 "jQuery.ajax() - script, CSP script-src compat (gh-3969)",
1372 "mock.php?action=cspAjaxScript",
1373 function( assert, jQuery, window, document, type, downloadedScriptCalled ) {
1376 assert.strictEqual( type, "GET", "Type changed to GET" );
1377 assert.strictEqual( downloadedScriptCalled, true, "External script called" );
1381 ajaxTest( "jQuery.ajax() - script, Remote", 2, function( assert ) {
1384 Globals.register( "testBar" );
1386 url: url( "mock.php?action=testbar" ),
1388 success: function() {
1389 assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (GET, no callback)" );
1394 ajaxTest( "jQuery.ajax() - script, Remote with POST", 4, function( assert ) {
1397 Globals.register( "testBar" );
1399 url: url( "mock.php?action=testbar" ),
1400 beforeSend: function( _jqXhr, settings ) {
1401 assert.strictEqual( settings.type, "GET", "Type changed to GET" );
1405 success: function( data, status ) {
1406 assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (POST, no callback)" );
1407 assert.strictEqual( status, "success", "Script results returned (POST, no callback)" );
1412 ajaxTest( "jQuery.ajax() - script, Remote with scheme-less URL", 2, function( assert ) {
1415 Globals.register( "testBar" );
1417 url: url( "mock.php?action=testbar" ),
1419 success: function() {
1420 assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (GET, no callback)" );
1425 ajaxTest( "jQuery.ajax() - malformed JSON", 2, function( assert ) {
1427 url: baseURL + "badjson.js",
1429 error: function( xhr, msg, detailedMsg ) {
1430 assert.strictEqual( msg, "parsererror", "A parse error occurred." );
1431 assert.ok( /(invalid|error|exception)/i.test( detailedMsg ), "Detailed parsererror message provided" );
1436 ajaxTest( "jQuery.ajax() - JSON by content-type", 5, function( assert ) {
1438 url: baseURL + "mock.php?action=json",
1443 success: function( json ) {
1444 assert.ok( json.length >= 2, "Check length" );
1445 assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" );
1446 assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" );
1447 assert.strictEqual( json[ 1 ][ "name" ], "Peter", "Check JSON: second, name" );
1448 assert.strictEqual( json[ 1 ][ "age" ], 25, "Check JSON: second, age" );
1453 ajaxTest( "jQuery.ajax() - JSON by content-type disabled with options", 6, function( assert ) {
1455 url: url( "mock.php?action=json" ),
1463 success: function( text ) {
1464 assert.strictEqual( typeof text, "string", "json wasn't auto-determined" );
1465 var json = JSON.parse( text );
1466 assert.ok( json.length >= 2, "Check length" );
1467 assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" );
1468 assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" );
1469 assert.strictEqual( json[ 1 ][ "name" ], "Peter", "Check JSON: second, name" );
1470 assert.strictEqual( json[ 1 ][ "age" ], 25, "Check JSON: second, age" );
1475 ajaxTest( "jQuery.ajax() - simple get", 1, function( assert ) {
1478 url: url( "mock.php?action=name&name=foo" ),
1479 success: function( msg ) {
1480 assert.strictEqual( msg, "bar", "Check for GET" );
1485 ajaxTest( "jQuery.ajax() - simple post", 1, function( assert ) {
1488 url: url( "mock.php?action=name" ),
1490 success: function( msg ) {
1491 assert.strictEqual( msg, "pan", "Check for POST" );
1496 ajaxTest( "jQuery.ajax() - data option - empty bodies for non-GET requests", 1, function( assert ) {
1498 url: baseURL + "mock.php?action=echoData",
1501 success: function( result ) {
1502 assert.strictEqual( result, "" );
1507 ajaxTest( "jQuery.ajax() - data - x-www-form-urlencoded (gh-2658)", 1, function( assert ) {
1510 data: { devo: "A Beautiful World" },
1512 beforeSend: function( _, s ) {
1513 assert.strictEqual( s.data, "devo=A+Beautiful+World", "data is '+'-encoded" );
1520 ajaxTest( "jQuery.ajax() - data - text/plain (gh-2658)", 1, function( assert ) {
1523 data: { devo: "A Beautiful World" },
1525 contentType: "text/plain",
1526 beforeSend: function( _, s ) {
1527 assert.strictEqual( s.data, "devo=A%20Beautiful%20World", "data is %20-encoded" );
1534 ajaxTest( "jQuery.ajax() - don't escape %20 with contentType override (gh-4119)", 1, function( assert ) {
1537 contentType: "application/x-www-form-urlencoded",
1538 headers: { "content-type": "application/json" },
1541 data: "{\"val\":\"%20\"}",
1542 beforeSend: function( _, s ) {
1543 assert.strictEqual( s.data, "{\"val\":\"%20\"}", "data is not %20-encoded" );
1550 ajaxTest( "jQuery.ajax() - escape %20 with contentType override (gh-4119)", 1, function( assert ) {
1553 contentType: "application/json",
1554 headers: { "content-type": "application/x-www-form-urlencoded" },
1557 data: "{\"val\":\"%20\"}",
1558 beforeSend: function( _, s ) {
1559 assert.strictEqual( s.data, "{\"val\":\"+\"}", "data is %20-encoded" );
1566 ajaxTest( "jQuery.ajax() - override contentType with header (gh-4119)", 1, function( assert ) {
1569 contentType: "application/json",
1570 headers: { "content-type": "application/x-www-form-urlencoded" },
1571 beforeSend: function( _, s ) {
1572 assert.strictEqual( s.contentType, "application/x-www-form-urlencoded",
1573 "contentType is overwritten" );
1580 ajaxTest( "jQuery.ajax() - data - no processing POST", 1, function( assert ) {
1583 data: { devo: "A Beautiful World" },
1585 contentType: "x-special-sauce",
1587 beforeSend: function( _, s ) {
1588 assert.deepEqual( s.data, { devo: "A Beautiful World" }, "data is not processed" );
1595 ajaxTest( "jQuery.ajax() - data - no processing GET", 1, function( assert ) {
1598 data: { devo: "A Beautiful World" },
1600 contentType: "x-something-else",
1602 beforeSend: function( _, s ) {
1603 assert.deepEqual( s.data, { devo: "A Beautiful World" }, "data is not processed" );
1610 ajaxTest( "jQuery.ajax() - data - process string with GET", 2, function( assert ) {
1615 contentType: "x-something-else",
1617 beforeSend: function( _, s ) {
1618 assert.equal( s.url, "bogus.html?a=1&b=2", "added data to url" );
1619 assert.equal( s.data, undefined, "removed data from settings" );
1626 var ifModifiedNow = new Date();
1629 /* jQuery.each arguments start */
1632 " (no cache)": false
1634 function( label, cache ) {
1637 "If-Modified-Since": {
1638 url: "mock.php?action=ims",
1642 url: "mock.php?action=etag",
1644 // Support: TestSwarm
1645 // TestSwarm is now proxied via Cloudflare which cuts out
1646 // headers relevant for ETag tests, failing them. We're still
1647 // running those tests in Karma on Chrome & Firefox (including
1649 qunitMethod: QUnit.isSwarm ? "skip" : "test"
1652 function( type, data ) {
1653 var url = baseURL + data.url + "&ts=" + ifModifiedNow++;
1654 QUnit[ data.qunitMethod ]( "jQuery.ajax() - " + type +
1655 " support" + label, function( assert ) {
1657 var done = assert.async();
1662 success: function( _, status ) {
1663 assert.strictEqual( status, "success", "Initial status is 'success'" );
1668 success: function( data, status, jqXHR ) {
1669 assert.strictEqual( status, "notmodified", "Following status is 'notmodified'" );
1670 assert.strictEqual( jqXHR.status, 304, "XHR status is 304" );
1671 assert.equal( data, null, "no response body is given" );
1673 complete: function() {
1683 /* jQuery.each arguments end */
1686 ajaxTest( "jQuery.ajax() - failing cross-domain (non-existing)", 1, function( assert ) {
1690 url: "https://example.invalid",
1691 error: function( xhr, _, e ) {
1692 assert.ok( true, "file not found: " + xhr.status + " => " + e );
1697 ajaxTest( "jQuery.ajax() - failing cross-domain", 1, function( assert ) {
1699 url: "https://" + externalHost,
1700 error: function( xhr, _, e ) {
1701 assert.ok( true, "access denied: " + xhr.status + " => " + e );
1706 ajaxTest( "jQuery.ajax() - atom+xml", 1, function( assert ) {
1708 url: url( "mock.php?action=atom" ),
1709 success: function() {
1710 assert.ok( true, "success" );
1715 QUnit.test( "jQuery.ajax() - statusText", function( assert ) {
1717 var done = assert.async();
1718 jQuery.ajax( url( "mock.php?action=status&code=200&text=Hello" ) ).done( function( _, statusText, jqXHR ) {
1719 assert.strictEqual( statusText, "success", "callback status text ok for success" );
1720 assert.ok( [ "Hello", "OK", "success" ].indexOf( jqXHR.statusText ) > -1,
1721 "jqXHR status text ok for success (" + jqXHR.statusText + ")" );
1722 jQuery.ajax( url( "mock.php?action=status&code=404&text=World" ) ).fail( function( jqXHR, statusText ) {
1723 assert.strictEqual( statusText, "error", "callback status text ok for error" );
1729 QUnit.test( "jQuery.ajax() - statusCode", function( assert ) {
1730 assert.expect( 20 );
1731 var done = assert.async(),
1734 function countComplete() {
1740 function createStatusCodes( name, isSuccess ) {
1741 name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
1744 assert.ok( isSuccess, name );
1747 assert.ok( !isSuccess, name );
1753 /* jQuery.each arguments start */
1758 function( uri, isSuccess ) {
1759 jQuery.ajax( url( uri ), {
1760 statusCode: createStatusCodes( "in options", isSuccess ),
1761 complete: countComplete
1764 jQuery.ajax( url( uri ), {
1765 complete: countComplete
1766 } ).statusCode( createStatusCodes( "immediately with method", isSuccess ) );
1768 jQuery.ajax( url( uri ), {
1769 complete: function( jqXHR ) {
1770 jqXHR.statusCode( createStatusCodes( "on complete", isSuccess ) );
1775 jQuery.ajax( url( uri ), {
1776 complete: function( jqXHR ) {
1777 setTimeout( function() {
1778 jqXHR.statusCode( createStatusCodes( "very late binding", isSuccess ) );
1784 jQuery.ajax( url( uri ), {
1785 statusCode: createStatusCodes( "all (options)", isSuccess ),
1786 complete: function( jqXHR ) {
1787 jqXHR.statusCode( createStatusCodes( "all (on complete)", isSuccess ) );
1788 setTimeout( function() {
1789 jqXHR.statusCode( createStatusCodes( "all (very late binding)", isSuccess ) );
1793 } ).statusCode( createStatusCodes( "all (immediately with method)", isSuccess ) );
1795 var testString = "";
1797 jQuery.ajax( url( uri ), {
1798 success: function( a, b, jqXHR ) {
1799 assert.ok( isSuccess, "success" );
1800 var statusCode = {};
1801 statusCode[ jqXHR.status ] = function() {
1804 jqXHR.statusCode( statusCode );
1807 error: function( jqXHR ) {
1808 assert.ok( !isSuccess, "error" );
1809 var statusCode = {};
1810 statusCode[ jqXHR.status ] = function() {
1813 jqXHR.statusCode( statusCode );
1816 complete: function() {
1820 "Test statusCode callbacks are ordered like " + ( isSuccess ? "success" : "error" ) + " callbacks"
1827 /* jQuery.each arguments end*/
1831 ajaxTest( "jQuery.ajax() - transitive conversions", 8, function( assert ) {
1834 url: url( "mock.php?action=json" ),
1836 "json myJson": function( data ) {
1837 assert.ok( true, "converter called" );
1842 success: function() {
1843 assert.ok( true, "Transitive conversion worked" );
1844 assert.strictEqual( this.dataTypes[ 0 ], "text", "response was retrieved as text" );
1845 assert.strictEqual( this.dataTypes[ 1 ], "myjson", "request expected myjson dataType" );
1849 url: url( "mock.php?action=json" ),
1851 "json myJson": function( data ) {
1852 assert.ok( true, "converter called (*)" );
1856 contents: false, /* headers are wrong so we ignore them */
1857 dataType: "* myJson",
1858 success: function() {
1859 assert.ok( true, "Transitive conversion worked (*)" );
1860 assert.strictEqual( this.dataTypes[ 0 ], "text", "response was retrieved as text (*)" );
1861 assert.strictEqual( this.dataTypes[ 1 ], "myjson", "request expected myjson dataType (*)" );
1867 ajaxTest( "jQuery.ajax() - overrideMimeType", 2, function( assert ) {
1870 url: url( "mock.php?action=json" ),
1871 beforeSend: function( xhr ) {
1872 xhr.overrideMimeType( "application/json" );
1874 success: function( json ) {
1875 assert.ok( json.data, "Mimetype overridden using beforeSend" );
1879 url: url( "mock.php?action=json" ),
1880 mimeType: "application/json",
1881 success: function( json ) {
1882 assert.ok( json.data, "Mimetype overridden using mimeType option" );
1888 ajaxTest( "jQuery.ajax() - empty json gets to error callback instead of success callback.", 1, function( assert ) {
1890 url: url( "mock.php?action=echoData" ),
1891 error: function( _, __, error ) {
1892 assert.equal( typeof error === "object", true, "Didn't get back error object for empty json response" );
1898 ajaxTest( "#2688 - jQuery.ajax() - beforeSend, cancel request", 2, function( assert ) {
1900 create: function() {
1901 return jQuery.ajax( {
1902 url: url( "name.html" ),
1903 beforeSend: function() {
1904 assert.ok( true, "beforeSend got called, canceling" );
1907 success: function() {
1908 assert.ok( false, "request didn't get canceled" );
1910 complete: function() {
1911 assert.ok( false, "request didn't get canceled" );
1914 assert.ok( false, "request didn't get canceled" );
1918 fail: function( _, reason ) {
1919 assert.strictEqual( reason, "canceled", "canceled request must fail with 'canceled' status text" );
1924 ajaxTest( "#2806 - jQuery.ajax() - data option - evaluate function values", 1, function( assert ) {
1926 url: baseURL + "mock.php?action=echoQuery",
1932 success: function( result ) {
1933 assert.strictEqual( result, "action=echoQuery&key=value" );
1938 QUnit.test( "#7531 - jQuery.ajax() - Location object as url", function( assert ) {
1944 xhr = jQuery.ajax( {
1945 url: window.location
1952 assert.ok( success, "document.location did not generate exception" );
1955 jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
1956 ajaxTest( "#7578 - jQuery.ajax() - JSONP - default for cache option" + label, 1, function( assert ) {
1958 url: baseURL + "mock.php?action=jsonp",
1960 crossDomain: crossDomain,
1961 beforeSend: function() {
1962 assert.strictEqual( this.cache, false, "cache must be false on JSON request" );
1970 ajaxTest( "#8107 - jQuery.ajax() - multiple method signatures introduced in 1.5", 4, function( assert ) {
1973 create: function() {
1974 return jQuery.ajax();
1977 assert.ok( true, "With no arguments" );
1981 create: function() {
1982 return jQuery.ajax( baseURL + "name.html" );
1985 assert.ok( true, "With only string URL argument" );
1989 create: function() {
1990 return jQuery.ajax( baseURL + "name.html", {} );
1993 assert.ok( true, "With string URL param and map" );
1997 create: function( options ) {
1998 return jQuery.ajax( options );
2000 url: baseURL + "name.html",
2001 success: function() {
2002 assert.ok( true, "With only map" );
2008 jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
2009 ajaxTest( "#8205 - jQuery.ajax() - JSONP - re-use callbacks name" + label, 4, function( assert ) {
2011 url: baseURL + "mock.php?action=jsonp",
2013 crossDomain: crossDomain,
2014 beforeSend: function( jqXHR, s ) {
2015 s.callback = s.jsonpCallback;
2017 assert.ok( this.callback in window, "JSONP callback name is in the window" );
2019 success: function() {
2020 var previous = this;
2023 previous.jsonpCallback,
2025 "jsonpCallback option is set back to default in callbacks"
2029 !( this.callback in window ),
2030 "JSONP callback name was removed from the window"
2034 url: baseURL + "mock.php?action=jsonp",
2036 crossDomain: crossDomain,
2037 beforeSend: function() {
2038 assert.strictEqual( this.jsonpCallback, previous.callback, "JSONP callback name is re-used" );
2047 QUnit.test( "#9887 - jQuery.ajax() - Context with circular references (#9887)", function( assert ) {
2050 var success = false,
2052 context.field = context;
2054 jQuery.ajax( "non-existing", {
2056 beforeSend: function() {
2057 assert.ok( this === context, "context was not deep extended" );
2065 assert.ok( success, "context with circular reference did not generate an exception" );
2068 jQuery.each( [ "as argument", "in settings object" ], function( inSetting, title ) {
2070 function request( assert, url, test ) {
2072 create: function() {
2073 return jQuery.ajax( inSetting ? { url: url } : url );
2076 assert.ok( true, ( test || url ) + " " + title );
2081 ajaxTest( "#10093 - jQuery.ajax() - falsy url " + title, 4, function( assert ) {
2083 request( assert, "", "empty string" ),
2084 request( assert, false ),
2085 request( assert, null ),
2086 request( assert, undefined )
2091 ajaxTest( "#11151 - jQuery.ajax() - parse error body", 2, function( assert ) {
2093 url: url( "mock.php?action=error&json=1" ),
2094 dataFilter: function( string ) {
2095 assert.ok( false, "dataFilter called" );
2098 error: function( jqXHR ) {
2099 assert.strictEqual( jqXHR.responseText, "{ \"code\": 40, \"message\": \"Bad Request\" }", "Error body properly set" );
2100 assert.deepEqual( jqXHR.responseJSON, { code: 40, message: "Bad Request" }, "Error body properly parsed" );
2105 ajaxTest( "#11426 - jQuery.ajax() - loading binary data shouldn't throw an exception in IE", 1, function( assert ) {
2107 url: url( "1x1.jpg" ),
2108 success: function( data ) {
2109 assert.ok( data === undefined || /JFIF/.test( data ), "success callback reached" );
2114 if ( typeof window.ArrayBuffer === "undefined" || typeof new XMLHttpRequest().responseType !== "string" ) {
2116 QUnit.skip( "No ArrayBuffer support in XHR", jQuery.noop );
2119 // No built-in support for binary data, but it's easy to add via a prefilter
2120 jQuery.ajaxPrefilter( "arraybuffer", function( s ) {
2121 s.xhrFields = { responseType: "arraybuffer" };
2122 s.responseFields.arraybuffer = "response";
2123 s.converters[ "binary arraybuffer" ] = true;
2126 ajaxTest( "gh-2498 - jQuery.ajax() - binary data shouldn't throw an exception", 2, function( assert ) {
2128 url: url( "1x1.jpg" ),
2129 dataType: "arraybuffer",
2130 success: function( data, s, jqxhr ) {
2131 assert.ok( data instanceof window.ArrayBuffer, "correct data type" );
2132 assert.ok( jqxhr.response instanceof window.ArrayBuffer, "data in jQXHR" );
2138 QUnit.test( "#11743 - jQuery.ajax() - script, throws exception", function( assert ) {
2140 var done = assert.async();
2141 var onerror = window.onerror;
2142 window.onerror = function() {
2143 assert.ok( true, "Exception thrown" );
2144 window.onerror = onerror;
2148 url: baseURL + "badjson.js",
2154 jQuery.each( [ "method", "type" ], function( _, globalOption ) {
2155 function request( assert, option ) {
2157 url: url( "mock.php?action=echoData" ),
2159 success: function( msg ) {
2160 assert.strictEqual( msg, "hello", "Check for POST (no override)" );
2164 options[ option ] = "GET";
2165 options.success = function( msg ) {
2166 assert.strictEqual( msg, "", "Check for no POST (overriding with " + option + ")" );
2173 "#12004 - jQuery.ajax() - method is an alias of type - " +
2174 globalOption + " set globally", 3,
2175 function( assert ) {
2179 options[ globalOption ] = "POST";
2180 jQuery.ajaxSetup( options );
2183 request( assert, "type" ),
2184 request( assert, "method" ),
2192 ajaxTest( "#13276 - jQuery.ajax() - compatibility between XML documents from ajax requests and parsed string", 1, function( assert ) {
2194 url: baseURL + "dashboard.xml",
2196 success: function( ajaxXML ) {
2197 var parsedXML = jQuery( jQuery.parseXML( "<tab title=\"Added\">blibli</tab>" ) ).find( "tab" );
2198 ajaxXML = jQuery( ajaxXML );
2200 ajaxXML.find( "infowindowtab" ).append( parsedXML );
2202 assert.strictEqual( e, undefined, "error" );
2205 assert.strictEqual( ajaxXML.find( "tab" ).length, 3, "Parsed node was added properly" );
2210 ajaxTest( "#13292 - jQuery.ajax() - converter is bypassed for 204 requests", 3, function( assert ) {
2212 url: baseURL + "mock.php?action=status&code=204&text=No+Content",
2213 dataType: "testing",
2215 "* testing": function() {
2216 throw "converter was called";
2219 success: function( data, status, jqXHR ) {
2220 assert.strictEqual( jqXHR.status, 204, "status code is 204" );
2221 assert.strictEqual( status, "nocontent", "status text is 'nocontent'" );
2222 assert.strictEqual( data, undefined, "data is undefined" );
2224 error: function( _, status, error ) {
2225 assert.ok( false, "error" );
2226 assert.strictEqual( status, "parsererror", "Parser Error" );
2227 assert.strictEqual( error, "converter was called", "Converter was called" );
2232 ajaxTest( "#13388 - jQuery.ajax() - responseXML", 3, function( assert ) {
2234 url: url( "with_fries.xml" ),
2236 success: function( resp, _, jqXHR ) {
2237 assert.notStrictEqual( resp, undefined, "XML document exists" );
2238 assert.ok( "responseXML" in jqXHR, "jqXHR.responseXML exists" );
2239 assert.strictEqual( resp, jqXHR.responseXML, "jqXHR.responseXML is set correctly" );
2244 ajaxTest( "#13922 - jQuery.ajax() - converter is bypassed for HEAD requests", 3, function( assert ) {
2246 url: baseURL + "mock.php?action=json",
2252 "text json": function() {
2253 throw "converter was called";
2256 success: function( data, status ) {
2257 assert.ok( true, "success" );
2258 assert.strictEqual( status, "nocontent", "data is undefined" );
2259 assert.strictEqual( data, undefined, "data is undefined" );
2261 error: function( _, status, error ) {
2262 assert.ok( false, "error" );
2263 assert.strictEqual( status, "parsererror", "Parser Error" );
2264 assert.strictEqual( error, "converter was called", "Converter was called" );
2269 // Chrome 78 dropped support for synchronous XHR requests inside of
2270 // beforeunload, unload, pagehide, and visibilitychange event handlers.
2271 // See https://bugs.chromium.org/p/chromium/issues/detail?id=952452
2272 // Safari 13 did similar changes. The below check will catch them both.
2273 if ( !/safari/i.test( navigator.userAgent ) ) {
2275 "#14379 - jQuery.ajax() on unload",
2276 "ajax/onunload.html",
2277 function( assert, jQuery, window, document, status ) {
2279 assert.strictEqual( status, "success", "Request completed" );
2284 ajaxTest( "#14683 - jQuery.ajax() - Exceptions thrown synchronously by xhr.send should be caught", 4, function( assert ) {
2286 url: baseURL + "mock.php?action=echoData",
2289 toString: function() {
2290 throw "Can't parse";
2294 done: function( data ) {
2295 assert.ok( false, "done: " + data );
2297 fail: function( jqXHR, status, error ) {
2298 assert.ok( true, "exception caught: " + error );
2299 assert.strictEqual( jqXHR.status, 0, "proper status code" );
2300 assert.strictEqual( status, "error", "proper status" );
2303 url: "https://" + externalHost + ":80q",
2304 done: function( data ) {
2305 assert.ok( false, "done: " + data );
2307 fail: function( _, status, error ) {
2308 assert.ok( true, "fail: " + status + " - " + error );
2313 ajaxTest( "gh-2587 - when content-type not xml, but looks like one", 1, function( assert ) {
2315 url: url( "mock.php?action=contentType" ),
2317 contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
2318 "response": "<test/>"
2320 success: function( result ) {
2324 "Should handle it as a string, not xml"
2330 ajaxTest( "gh-2587 - when content-type not xml, but looks like one", 1, function( assert ) {
2332 url: url( "mock.php?action=contentType" ),
2334 contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
2335 "response": "<test/>"
2337 success: function( result ) {
2341 "Should handle it as a string, not xml"
2347 ajaxTest( "gh-2587 - when content-type not json, but looks like one", 1, function( assert ) {
2349 url: url( "mock.php?action=contentType" ),
2351 contentType: "test/jsontest",
2352 "response": JSON.stringify( { test: "test" } )
2354 success: function( result ) {
2358 "Should handle it as a string, not json"
2364 ajaxTest( "gh-2587 - when content-type not html, but looks like one", 1, function( assert ) {
2366 url: url( "mock.php?action=contentType" ),
2368 contentType: "test/htmltest",
2369 "response": "<p>test</p>"
2371 success: function( result ) {
2375 "Should handle it as a string, not html"
2381 ajaxTest( "gh-2587 - when content-type not javascript, but looks like one", 1, function( assert ) {
2383 url: url( "mock.php?action=contentType" ),
2385 contentType: "test/testjavascript",
2386 "response": "alert(1)"
2388 success: function( result ) {
2392 "Should handle it as a string, not javascript"
2398 ajaxTest( "gh-2587 - when content-type not ecmascript, but looks like one", 1, function( assert ) {
2400 url: url( "mock.php?action=contentType" ),
2402 contentType: "test/testjavascript",
2403 "response": "alert(1)"
2405 success: function( result ) {
2409 "Should handle it as a string, not ecmascript"
2415 //----------- jQuery.ajaxPrefilter()
2417 ajaxTest( "jQuery.ajaxPrefilter() - abort", 1, function( assert ) {
2422 // Ensure prefix does not throw an error
2423 jQuery.ajaxPrefilter( "+prefix", function( options, _, jqXHR ) {
2424 if ( options.abortInPrefilter ) {
2429 abortInPrefilter: true,
2431 assert.ok( false, "error callback called" );
2433 fail: function( _, reason ) {
2434 assert.strictEqual( reason, "canceled", "Request aborted by the prefilter must fail with 'canceled' status text" );
2439 //----------- jQuery.ajaxSetup()
2441 QUnit.test( "jQuery.ajaxSetup()", function( assert ) {
2443 var done = assert.async();
2445 url: url( "mock.php?action=name&name=foo" ),
2446 success: function( msg ) {
2447 assert.strictEqual( msg, "bar", "Check for GET" );
2454 QUnit.test( "jQuery.ajaxSetup({ timeout: Number }) - with global timeout", function( assert ) {
2456 var done = assert.async();
2459 assert.ok( passed++ < 2, "Error callback executed" );
2460 if ( passed === 2 ) {
2461 jQuery( document ).off( "ajaxError.setupTest" );
2465 fail = function( a, b ) {
2466 assert.ok( false, "Check for timeout failed " + a + " " + b );
2470 jQuery( document ).on( "ajaxError.setupTest", pass );
2478 url: url( "mock.php?action=wait&wait=5" ),
2484 QUnit.test( "jQuery.ajaxSetup({ timeout: Number }) with localtimeout", function( assert ) {
2486 var done = assert.async();
2493 url: url( "mock.php?action=wait&wait=1" ),
2495 assert.ok( false, "Check for local timeout failed" );
2498 success: function() {
2499 assert.ok( true, "Check for local timeout" );
2505 //----------- jQuery.domManip()
2507 QUnit.test( "#11264 - jQuery.domManip() - no side effect because of ajaxSetup or global events", function( assert ) {
2514 jQuery( document ).on( "ajaxStart ajaxStop", function() {
2515 assert.ok( false, "Global event triggered" );
2518 jQuery( "#qunit-fixture" ).append( "<script src='" + baseURL + "mock.php?action=script'></script>" );
2520 jQuery( document ).off( "ajaxStart ajaxStop" );
2524 "jQuery#load() - always use GET method even if it overrided through ajaxSetup (#11264)",
2525 function( assert ) {
2527 var done = assert.async();
2533 jQuery( "#qunit-fixture" ).load( baseURL + "mock.php?action=echoMethod", function( method ) {
2534 assert.equal( method, "GET" );
2541 "jQuery#load() - should resolve with correct context",
2542 function( assert ) {
2544 var done = assert.async();
2545 var ps = jQuery( "<p></p><p></p>" );
2548 ps.appendTo( "#qunit-fixture" );
2550 ps.load( baseURL + "mock.php?action=echoMethod", function() {
2551 assert.strictEqual( this, ps[ i++ ] );
2561 "#11402 - jQuery.domManip() - script in comments are properly evaluated",
2562 function( assert ) {
2564 jQuery( "#qunit-fixture" ).load( baseURL + "cleanScript.html", assert.async() );
2568 //----------- jQuery.get()
2570 QUnit.test( "jQuery.get( String, Hash, Function ) - parse xml and use text() on nodes", function( assert ) {
2572 var done = assert.async();
2573 jQuery.get( url( "dashboard.xml" ), function( xml ) {
2575 jQuery( "tab", xml ).each( function() {
2576 content.push( jQuery( this ).text() );
2578 assert.strictEqual( content[ 0 ], "blabla", "Check first tab" );
2579 assert.strictEqual( content[ 1 ], "blublu", "Check second tab" );
2584 QUnit.test( "#8277 - jQuery.get( String, Function ) - data in ajaxSettings", function( assert ) {
2586 var done = assert.async();
2590 jQuery.get( url( "mock.php?action=echoQuery" ), function( data ) {
2591 assert.ok( /helloworld$/.test( data ), "Data from ajaxSettings was used" );
2596 //----------- jQuery.getJSON()
2598 QUnit.test( "jQuery.getJSON( String, Hash, Function ) - JSON array", function( assert ) {
2600 var done = assert.async();
2602 url( "mock.php?action=json" ),
2607 assert.ok( json.length >= 2, "Check length" );
2608 assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" );
2609 assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" );
2610 assert.strictEqual( json[ 1 ][ "name" ], "Peter", "Check JSON: second, name" );
2611 assert.strictEqual( json[ 1 ][ "age" ], 25, "Check JSON: second, age" );
2617 QUnit.test( "jQuery.getJSON( String, Function ) - JSON object", function( assert ) {
2619 var done = assert.async();
2620 jQuery.getJSON( url( "mock.php?action=json" ), function( json ) {
2621 if ( json && json[ "data" ] ) {
2622 assert.strictEqual( json[ "data" ][ "lang" ], "en", "Check JSON: lang" );
2623 assert.strictEqual( json[ "data" ].length, 25, "Check JSON: length" );
2629 QUnit.test( "jQuery.getJSON( String, Function ) - JSON object with absolute url to local content", function( assert ) {
2631 var done = assert.async();
2632 var absoluteUrl = url( "mock.php?action=json" );
2634 // Make a relative URL absolute relative to the document location
2635 if ( !/^[a-z][a-z0-9+.-]*:/i.test( absoluteUrl ) ) {
2637 // An absolute path replaces everything after the host
2638 if ( absoluteUrl.charAt( 0 ) === "/" ) {
2639 absoluteUrl = window.location.href.replace( /(:\/*[^/]*).*$/, "$1" ) + absoluteUrl;
2641 // A relative path replaces the last slash-separated path segment
2643 absoluteUrl = window.location.href.replace( /[^/]*$/, "" ) + absoluteUrl;
2647 jQuery.getJSON( absoluteUrl, function( json ) {
2648 assert.strictEqual( json.data.lang, "en", "Check JSON: lang" );
2649 assert.strictEqual( json.data.length, 25, "Check JSON: length" );
2654 //----------- jQuery.getScript()
2656 QUnit.test( "jQuery.getScript( String, Function ) - with callback",
2657 function( assert ) {
2659 var done = assert.async();
2661 Globals.register( "testBar" );
2662 jQuery.getScript( url( "mock.php?action=testbar" ), function() {
2663 assert.strictEqual( window[ "testBar" ], "bar", "Check if script was evaluated" );
2669 QUnit.test( "jQuery.getScript( String, Function ) - no callback", function( assert ) {
2671 Globals.register( "testBar" );
2672 jQuery.getScript( url( "mock.php?action=testbar" ) ).done( assert.async() );
2675 QUnit.test( "#8082 - jQuery.getScript( String, Function ) - source as responseText", function( assert ) {
2677 var done = assert.async();
2679 Globals.register( "testBar" );
2680 jQuery.getScript( url( "mock.php?action=testbar" ), function( data, _, jqXHR ) {
2681 assert.strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script" );
2686 QUnit.test( "jQuery.getScript( Object ) - with callback", function( assert ) {
2688 var done = assert.async();
2690 Globals.register( "testBar" );
2692 url: url( "mock.php?action=testbar" ),
2693 success: function() {
2694 assert.strictEqual( window[ "testBar" ], "bar", "Check if script was evaluated" );
2700 QUnit.test( "jQuery.getScript( Object ) - no callback", function( assert ) {
2702 Globals.register( "testBar" );
2703 jQuery.getScript( { url: url( "mock.php?action=testbar" ) } ).done( assert.async() );
2706 // //----------- jQuery.fn.load()
2708 // check if load can be called with only url
2709 QUnit.test( "jQuery.fn.load( String )", function( assert ) {
2712 beforeSend: function() {
2713 assert.strictEqual( this.type, "GET", "no data means GET request" );
2716 jQuery( "#first" ).load( baseURL + "name.html", assert.async() );
2719 QUnit.test( "jQuery.fn.load() - 404 error callbacks", function( assert ) {
2721 var done = assert.async();
2723 addGlobalEvents( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError", assert )();
2724 jQuery( document ).on( "ajaxStop", done );
2725 jQuery( "<div></div>" ).load( baseURL + "404.txt", function() {
2726 assert.ok( true, "complete" );
2730 // check if load can be called with url and null data
2731 QUnit.test( "jQuery.fn.load( String, null )", function( assert ) {
2734 beforeSend: function() {
2735 assert.strictEqual( this.type, "GET", "no data means GET request" );
2738 jQuery( "#first" ).load( baseURL + "name.html", null, assert.async() );
2741 // check if load can be called with url and undefined data
2742 QUnit.test( "jQuery.fn.load( String, undefined )", function( assert ) {
2745 beforeSend: function() {
2746 assert.strictEqual( this.type, "GET", "no data means GET request" );
2749 jQuery( "#first" ).load( baseURL + "name.html", undefined, assert.async() );
2752 // check if load can be called with only url
2753 QUnit.test( "jQuery.fn.load( URL_SELECTOR )", function( assert ) {
2755 var done = assert.async();
2756 jQuery( "#first" ).load( baseURL + "test3.html div.user", function() {
2757 assert.strictEqual( jQuery( this ).children( "div" ).length, 2, "Verify that specific elements were injected" );
2762 // Selector should be trimmed to avoid leading spaces (#14773)
2763 QUnit.test( "jQuery.fn.load( URL_SELECTOR with spaces )", function( assert ) {
2765 var done = assert.async();
2766 jQuery( "#first" ).load( baseURL + "test3.html #superuser ", function() {
2767 assert.strictEqual( jQuery( this ).children( "div" ).length, 1, "Verify that specific elements were injected" );
2772 // Selector should be trimmed to avoid leading spaces (#14773)
2773 // Selector should include any valid non-HTML whitespace (#3003)
2774 QUnit.test( "jQuery.fn.load( URL_SELECTOR with non-HTML whitespace(#3003) )", function( assert ) {
2776 var done = assert.async();
2777 jQuery( "#first" ).load( baseURL + "test3.html #whitespace\\\\xA0 ", function() {
2778 assert.strictEqual( jQuery( this ).children( "div" ).length, 1, "Verify that specific elements were injected" );
2783 QUnit.test( "jQuery.fn.load( String, Function ) - simple: inject text into DOM", function( assert ) {
2785 var done = assert.async();
2786 jQuery( "#first" ).load( url( "name.html" ), function() {
2787 assert.ok( /^ERROR/.test( jQuery( "#first" ).text() ), "Check if content was injected into the DOM" );
2792 QUnit.test( "jQuery.fn.load( String, Function ) - check scripts", function( assert ) {
2794 var done = assert.async();
2795 var verifyEvaluation = function() {
2796 assert.strictEqual( window[ "testBar" ], "bar", "Check if script src was evaluated after load" );
2797 assert.strictEqual( jQuery( "#ap" ).html(), "bar", "Check if script evaluation has modified DOM" );
2801 Globals.register( "testFoo" );
2802 Globals.register( "testBar" );
2804 jQuery( "#first" ).load( url( "mock.php?action=testHTML&baseURL=" + baseURL ), function() {
2805 assert.ok( jQuery( "#first" ).html().match( /^html text/ ), "Check content after loading html" );
2806 assert.strictEqual( jQuery( "#foo" ).html(), "foo", "Check if script evaluation has modified DOM" );
2807 assert.strictEqual( window[ "testFoo" ], "foo", "Check if script was evaluated after load" );
2808 setTimeout( verifyEvaluation, 600 );
2812 QUnit.test( "jQuery.fn.load( String, Function ) - check file with only a script tag", function( assert ) {
2814 var done = assert.async();
2815 Globals.register( "testFoo" );
2817 jQuery( "#first" ).load( url( "test2.html" ), function() {
2818 assert.strictEqual( jQuery( "#foo" ).html(), "foo", "Check if script evaluation has modified DOM" );
2819 assert.strictEqual( window[ "testFoo" ], "foo", "Check if script was evaluated after load" );
2824 QUnit.test( "jQuery.fn.load( String, Function ) - dataFilter in ajaxSettings", function( assert ) {
2826 var done = assert.async();
2828 dataFilter: function() {
2829 return "Hello World";
2832 jQuery( "<div></div>" ).load( url( "name.html" ), function( responseText ) {
2833 assert.strictEqual( jQuery( this ).html(), "Hello World", "Test div was filled with filtered data" );
2834 assert.strictEqual( responseText, "Hello World", "Test callback receives filtered data" );
2839 QUnit.test( "jQuery.fn.load( String, Object, Function )", function( assert ) {
2841 var done = assert.async();
2842 jQuery( "<div></div>" ).load( url( "mock.php?action=echoHtml" ), {
2845 var $node = jQuery( this );
2846 assert.strictEqual( $node.find( "#method" ).text(), "POST", "Check method" );
2847 assert.strictEqual( $node.find( "#data" ).text(), "bar=ok", "Check if data is passed correctly" );
2852 QUnit.test( "jQuery.fn.load( String, String, Function )", function( assert ) {
2854 var done = assert.async();
2856 jQuery( "<div></div>" ).load( url( "mock.php?action=echoHtml" ), "foo=3&bar=ok", function() {
2857 var $node = jQuery( this );
2858 assert.strictEqual( $node.find( "#method" ).text(), "GET", "Check method" );
2859 assert.ok( $node.find( "#query" ).text().match( /foo=3&bar=ok/ ), "Check if a string of data is passed correctly" );
2864 QUnit.test( "jQuery.fn.load() - callbacks get the correct parameters", function( assert ) {
2866 var completeArgs = {},
2867 done = assert.async();
2870 success: function( _, status, jqXHR ) {
2871 completeArgs[ this.url ] = [ jqXHR.responseText, status, jqXHR ];
2873 error: function( jqXHR, status ) {
2874 completeArgs[ this.url ] = [ jqXHR.responseText, status, jqXHR ];
2883 url: baseURL + "mock.php?action=echoQuery&arg=pop"
2887 url: baseURL + "404.txt"
2890 function( options ) {
2891 return jQuery.Deferred( function( defer ) {
2892 jQuery( "#foo" ).load( options.url, function() {
2893 var args = arguments;
2894 assert.strictEqual( completeArgs[ options.url ].length, args.length, "same number of arguments (" + options.type + ")" );
2895 jQuery.each( completeArgs[ options.url ], function( i, value ) {
2896 assert.strictEqual( args[ i ], value, "argument #" + i + " is the same (" + options.type + ")" );
2905 QUnit.test( "#2046 - jQuery.fn.load( String, Function ) with ajaxSetup on dataType json", function( assert ) {
2907 var done = assert.async();
2912 jQuery( document ).on( "ajaxComplete", function( e, xml, s ) {
2913 assert.strictEqual( s.dataType, "html", "Verify the load() dataType was html" );
2914 jQuery( document ).off( "ajaxComplete" );
2917 jQuery( "#first" ).load( baseURL + "test3.html" );
2920 QUnit.test( "#10524 - jQuery.fn.load() - data specified in ajaxSettings is merged in", function( assert ) {
2922 var done = assert.async();
2932 jQuery( "#foo" ).load( baseURL + "mock.php?action=echoQuery", data );
2933 jQuery( document ).on( "ajaxComplete", function( event, jqXHR, options ) {
2934 assert.ok( ~options.data.indexOf( "foo=bar" ), "Data from ajaxSettings was used" );
2939 // //----------- jQuery.post()
2941 QUnit.test( "jQuery.post() - data", function( assert ) {
2943 var done = assert.async();
2947 url( "mock.php?action=xml" ),
2952 jQuery( "math", xml ).each( function() {
2953 assert.strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
2954 assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
2959 url: url( "mock.php?action=echoData" ),
2967 success: function( data ) {
2968 assert.strictEqual( data, "test%5Blength%5D=7&test%5Bfoo%5D=bar", "Check if a sub-object with a length param is serialized correctly" );
2974 QUnit.test( "jQuery.post( String, Hash, Function ) - simple with xml", function( assert ) {
2976 var done = assert.async();
2980 url( "mock.php?action=xml" ),
2985 jQuery( "math", xml ).each( function() {
2986 assert.strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
2987 assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
2991 jQuery.post( url( "mock.php?action=xml&cal=5-2" ), {}, function( xml ) {
2992 jQuery( "math", xml ).each( function() {
2993 assert.strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
2994 assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
2997 ).always( function() {
3002 QUnit.test( "jQuery[get|post]( options ) - simple with xml", function( assert ) {
3004 var done = assert.async();
3006 jQuery.when.apply( jQuery,
3007 jQuery.map( [ "get", "post" ], function( method ) {
3008 return jQuery[ method ]( {
3009 url: url( "mock.php?action=xml" ),
3013 success: function( xml ) {
3014 jQuery( "math", xml ).each( function() {
3015 assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
3020 ).always( function() {
3025 //----------- jQuery.active
3027 QUnit.test( "jQuery.active", function( assert ) {
3029 assert.ok( jQuery.active === 0, "ajax active counter should be zero: " + jQuery.active );