@@ -513,10 +513,10 @@ describe('#db IAMAuth Plugin', function() {
513513 } ) ;
514514
515515 it ( 'throws error for unspecified IAM API key' , function ( ) {
516- var cloudantClient = new Client ( { plugins : 'iamauth' } ) ;
517516 assert . throws (
518517 ( ) => {
519- cloudantClient . request ( { url : SERVER + DBNAME } ) ;
518+ /* eslint-disable no-new */
519+ new Client ( { plugins : 'iamauth' } ) ;
520520 } ,
521521 / M i s s i n g I A M A P I k e y f r o m c o n f i g u r a t i o n / ,
522522 'did not throw with expected message'
@@ -557,4 +557,51 @@ describe('#db IAMAuth Plugin', function() {
557557 assert . fail ( `Unexpected reject: ${ err } ` ) ;
558558 } ) ;
559559 } ) ;
560+
561+ it ( 'successfully retries request on 500 IAM token service response and returns 200 response' , function ( done ) {
562+ if ( process . env . NOCK_OFF ) {
563+ this . skip ( ) ;
564+ }
565+
566+ var iamMocks = nock ( TOKEN_SERVER )
567+ . post ( '/identity/token' , {
568+ 'grant_type' : 'urn:ibm:params:oauth:grant-type:apikey' ,
569+ 'response_type' : 'cloud_iam' ,
570+ 'apikey' : IAM_API_KEY
571+ } )
572+ . times ( 2 )
573+ . reply ( 500 , { error : 'internal_server_error' , reason : 'Internal Server Error' } )
574+ . post ( '/identity/token' , {
575+ 'grant_type' : 'urn:ibm:params:oauth:grant-type:apikey' ,
576+ 'response_type' : 'cloud_iam' ,
577+ 'apikey' : IAM_API_KEY
578+ } )
579+ . reply ( 200 , MOCK_IAM_TOKEN_RESPONSE ) ;
580+
581+ var cloudantMocks = nock ( SERVER )
582+ . post ( '/_iam_session' , { access_token : MOCK_ACCESS_TOKEN } )
583+ . reply ( 200 , { ok : true } , MOCK_SET_IAM_SESSION_HEADER )
584+ . get ( DBNAME )
585+ . reply ( 200 , { doc_count : 0 } ) ;
586+
587+ var cloudantClient = new Client ( { maxAttempt : 3 , plugins : { iamauth : { iamApiKey : IAM_API_KEY } } } ) ;
588+ var req = { url : SERVER + DBNAME , method : 'GET' } ;
589+
590+ var startTs = ( new Date ( ) ) . getTime ( ) ;
591+
592+ cloudantClient . request ( req , function ( err , resp , data ) {
593+ assert . equal ( err , null ) ;
594+ assert . equal ( resp . request . headers . cookie , MOCK_IAM_SESSION ) ;
595+ assert . equal ( resp . statusCode , 200 ) ;
596+ assert . ok ( data . indexOf ( '"doc_count":0' ) > - 1 ) ;
597+
598+ // validate retry delay
599+ var now = ( new Date ( ) ) . getTime ( ) ;
600+ assert . ok ( now - startTs > ( 500 + 1000 ) ) ;
601+
602+ iamMocks . done ( ) ;
603+ cloudantMocks . done ( ) ;
604+ done ( ) ;
605+ } ) ;
606+ } ) ;
560607} ) ;
0 commit comments