Skip to content

Commit e90a58d

Browse files
author
M. Emin Cihangeri
committed
chore: Create mock data for test
1 parent 091ebc5 commit e90a58d

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

packages/connectivity/src/http-agent/http-agent.spec.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { X509Certificate } from 'node:crypto';
22
import mock from 'mock-fs';
3-
import * as jks from 'jks-js';
43
import { 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';
510
import { registerDestinationCache } from '../scp-cf/destination/register-destination-cache';
611
import { certAsString } from '../../../../test-resources/test/test-util/test-certificate';
712
import { 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

Comments
 (0)