Skip to content

Commit

Permalink
Add SOCKS auth tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Apr 6, 2024
1 parent 5908e84 commit 70023c1
Showing 1 changed file with 72 additions and 3 deletions.
75 changes: 72 additions & 3 deletions packages/socks-proxy-agent/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,77 @@ describe('SocksProxyAgent', () => {
const body = await json(res);
assert.equal('bar', body.foo);
});

it('should work with username/password auth', async () => {
let authWasCalled = false;
socksServer._auths.pop();
socksServer.useAuth(
socks.auth.UserPassword(
(
user: string,
password: string,
cb: (valid: boolean) => void
) => {
authWasCalled = true;
cb(user === 'nodejs' && password === 'rules!');
}
)
);

socksServerUrl.username = 'nodejs'
socksServerUrl.password = 'rules!'
console.log(socksServerUrl.href)

httpServer.once('request', function (req, res) {
assert.equal('/foo', req.url);
res.statusCode = 404;
res.end(JSON.stringify(req.headers));
});

const res = await req(new URL('/foo', httpServerUrl), {
agent: new SocksProxyAgent(socksServerUrl),
headers: { foo: 'bar' },
});
assert(authWasCalled);
assert.equal(404, res.statusCode);

const body = await json(res);
assert.equal('bar', body.foo);

});

it('should emit "error" event if username/password auth fails', async () => {
let authWasCalled = false;
socksServer._auths.pop();
socksServer.useAuth(
socks.auth.UserPassword(
(
user: string,
password: string,
cb: (valid: boolean) => void
) => {
authWasCalled = true;
cb(user === 'nodejs' && password === 'rules!');
}
)
);

socksServerUrl.username = 'nodejs'
socksServerUrl.password = 'bad'

let err: Error | undefined;
try {
await req(new URL('/foo', httpServerUrl), {
agent: new SocksProxyAgent(socksServerUrl),
headers: { foo: 'bar' },
});
} catch (_err) {
err = _err as Error;
}
assert(authWasCalled);
assert(err);
assert.equal(err.message, `Socks5 Authentication failed`);
});
});

describe('"https" module', () => {
Expand All @@ -154,12 +225,10 @@ describe('SocksProxyAgent', () => {
res.end(JSON.stringify(req.headers));
});

const agent = new SocksProxyAgent(socksServerUrl);

const res = await req(
`https://127.0.0.1:${httpsServerUrl.port}/foo`,
{
agent,
agent: new SocksProxyAgent(socksServerUrl),
rejectUnauthorized: false,
headers: { foo: 'bar' },
}
Expand Down

0 comments on commit 70023c1

Please sign in to comment.