Files
dragonchain-prime-node-sdk/tests/client.test.ts
Andrew Miller 98ab2e05a7
Some checks failed
Build and Test / build (16.x) (push) Failing after 12s
Build and Test / build (18.x) (push) Failing after 6s
Build and Test / build (20.x) (push) Failing after 6s
Initial Commit
2025-11-05 15:25:20 -05:00

89 lines
2.5 KiB
TypeScript

/**
* Tests for HTTP client with HMAC authentication
*/
import * as crypto from 'crypto';
import { DragonchainClient } from '../src/client';
describe('DragonchainClient', () => {
const testConfig = {
publicId: 'test-public-id',
authKeyId: 'test-auth-key-id',
authKey: 'test-auth-key',
baseURL: 'https://test.dragonchain.com',
};
let client: DragonchainClient;
beforeEach(() => {
client = new DragonchainClient(testConfig);
});
describe('constructor', () => {
it('should create a client with correct configuration', () => {
expect(client).toBeDefined();
expect(client.get).toBeDefined();
});
it('should remove trailing slash from baseURL', () => {
const clientWithSlash = new DragonchainClient({
...testConfig,
baseURL: 'https://test.dragonchain.com/',
});
expect(clientWithSlash).toBeDefined();
});
});
describe('HMAC authentication', () => {
it('should generate correct HMAC signature', () => {
// Test HMAC generation by creating a mock scenario
const method = 'POST';
const path = '/api/v1/transaction';
const timestamp = '1234567890';
const contentType = 'application/json';
const body = Buffer.from(JSON.stringify({ test: 'data' }));
// Calculate expected values
const contentHash = crypto.createHash('sha256').update(body).digest('base64');
const message = [
method.toUpperCase(),
path,
testConfig.publicId,
timestamp,
contentType,
contentHash,
].join('\n');
const expectedHmac = crypto
.createHmac('sha256', testConfig.authKey)
.update(message)
.digest('base64');
// The auth header should follow the format: DC1-HMAC-SHA256 {authKeyId}:{signature}
const expectedAuthHeader = `DC1-HMAC-SHA256 ${testConfig.authKeyId}:${expectedHmac}`;
// We can't directly test the private method, but we verify the format is correct
expect(expectedAuthHeader).toContain('DC1-HMAC-SHA256');
expect(expectedAuthHeader).toContain(testConfig.authKeyId);
});
});
describe('request methods', () => {
it('should have get method', () => {
expect(typeof client.get).toBe('function');
});
it('should have post method', () => {
expect(typeof client.post).toBe('function');
});
it('should have put method', () => {
expect(typeof client.put).toBe('function');
});
it('should have delete method', () => {
expect(typeof client.delete).toBe('function');
});
});
});