11import { X509Certificate } from 'node:crypto' ;
22import mock from 'mock-fs' ;
3- import * as jks from 'jks-js' ;
43import { createLogger } from '@sap-cloud-sdk/util' ;
4+
5+ // Mock jks-js module
6+ jest . mock ( 'jks-js' , ( ) => ( {
7+ toPem : jest . fn ( )
8+ } ) ) ;
9+ import * as jks from 'jks-js' ;
510import { registerDestinationCache } from '../scp-cf/destination/register-destination-cache' ;
611import { certAsString } from '../../../../test-resources/test/test-util/test-certificate' ;
712import { getAgentConfig } from './http-agent' ;
@@ -174,6 +179,15 @@ describe('createAgent', () => {
174179 } ) ;
175180
176181 it ( 'does not throw an error for supported JKS format' , async ( ) => {
182+ // Mock jks.toPem to return valid PEM data
183+ const mockPemKeystore = {
184+ 'alias1' : {
185+ cert : '-----BEGIN CERTIFICATE-----\nMII...\n-----END CERTIFICATE-----' ,
186+ key : '-----BEGIN PRIVATE KEY-----\nMII...\n-----END PRIVATE KEY-----'
187+ }
188+ } ;
189+ ( jks . toPem as jest . MockedFunction < typeof jks . toPem > ) . mockReturnValue ( mockPemKeystore ) ;
190+
177191 const destination : HttpDestination = {
178192 url : 'https://destination.example.com' ,
179193 authentication : 'ClientCertificateAuthentication' ,
@@ -182,22 +196,21 @@ describe('createAgent', () => {
182196 certificates : [
183197 {
184198 name : 'cert.jks' ,
185- content : 'base64string' ,
199+ content : 'base64string' , // Can remain dummy since we're mocking
186200 type : 'CERTIFICATE'
187201 }
188202 ]
189203 } ;
190204
191- // Mock the jks.toPem to avoid actual JKS parsing
192- const mockPem = {
193- alias : {
194- cert : 'mock-cert' ,
195- key : 'mock-key'
196- }
205+ const expectedOptions = {
206+ rejectUnauthorized : true ,
207+ cert : Buffer . from ( mockPemKeystore [ 'alias1' ] . cert , 'utf8' ) ,
208+ key : Buffer . from ( mockPemKeystore [ 'alias1' ] . key , 'utf8' )
197209 } ;
198- jest . spyOn ( jks , 'toPem' ) . mockReturnValue ( mockPem ) ;
199210
200- await expect ( getAgentConfig ( destination ) ) . resolves . toBeDefined ( ) ;
211+ expect (
212+ ( await getAgentConfig ( destination ) ) [ 'httpsAgent' ] [ 'options' ]
213+ ) . toMatchObject ( expectedOptions ) ;
201214 } ) ;
202215
203216 it ( 'throws an error if the format is not supported' , async ( ) => {
0 commit comments