@@ -285,20 +285,20 @@ if(typeof XDomainRequest === 'undefined') {
285285 QUnit . test ( "cross domain post request should change data to form data (#90)" , function ( assert ) {
286286 var done = assert . async ( ) ;
287287 var headers = { } ,
288- restore = makeFixture ( function ( ) {
289- this . open = function ( type , url ) { } ;
290-
291- this . send = function ( ) {
292- this . readyState = 4 ;
293- this . status = 204 ;
294- this . responseText = '' ;
295- this . onreadystatechange ( ) ;
296- } ;
297-
298- this . setRequestHeader = function ( header , value ) {
299- headers [ header ] = value ;
300- } ;
301- } ) ;
288+ restore = makeFixture ( function ( ) {
289+ this . open = function ( type , url ) { } ;
290+
291+ this . send = function ( ) {
292+ this . readyState = 4 ;
293+ this . status = 204 ;
294+ this . responseText = '' ;
295+ this . onreadystatechange ( ) ;
296+ } ;
297+
298+ this . setRequestHeader = function ( header , value ) {
299+ headers [ header ] = value ;
300+ } ;
301+ } ) ;
302302 ajax ( {
303303 type : "POST" ,
304304 url : "https://httpbin.org/post" ,
@@ -569,23 +569,71 @@ QUnit.test("It doesn't stringify FormData", function(assert) {
569569 } ) ;
570570} ) ;
571571
572+ QUnit . test ( "abort" , function ( assert ) {
573+ var done = assert . async ( ) ;
574+ var restore = makeFixture ( function ( ) {
575+ var aborted = false ;
576+ this . open = function ( type , url ) { } ;
577+ this . setRequestHeader = function ( header , value ) { } ;
578+ this . send = function ( ) { } ;
579+ this . abort = function ( ) {
580+ assert . ok ( true , 'called the underlying XHR.abort' ) ;
581+ done ( ) ;
582+ } ;
583+ } ) ;
584+
585+ var request = ajax ( {
586+ url : "/foo"
587+ } ) ;
588+
589+ request . abort ( ) ;
590+ } ) ;
591+
592+ QUnit . test ( "abort prevents sending if beforeSend is not finished" , function ( assert ) {
593+ var done = assert . async ( ) ;
594+ var restore = makeFixture ( function ( ) {
595+ var aborted = false ;
596+ this . open = function ( type , url ) { } ;
597+ this . setRequestHeader = function ( header , value ) { } ;
598+ this . abort = function ( ) {
599+ assert . ok ( true , 'XHR abort was called' ) ;
600+ } ;
601+ this . send = function ( ) {
602+ assert . notOk ( true , 'should not have been called' ) ;
603+ } ;
604+ } ) ;
605+
606+ var request = ajax ( {
607+ url : "/foo" ,
608+ beforeSend : function ( xhr ) {
609+ return new Promise ( resolve => {
610+ setTimeout ( resolve , 1 ) ;
611+ } ) ;
612+ }
613+ } ) ;
614+
615+ request . abort ( ) ;
616+
617+ setTimeout ( done , 10 ) ;
618+ } ) ;
619+
572620QUnit . test ( "beforeSend" , function ( assert ) {
573621 var done = assert . async ( ) ;
574622 var headers = { } ,
575- restore = makeFixture ( function ( ) {
576- this . open = function ( type , url ) { } ;
623+ restore = makeFixture ( function ( ) {
624+ this . open = function ( type , url ) { } ;
577625
578- this . send = function ( ) {
579- this . readyState = 4 ;
580- this . status = 204 ;
581- this . responseText = '' ;
582- this . onreadystatechange ( ) ;
583- } ;
626+ this . send = function ( ) {
627+ this . readyState = 4 ;
628+ this . status = 204 ;
629+ this . responseText = '' ;
630+ this . onreadystatechange ( ) ;
631+ } ;
584632
585- this . setRequestHeader = function ( header , value ) {
586- headers [ header ] = value ;
587- } ;
588- } ) ;
633+ this . setRequestHeader = function ( header , value ) {
634+ headers [ header ] = value ;
635+ } ;
636+ } ) ;
589637
590638 ajax ( {
591639 type : "post" ,
@@ -612,116 +660,65 @@ QUnit.test("beforeSend", function (assert) {
612660 } ) ;
613661} ) ;
614662
615- QUnit . test ( "abort " , function ( assert ) {
663+ QUnit . test ( "beforeSend async " , function ( assert ) {
616664 var done = assert . async ( ) ;
665+ var headers = { } ;
617666 var restore = makeFixture ( function ( ) {
618- var aborted = false ;
619- this . open = function ( type , url ) { } ;
620- this . setRequestHeader = function ( header , value ) { } ;
621- this . send = function ( ) { } ;
622- this . abort = function ( ) {
623- assert . ok ( true , 'called the underlying XHR.abort' ) ;
624- done ( ) ;
625- } ;
626- } ) ;
667+ this . open = function ( type , url ) { } ;
627668
628- var request = ajax ( {
629- url : "/foo"
630- } ) ;
631-
632- request . abort ( ) ;
633- } ) ;
669+ this . send = function ( ) {
670+ this . readyState = 4 ;
671+ this . status = 204 ;
672+ this . responseText = '' ;
673+ this . onreadystatechange ( ) ;
674+ } ;
634675
635- QUnit . test ( "abort prevents sending if beforeSend is not finished" , function ( assert ) {
636- var done = assert . async ( ) ;
637- var restore = makeFixture ( function ( ) {
638- var aborted = false ;
639- this . open = function ( type , url ) { } ;
640- this . setRequestHeader = function ( header , value ) { } ;
641- this . abort = function ( ) {
642- assert . ok ( true , 'XHR abort was called' ) ;
643- } ;
644- this . send = function ( ) {
645- assert . notOk ( true , 'should not have been called' ) ;
646- } ;
647- } ) ;
676+ this . setRequestHeader = function ( header , value ) {
677+ headers [ header ] = value ;
678+ } ;
679+ } ) ;
648680
649- var request = ajax ( {
681+ ajax ( {
650682 url : "/foo" ,
651683 beforeSend : function ( xhr ) {
652684 return new Promise ( resolve => {
653- setTimeout ( resolve , 1 ) ;
685+ setTimeout ( ( ) => {
686+ xhr . setRequestHeader ( "Authorization" , "Bearer 123" ) ;
687+ resolve ( ) ;
688+ } , 1 ) ;
654689 } ) ;
655690 }
656- } ) ;
657-
658- request . abort ( ) ;
659-
660- setTimeout ( done , 10 ) ;
691+ } ) . then ( function ( value ) {
692+ assert . ok ( headers . hasOwnProperty ( 'Authorization' ) , "authorization header set" ) ;
693+ } , function ( reason ) {
694+ assert . notOk ( reason , "request failed with reason = " , reason ) ;
695+ } ) . then ( done ) ;
661696} ) ;
662697
663- QUnit . module ( "beforeSend async" , function ( hooks ) {
664- var headers , restore ;
665-
666- hooks . beforeEach ( function ( ) {
667- headers = { } ;
668- restore = makeFixture ( function ( ) {
669- this . open = function ( type , url ) { } ;
670-
671- this . send = function ( ) {
672- this . readyState = 4 ;
673- this . status = 204 ;
674- this . responseText = '' ;
675- this . onreadystatechange ( ) ;
676- } ;
677-
678- this . setRequestHeader = function ( header , value ) {
679- headers [ header ] = value ;
680- } ;
681- } ) ;
682- } ) ;
683-
684- hooks . afterEach ( function ( ) {
685- restore ( ) ;
686- } ) ;
687-
688- QUnit . test ( "waits for promise to resolve" , function ( assert ) {
689- var done = assert . async ( ) ;
690-
691- ajax ( {
692- url : "/foo" ,
693- beforeSend : function ( xhr ) {
694- return new Promise ( resolve => {
695- setTimeout ( ( ) => {
696- xhr . setRequestHeader ( "Authorization" , "Bearer 123" ) ;
697- resolve ( ) ;
698- } , 1 ) ;
699- } ) ;
700- }
701- } ) . then ( function ( value ) {
702- assert . ok ( headers . hasOwnProperty ( 'Authorization' ) , "authorization header set" ) ;
703- } , function ( reason ) {
704- assert . notOk ( reason , "request failed with reason = " , reason ) ;
705- } ) . then ( done ) ;
698+ QUnit . test ( "beforeSend rejects the ajax promise on failure" , function ( assert ) {
699+ var done = assert . async ( ) ;
700+ var error = new Error ( ) ;
701+ var restore = makeFixture ( function ( ) {
702+ this . open = function ( type , url ) { } ;
703+ this . send = function ( ) {
704+ assert . notOk ( true , 'Should not be called' ) ;
705+ } ;
706+ this . setRequestHeader = function ( header , value ) { } ;
706707 } ) ;
707708
708- QUnit . test ( "rejects the ajax promise on failure" , function ( assert ) {
709- var done = assert . async ( ) ;
710- var error = new Error ( ) ;
711-
712- ajax ( {
713- url : "/foo" ,
714- beforeSend : function ( xhr ) {
715- return new Promise ( ( resolve , reject ) => {
716- setTimeout ( ( ) => {
717- reject ( error ) ;
718- } , 1 ) ;
719- } ) ;
720- }
721- } ) . then ( function ( value ) {
722- assert . notOk ( true , "request should have rejected" ) ;
723- } , function ( reason ) {
724- assert . ok ( true , "request rejected" ) ;
725- } ) . then ( done ) ;
726- } ) ;
709+ ajax ( {
710+ url : "/foo" ,
711+ beforeSend : function ( xhr ) {
712+ return new Promise ( ( resolve , reject ) => {
713+ setTimeout ( ( ) => {
714+ reject ( error ) ;
715+ } , 1 ) ;
716+ } ) ;
717+ }
718+ } ) . then ( function ( value ) {
719+ assert . notOk ( true , "request should have rejected" ) ;
720+ } , function ( reason ) {
721+ assert . ok ( true , "request rejected" ) ;
722+ assert . equal ( reason , error , "error is what we expect" ) ;
723+ } ) . then ( done ) ;
727724} ) ;
0 commit comments