59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
|
import * as net from 'net';
|
|
import { SmartProxy } from '../ts/index.js';
|
|
|
|
tap.test('simple socket handler test', async () => {
|
|
const proxy = new SmartProxy({
|
|
routes: [{
|
|
name: 'simple-handler',
|
|
match: {
|
|
ports: 8888
|
|
// No domains restriction - will match all connections on this port
|
|
},
|
|
action: {
|
|
type: 'socket-handler',
|
|
socketHandler: (socket, context) => {
|
|
console.log('Handler called!');
|
|
socket.write('HELLO\n');
|
|
socket.end();
|
|
}
|
|
}
|
|
}],
|
|
enableDetailedLogging: true
|
|
});
|
|
|
|
await proxy.start();
|
|
|
|
// Test connection
|
|
const client = new net.Socket();
|
|
let response = '';
|
|
|
|
client.on('data', (data) => {
|
|
response += data.toString();
|
|
});
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
client.connect(8888, 'localhost', () => {
|
|
console.log('Connected');
|
|
// Send some initial data to trigger the handler
|
|
client.write('test\n');
|
|
resolve();
|
|
});
|
|
client.on('error', reject);
|
|
});
|
|
|
|
// Wait for response
|
|
await new Promise(resolve => {
|
|
client.on('close', () => {
|
|
console.log('Connection closed');
|
|
resolve(undefined);
|
|
});
|
|
});
|
|
|
|
console.log('Got response:', response);
|
|
expect(response).toEqual('HELLO\n');
|
|
|
|
await proxy.stop();
|
|
});
|
|
|
|
export default tap.start(); |