fix(tests): make tests more robust and bump small dependencies
This commit is contained in:
@@ -5,19 +5,27 @@ import { SmartProxy } from '../ts/proxies/smart-proxy/smart-proxy.js';
|
||||
let echoServer: net.Server;
|
||||
let proxy: SmartProxy;
|
||||
|
||||
const ECHO_PORT = 47400;
|
||||
const PROXY_PORT_1 = 47401;
|
||||
const PROXY_PORT_2 = 47402;
|
||||
|
||||
tap.test('port forwarding should not immediately close connections', async (tools) => {
|
||||
// Set a timeout for this test
|
||||
tools.timeout(10000); // 10 seconds
|
||||
// Create an echo server
|
||||
echoServer = await new Promise<net.Server>((resolve) => {
|
||||
echoServer = await new Promise<net.Server>((resolve, reject) => {
|
||||
const server = net.createServer((socket) => {
|
||||
socket.on('data', (data) => {
|
||||
socket.write(`ECHO: ${data}`);
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(8888, () => {
|
||||
console.log('Echo server listening on port 8888');
|
||||
|
||||
server.on('error', (err) => {
|
||||
console.error(`Echo server error: ${err.message}`);
|
||||
reject(err);
|
||||
});
|
||||
server.listen(ECHO_PORT, () => {
|
||||
console.log(`Echo server listening on port ${ECHO_PORT}`);
|
||||
resolve(server);
|
||||
});
|
||||
});
|
||||
@@ -26,10 +34,10 @@ tap.test('port forwarding should not immediately close connections', async (tool
|
||||
proxy = new SmartProxy({
|
||||
routes: [{
|
||||
name: 'test-forward',
|
||||
match: { ports: 9999 },
|
||||
match: { ports: PROXY_PORT_1 },
|
||||
action: {
|
||||
type: 'forward',
|
||||
targets: [{ host: 'localhost', port: 8888 }]
|
||||
targets: [{ host: 'localhost', port: ECHO_PORT }]
|
||||
}
|
||||
}]
|
||||
});
|
||||
@@ -37,21 +45,24 @@ tap.test('port forwarding should not immediately close connections', async (tool
|
||||
await proxy.start();
|
||||
|
||||
// Test connection through proxy
|
||||
const client = net.createConnection(9999, 'localhost');
|
||||
|
||||
const client = net.createConnection(PROXY_PORT_1, 'localhost');
|
||||
|
||||
const result = await new Promise<string>((resolve, reject) => {
|
||||
client.on('data', (data) => {
|
||||
const response = data.toString();
|
||||
client.end(); // Close the connection after receiving data
|
||||
resolve(response);
|
||||
});
|
||||
|
||||
|
||||
client.on('error', reject);
|
||||
|
||||
|
||||
client.write('Hello');
|
||||
});
|
||||
|
||||
expect(result).toEqual('ECHO: Hello');
|
||||
|
||||
// Stop proxy from test 1 before test 2 reassigns the variable
|
||||
await proxy.stop();
|
||||
});
|
||||
|
||||
tap.test('TLS passthrough should work correctly', async () => {
|
||||
@@ -59,7 +70,7 @@ tap.test('TLS passthrough should work correctly', async () => {
|
||||
proxy = new SmartProxy({
|
||||
routes: [{
|
||||
name: 'tls-test',
|
||||
match: { ports: 8443, domains: 'test.example.com' },
|
||||
match: { ports: PROXY_PORT_2, domains: 'test.example.com' },
|
||||
action: {
|
||||
type: 'forward',
|
||||
tls: { mode: 'passthrough' },
|
||||
@@ -85,16 +96,6 @@ tap.test('cleanup', async () => {
|
||||
});
|
||||
});
|
||||
}
|
||||
if (proxy) {
|
||||
await proxy.stop();
|
||||
console.log('Proxy stopped');
|
||||
}
|
||||
});
|
||||
|
||||
export default tap.start().then(() => {
|
||||
// Force exit after tests complete
|
||||
setTimeout(() => {
|
||||
console.log('Forcing process exit');
|
||||
process.exit(0);
|
||||
}, 1000);
|
||||
});
|
||||
export default tap.start();
|
||||
Reference in New Issue
Block a user