@@ -144,6 +144,77 @@ describe('SocksProxyAgent', () => {
144
144
const body = await json ( res ) ;
145
145
assert . equal ( 'bar' , body . foo ) ;
146
146
} ) ;
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
+ } ) ;
147
218
} ) ;
148
219
149
220
describe ( '"https" module' , ( ) => {
@@ -154,12 +225,10 @@ describe('SocksProxyAgent', () => {
154
225
res . end ( JSON . stringify ( req . headers ) ) ;
155
226
} ) ;
156
227
157
- const agent = new SocksProxyAgent ( socksServerUrl ) ;
158
-
159
228
const res = await req (
160
229
`https://127.0.0.1:${ httpsServerUrl . port } /foo` ,
161
230
{
162
- agent,
231
+ agent : new SocksProxyAgent ( socksServerUrl ) ,
163
232
rejectUnauthorized : false ,
164
233
headers : { foo : 'bar' } ,
165
234
}
0 commit comments