@@ -112,6 +112,31 @@ describe("OpenAICompatibleEmbedder", () => {
112112 expect ( embedder ) . toBeDefined ( )
113113 } )
114114
115+ it ( "should create embedder with custom headers" , ( ) => {
116+ const customHeaders = {
117+ "X-Custom-Header" : "custom-value" ,
118+ "X-Another-Header" : "another-value" ,
119+ }
120+ embedder = new OpenAICompatibleEmbedder ( testBaseUrl , testApiKey , testModelId , undefined , customHeaders )
121+
122+ expect ( MockedOpenAI ) . toHaveBeenCalledWith ( {
123+ baseURL : testBaseUrl ,
124+ apiKey : testApiKey ,
125+ defaultHeaders : customHeaders ,
126+ } )
127+ expect ( embedder ) . toBeDefined ( )
128+ } )
129+
130+ it ( "should create embedder without custom headers when not provided" , ( ) => {
131+ embedder = new OpenAICompatibleEmbedder ( testBaseUrl , testApiKey , testModelId , undefined , undefined )
132+
133+ expect ( MockedOpenAI ) . toHaveBeenCalledWith ( {
134+ baseURL : testBaseUrl ,
135+ apiKey : testApiKey ,
136+ } )
137+ expect ( embedder ) . toBeDefined ( )
138+ } )
139+
115140 it ( "should throw error when baseUrl is missing" , ( ) => {
116141 expect ( ( ) => new OpenAICompatibleEmbedder ( "" , testApiKey , testModelId ) ) . toThrow (
117142 "embeddings:validation.baseUrlRequired" ,
@@ -813,6 +838,81 @@ describe("OpenAICompatibleEmbedder", () => {
813838 expect ( baseResult . embeddings [ 0 ] ) . toEqual ( [ 0.4 , 0.5 , 0.6 ] )
814839 } )
815840
841+ it ( "should include custom headers in direct fetch requests" , async ( ) => {
842+ const testTexts = [ "Test text" ]
843+ const customHeaders = {
844+ "X-Custom-Header" : "custom-value" ,
845+ "X-API-Version" : "v2" ,
846+ }
847+ const base64String = createBase64Embedding ( [ 0.1 , 0.2 , 0.3 ] )
848+
849+ // Test Azure URL with custom headers (direct fetch)
850+ const azureEmbedder = new OpenAICompatibleEmbedder (
851+ azureUrl ,
852+ testApiKey ,
853+ testModelId ,
854+ undefined ,
855+ customHeaders ,
856+ )
857+ const mockFetchResponse = createMockResponse ( {
858+ data : [ { embedding : base64String } ] ,
859+ usage : { prompt_tokens : 10 , total_tokens : 15 } ,
860+ } )
861+ ; ( global . fetch as MockedFunction < typeof fetch > ) . mockResolvedValue ( mockFetchResponse as any )
862+
863+ const azureResult = await azureEmbedder . createEmbeddings ( testTexts )
864+ expect ( global . fetch ) . toHaveBeenCalledWith (
865+ azureUrl ,
866+ expect . objectContaining ( {
867+ method : "POST" ,
868+ headers : expect . objectContaining ( {
869+ "Content-Type" : "application/json" ,
870+ "api-key" : testApiKey ,
871+ Authorization : `Bearer ${ testApiKey } ` ,
872+ "X-Custom-Header" : "custom-value" ,
873+ "X-API-Version" : "v2" ,
874+ } ) ,
875+ } ) ,
876+ )
877+ expect ( mockEmbeddingsCreate ) . not . toHaveBeenCalled ( )
878+ expectEmbeddingValues ( azureResult . embeddings [ 0 ] , [ 0.1 , 0.2 , 0.3 ] )
879+ } )
880+
881+ it ( "should handle custom headers that override default headers" , async ( ) => {
882+ const testTexts = [ "Test text" ]
883+ const customHeaders = {
884+ "api-key" : "override-key" , // Override the default api-key
885+ "X-Custom-Header" : "custom-value" ,
886+ }
887+ const base64String = createBase64Embedding ( [ 0.1 , 0.2 , 0.3 ] )
888+
889+ const azureEmbedder = new OpenAICompatibleEmbedder (
890+ azureUrl ,
891+ testApiKey ,
892+ testModelId ,
893+ undefined ,
894+ customHeaders ,
895+ )
896+ const mockFetchResponse = createMockResponse ( {
897+ data : [ { embedding : base64String } ] ,
898+ usage : { prompt_tokens : 10 , total_tokens : 15 } ,
899+ } )
900+ ; ( global . fetch as MockedFunction < typeof fetch > ) . mockResolvedValue ( mockFetchResponse as any )
901+
902+ const azureResult = await azureEmbedder . createEmbeddings ( testTexts )
903+ expect ( global . fetch ) . toHaveBeenCalledWith (
904+ azureUrl ,
905+ expect . objectContaining ( {
906+ method : "POST" ,
907+ headers : expect . objectContaining ( {
908+ "api-key" : "override-key" , // Custom header overrides default
909+ "X-Custom-Header" : "custom-value" ,
910+ } ) ,
911+ } ) ,
912+ )
913+ expectEmbeddingValues ( azureResult . embeddings [ 0 ] , [ 0.1 , 0.2 , 0.3 ] )
914+ } )
915+
816916 it . each ( [
817917 [ 401 , "Authentication failed. Please check your API key." ] ,
818918 [ 500 , "Failed to create embeddings after 3 attempts" ] ,
0 commit comments