91 lines
3.2 KiB
TypeScript
91 lines
3.2 KiB
TypeScript
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
|
import * as plugins from '../ts/core_node/plugins.js';
|
|
import { SmartRequest } from '../ts/client/index.js';
|
|
import { CoreRequest } from '../ts/core_node/index.js';
|
|
|
|
// Check if Docker socket exists (common unix socket for testing)
|
|
const dockerSocketPath = '/var/run/docker.sock';
|
|
let dockerAvailable = false;
|
|
|
|
try {
|
|
await plugins.fs.promises.access(dockerSocketPath, plugins.fs.constants.R_OK);
|
|
dockerAvailable = true;
|
|
} catch (error) {
|
|
console.log(
|
|
'Docker socket not available - skipping unix socket tests. To enable, ensure Docker is running.',
|
|
);
|
|
}
|
|
|
|
tap.test('node: 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('node: should parse unix socket URLs correctly', async () => {
|
|
const result = CoreRequest.parseUnixSocketUrl('unix:/var/run/docker.sock:/v1.24/version');
|
|
expect(result.socketPath).toEqual('/var/run/docker.sock');
|
|
expect(result.path).toEqual('/v1.24/version');
|
|
});
|
|
|
|
if (dockerAvailable) {
|
|
tap.test('node: 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('node: 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('node: 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('node: 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(
|
|
'node: unix socket tests skipped - Docker socket not available',
|
|
);
|
|
}
|
|
|
|
export default tap.start();
|