Skip to content

Commit 70023c1

Browse files
committed
Add SOCKS auth tests
1 parent 5908e84 commit 70023c1

File tree

1 file changed

+72
-3
lines changed
  • packages/socks-proxy-agent/test

1 file changed

+72
-3
lines changed

packages/socks-proxy-agent/test/test.ts

+72-3
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,77 @@ describe('SocksProxyAgent', () => {
144144
const body = await json(res);
145145
assert.equal('bar', body.foo);
146146
});
147+
148+
it('should work with username/password auth', async () => {
149+
let authWasCalled = false;
150+
socksServer._auths.pop();
151+
socksServer.useAuth(
152+
socks.auth.UserPassword(
153+
(
154+
user: string,
155+
password: string,
156+
cb: (valid: boolean) => void
157+
) => {
158+
authWasCalled = true;
159+
cb(user === 'nodejs' && password === 'rules!');
160+
}
161+
)
162+
);
163+
164+
socksServerUrl.username = 'nodejs'
165+
socksServerUrl.password = 'rules!'
166+
console.log(socksServerUrl.href)
167+
168+
httpServer.once('request', function (req, res) {
169+
assert.equal('/foo', req.url);
170+
res.statusCode = 404;
171+
res.end(JSON.stringify(req.headers));
172+
});
173+
174+
const res = await req(new URL('/foo', httpServerUrl), {
175+
agent: new SocksProxyAgent(socksServerUrl),
176+
headers: { foo: 'bar' },
177+
});
178+
assert(authWasCalled);
179+
assert.equal(404, res.statusCode);
180+
181+
const body = await json(res);
182+
assert.equal('bar', body.foo);
183+
184+
});
185+
186+
it('should emit "error" event if username/password auth fails', async () => {
187+
let authWasCalled = false;
188+
socksServer._auths.pop();
189+
socksServer.useAuth(
190+
socks.auth.UserPassword(
191+
(
192+
user: string,
193+
password: string,
194+
cb: (valid: boolean) => void
195+
) => {
196+
authWasCalled = true;
197+
cb(user === 'nodejs' && password === 'rules!');
198+
}
199+
)
200+
);
201+
202+
socksServerUrl.username = 'nodejs'
203+
socksServerUrl.password = 'bad'
204+
205+
let err: Error | undefined;
206+
try {
207+
await req(new URL('/foo', httpServerUrl), {
208+
agent: new SocksProxyAgent(socksServerUrl),
209+
headers: { foo: 'bar' },
210+
});
211+
} catch (_err) {
212+
err = _err as Error;
213+
}
214+
assert(authWasCalled);
215+
assert(err);
216+
assert.equal(err.message, `Socks5 Authentication failed`);
217+
});
147218
});
148219

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

157-
const agent = new SocksProxyAgent(socksServerUrl);
158-
159228
const res = await req(
160229
`https://127.0.0.1:${httpsServerUrl.port}/foo`,
161230
{
162-
agent,
231+
agent: new SocksProxyAgent(socksServerUrl),
163232
rejectUnauthorized: false,
164233
headers: { foo: 'bar' },
165234
}

0 commit comments

Comments
 (0)