@@ -385,6 +385,8 @@ describe('processStreamResponse', () => {
385385
386386describe ( 'ApiClient' , ( ) => {
387387 describe ( 'constructor' , ( ) => {
388+ jasmine . DEFAULT_TIMEOUT_INTERVAL = 60000 ; // 60 seconds.
389+
388390 it ( 'should initialize with provided values' , ( ) => {
389391 const client = new ApiClient ( {
390392 auth : new FakeAuth ( ) ,
@@ -721,6 +723,94 @@ describe('ApiClient', () => {
721723 expect ( headers [ 'x-goog-api-client' ] ) . toContain ( 'google-genai-sdk/' ) ;
722724 expect ( client . getApiVersion ( ) ) . toBe ( 'v1beta1' ) ;
723725 } ) ;
726+
727+ it ( 'should retry requests if retry options are set' , async ( ) => {
728+ const client = new ApiClient ( {
729+ auth : new FakeAuth ( ) ,
730+ project : 'vertex-project' ,
731+ location : 'vertex-location' ,
732+ vertexai : true ,
733+ apiVersion : 'v1beta1' ,
734+ httpOptions : {
735+ retryOptions : {
736+ attempts : 2 ,
737+ } ,
738+ } ,
739+ uploader : new CrossUploader ( ) ,
740+ downloader : new CrossDownloader ( ) ,
741+ } ) ;
742+ const fetchSpy = spyOn ( global , 'fetch' ) . and . returnValue (
743+ Promise . resolve (
744+ new Response (
745+ JSON . stringify ( { 'error' : 'Internal Server Error' } ) ,
746+ fetch500Options ,
747+ ) ,
748+ ) ,
749+ ) ;
750+ await client
751+ . request ( { path : 'test-path' , httpMethod : 'POST' } )
752+ . catch ( ( e ) => {
753+ console . log ( e ) ;
754+ } ) ;
755+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 2 ) ;
756+ } ) ;
757+
758+ it ( 'should not retry requests if retry options are not set' , async ( ) => {
759+ const client = new ApiClient ( {
760+ auth : new FakeAuth ( ) ,
761+ project : 'vertex-project' ,
762+ location : 'vertex-location' ,
763+ vertexai : true ,
764+ apiVersion : 'v1beta1' ,
765+ uploader : new CrossUploader ( ) ,
766+ downloader : new CrossDownloader ( ) ,
767+ } ) ;
768+ const fetchSpy = spyOn ( global , 'fetch' ) . and . returnValue (
769+ Promise . resolve (
770+ new Response (
771+ JSON . stringify ( { 'error' : 'Internal Server Error' } ) ,
772+ fetch500Options ,
773+ ) ,
774+ ) ,
775+ ) ;
776+ await client
777+ . request ( { path : 'test-path' , httpMethod : 'POST' } )
778+ . catch ( ( e ) => {
779+ expect ( e . name ) . toEqual ( 'ApiError' ) ;
780+ expect ( e . message ) . toContain ( 'Internal Server Error' ) ;
781+ expect ( e . status ) . toEqual ( 500 ) ;
782+ } ) ;
783+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 1 ) ;
784+ } ) ;
785+
786+ it ( 'should retry requests with default retry options if retry options are not set' , async ( ) => {
787+ const client = new ApiClient ( {
788+ auth : new FakeAuth ( ) ,
789+ project : 'vertex-project' ,
790+ location : 'vertex-location' ,
791+ vertexai : true ,
792+ apiVersion : 'v1beta1' ,
793+ httpOptions : {
794+ retryOptions : { } ,
795+ } ,
796+ uploader : new CrossUploader ( ) ,
797+ downloader : new CrossDownloader ( ) ,
798+ } ) ;
799+ const fetchSpy = spyOn ( global , 'fetch' ) . and . returnValue (
800+ Promise . resolve (
801+ new Response (
802+ JSON . stringify ( { 'error' : 'Internal Server Error' } ) ,
803+ fetch500Options ,
804+ ) ,
805+ ) ,
806+ ) ;
807+ await client
808+ . request ( { path : 'test-path' , httpMethod : 'POST' } )
809+ . catch ( ( e ) => {
810+ console . log ( e ) ;
811+ } ) ;
812+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 5 ) ; // Default retry attempts is 5.
813+ } ) ;
724814 } ) ;
725815
726816 describe ( 'post/get methods' , ( ) => {
0 commit comments