Skip to content

Commit

Permalink
fix tests for tcp tunnel as well
Browse files Browse the repository at this point in the history
  • Loading branch information
jirimoravcik committed Jan 15, 2025
1 parent 9f210ee commit eddf358
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
17 changes: 9 additions & 8 deletions test/tcp_tunnel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const http = require('http');
const proxy = require('proxy');

const { createTunnel, closeTunnel } = require('../src/index');
const { expectThrowsAsync } = require('./utils/throws_async');

const destroySocket = (socket) => new Promise((resolve, reject) => {
if (!socket || socket.destroyed) return resolve();
Expand Down Expand Up @@ -42,16 +43,16 @@ const closeServer = (server, connections) => new Promise((resolve, reject) => {

describe('tcp_tunnel.createTunnel', () => {
it('throws error if proxyUrl is not in correct format', () => {
assert.throws(() => { createTunnel('socks://user:[email protected]:123', 'localhost:9000'); }, /must have the "http" protocol/);
assert.throws(() => { createTunnel('socks5://user:[email protected]', 'localhost:9000'); }, /must have the "http" protocol/);
expectThrowsAsync(async () => { await createTunnel('socks://user:[email protected]:123', 'localhost:9000'); }, /must have the "http" protocol/);
expectThrowsAsync(async () => { await createTunnel('socks5://user:[email protected]', 'localhost:9000'); }, /must have the "http" protocol/);
});
it('throws error if target is not in correct format', () => {
assert.throws(() => { createTunnel('http://user:[email protected]:12'); }, 'Missing target hostname');
assert.throws(() => { createTunnel('http://user:[email protected]:12', null); }, 'Missing target hostname');
assert.throws(() => { createTunnel('http://user:[email protected]:12', ''); }, 'Missing target hostname');
assert.throws(() => { createTunnel('http://user:[email protected]:12', 'whatever'); }, 'Missing target port');
assert.throws(() => { createTunnel('http://user:[email protected]:12', 'whatever:'); }, 'Missing target port');
assert.throws(() => { createTunnel('http://user:[email protected]:12', ':whatever'); }, /Invalid URL/);
expectThrowsAsync(async () => { await createTunnel('http://user:[email protected]:12'); }, 'Missing target hostname');
expectThrowsAsync(async () => { await createTunnel('http://user:[email protected]:12', null); }, 'Missing target hostname');
expectThrowsAsync(async () => { await createTunnel('http://user:[email protected]:12', ''); }, 'Missing target hostname');
expectThrowsAsync(async () => { await createTunnel('http://user:[email protected]:12', 'whatever'); }, 'Missing target port');
expectThrowsAsync(async () => { await createTunnel('http://user:[email protected]:12', 'whatever:'); }, 'Missing target port');
expectThrowsAsync(async () => { await createTunnel('http://user:[email protected]:12', ':whatever'); }, /Invalid URL/);
});
it('correctly tunnels to tcp service and then is able to close the connection', () => {
const proxyServerConnections = [];
Expand Down
15 changes: 12 additions & 3 deletions test/utils/throws_async.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
const { expect } = require('chai');

const expectThrowsAsync = async (method, errorMessage) => {
/**
* Expect an async function to throw
* @param {*} func Async function to be tested
* @param {*} errorMessage Error message to be expected, can be a string or a RegExp
*/
const expectThrowsAsync = async (func, errorMessage) => {
let error = null;
try {
await method();
await func();
} catch (err) {
error = err;
}
expect(error).to.be.an('Error');
if (errorMessage) {
expect(error.message).to.equal(errorMessage);
if (errorMessage instanceof RegExp) {
expect(error.message).to.match(errorMessage);
} else {
expect(error.message).to.contain(errorMessage);
}
}
};

Expand Down

0 comments on commit eddf358

Please sign in to comment.