Files
smartrequest/test/test.unixsocket.bun.ts

102 lines
3.5 KiB
TypeScript
Raw Normal View History

import { tap, expect } from '@git.zone/tstest/tapbundle';
import { SmartRequest } from '../ts/client/index.js';
import { CoreRequest } from '../ts/core_bun/index.js';
// Check if Docker socket exists (common unix socket for testing)
const dockerSocketPath = '/var/run/docker.sock';
let dockerAvailable = false;
try {
const file = Bun.file(dockerSocketPath);
dockerAvailable = await file.exists();
} catch (error) {
console.log(
'Docker socket not available - skipping unix socket tests. To enable, ensure Docker is running.',
);
}
tap.test('bun: should detect unix socket URLs correctly', async () => {
expect(CoreRequest.isUnixSocket('unix:/var/run/docker.sock:/version')).toBeTrue();
expect(CoreRequest.isUnixSocket('http://unix:/var/run/docker.sock:/version')).toBeTrue();
expect(CoreRequest.isUnixSocket('https://unix:/var/run/docker.sock:/version')).toBeTrue();
expect(CoreRequest.isUnixSocket('http://example.com')).toBeFalse();
expect(CoreRequest.isUnixSocket('https://example.com')).toBeFalse();
});
tap.test('bun: should parse unix socket URLs correctly', async () => {
const result = CoreRequest.parseUnixSocketUrl('unix:/var/run/docker.sock:/v1.24/version');
expect(result.socketPath).toEqual('unix:/var/run/docker.sock');
expect(result.path).toEqual('/v1.24/version');
});
if (dockerAvailable) {
tap.test('bun: should connect to Docker via unix socket (unix: protocol)', async () => {
const response = await SmartRequest.create()
.url('http://unix:/var/run/docker.sock:/version')
.get();
expect(response.ok).toBeTrue();
expect(response.status).toEqual(200);
const body = await response.json();
expect(body).toHaveProperty('Version');
console.log(`Docker version: ${body.Version}`);
});
tap.test('bun: should connect to Docker via socketPath option', async () => {
const response = await CoreRequest.create('http://localhost/version', {
socketPath: '/var/run/docker.sock',
});
expect(response.ok).toBeTrue();
expect(response.status).toEqual(200);
const body = await response.json();
expect(body).toHaveProperty('Version');
});
tap.test('bun: should connect to Docker via unix option', async () => {
const response = await CoreRequest.create('http://localhost/version', {
unix: '/var/run/docker.sock',
});
expect(response.ok).toBeTrue();
expect(response.status).toEqual(200);
const body = await response.json();
expect(body).toHaveProperty('Version');
});
tap.test('bun: should handle unix socket with query parameters', async () => {
const response = await SmartRequest.create()
.url('http://unix:/var/run/docker.sock:/containers/json')
.query({ all: 'true' })
.get();
expect(response.ok).toBeTrue();
expect(response.status).toEqual(200);
const body = await response.json();
expect(Array.isArray(body)).toBeTrue();
});
tap.test('bun: should handle unix socket with POST requests', async () => {
// Test POST to Docker API (this specific endpoint may require permissions)
const response = await SmartRequest.create()
.url('http://unix:/var/run/docker.sock:/containers/json')
.query({ all: 'true', limit: '1' })
.get();
expect(response.status).toBeGreaterThanOrEqual(200);
expect(response.status).toBeLessThan(500);
await response.text(); // Consume body
});
} else {
tap.skip.test(
'bun: unix socket tests skipped - Docker socket not available',
);
}
export default tap.start();