@@ -356,85 +356,107 @@ tape('onion plug', function (t) {
356
356
t . end ( )
357
357
} )
358
358
359
- tape ( 'id of stream from server' , function ( t ) {
360
- check = function ( id , cb ) {
361
- cb ( null , true )
362
- }
363
- var close = combined . server ( function ( stream ) {
364
- var addr = combined . parse ( stream . address )
365
- t . ok ( addr )
366
- //console.log('address as seen on server', addr)
367
- t . equal ( addr [ 0 ] . name , 'net' )
368
- t . deepEqual ( addr [ 1 ] , combined . parse ( combined . stringify ( ) ) [ 1 ] )
369
-
370
- pull ( stream . source , stream . sink ) //echo
371
- } , function ( err ) {
372
- if ( err ) throw err
373
- } , function ( ) {
374
-
375
- combined . client ( combined . stringify ( ) , function ( err , stream ) {
376
- if ( err ) throw err
359
+ function testServerId ( combined , name , port ) {
360
+ tape ( 'id of stream from server' , function ( t ) {
361
+ check = function ( id , cb ) {
362
+ cb ( null , true )
363
+ }
364
+ var close = combined . server ( function ( stream ) {
365
+ console . log ( 'raw address on server:' , stream . address )
377
366
var addr = combined . parse ( stream . address )
378
- t . equal ( addr [ 0 ] . name , 'net' )
379
- t . equal ( addr [ 0 ] . port , 4848 )
367
+ t . ok ( addr )
368
+ console . log ( 'address as seen on server' , addr )
369
+ t . equal ( ( addr [ 0 ] . name || addr [ 0 ] . protocol ) . replace ( ':' , '' ) , name )
380
370
t . deepEqual ( addr [ 1 ] , combined . parse ( combined . stringify ( ) ) [ 1 ] )
381
- stream . source ( true , function ( ) {
382
- close ( t . end )
371
+
372
+ pull ( stream . source , stream . sink ) //echo
373
+ } , function ( err ) {
374
+ if ( err ) throw err
375
+ } , function ( ) {
376
+
377
+ combined . client ( combined . stringify ( ) , function ( err , stream ) {
378
+ if ( err ) throw err
379
+ var addr = combined . parse ( stream . address )
380
+ t . equal ( ( addr [ 0 ] . name || addr [ 0 ] . protocol ) . replace ( ':' , '' ) , name )
381
+ if ( addr [ 0 ] . protocol === 'ws:' )
382
+ t . equal ( + addr [ 0 ] . port , 4849 )
383
+ else
384
+ t . equal ( + addr [ 0 ] . port , 4848 )
385
+ t . deepEqual ( addr [ 1 ] , combined . parse ( combined . stringify ( ) ) [ 1 ] )
386
+ stream . source ( true , function ( ) {
387
+ close ( t . end )
388
+ } )
383
389
} )
384
390
} )
385
391
} )
386
- } )
392
+
393
+ }
394
+
395
+ testServerId ( combined , 'net' )
396
+ testServerId ( combined_ws , 'ws' )
397
+
398
+
399
+
387
400
388
401
function testAbort ( name , combined ) {
389
402
tape ( name + ', aborted' , function ( t ) {
390
403
var close = combined . server ( function onConnection ( ) {
391
404
throw new Error ( 'should never happen' )
392
- } , null , ( ) => {
393
- var abort = combined . client ( combined . stringify ( ) , function ( err , stream ) {
394
- t . ok ( err , 'the error is expected' )
395
-
396
- // NOTE: without the timeout, we try to close the server
397
- // before it actually started listening, which fails and then
398
- // the server keeps runnung, causing the next test to fail with EADDRINUSE
399
- //
400
- // This is messy, combined.server should be a proper async call
401
- setTimeout ( function ( ) {
402
- //console.log('Calling close')
403
- close ( t . end )
404
- } , 500 )
405
- } )
405
+ } )
406
406
407
- abort ( )
407
+ var abort = combined . client ( combined . stringify ( ) , function ( err , stream ) {
408
+ t . ok ( err )
409
+ console . error ( "CLIENT ABORTED" , err )
410
+ // NOTE: without the timeout, we try to close the server
411
+ // before it actually started listening, which fails and then
412
+ // the server keeps runnung, causing the next test to fail with EADDRINUSE
413
+ //
414
+ // This is messy, combined.server should be a proper async call
415
+ setTimeout ( function ( ) {
416
+ console . log ( 'Calling close' )
417
+ close ( t . end )
418
+ } , 500 )
408
419
} )
420
+
421
+ abort ( )
409
422
} )
410
423
}
411
424
412
425
testAbort ( 'combined' , combined )
413
426
testAbort ( 'combined.ws' , combined_ws )
414
427
415
- tape ( 'error should have client address on it' , function ( t ) {
416
- // return t.end()
417
- check = function ( id , cb ) {
418
- throw new Error ( 'should never happen' )
419
- }
420
- var close = combined . server ( function ( stream ) {
421
- throw new Error ( 'should never happen' )
422
- } , function ( err ) {
423
- t . ok ( / ^ n e t : / . test ( err . address ) )
424
- t . ok ( / ~ s h s : / . test ( err . address ) )
425
- //the shs address won't actually parse, because it doesn't have the key in it
426
- //because the key is not known in a wrong number.
427
- } , function ( ) {
428
- //very unlikely this is the address, which will give a wrong number at the server.
429
- var addr = combined . stringify ( ) . replace ( / s h s : ......../ , 'shs:XXXXXXXX' )
430
- combined . client ( addr , function ( err , stream ) {
431
- //client should see client auth rejected
432
- t . ok ( err )
433
- //console.log('Calling close')
434
- close ( t . end )
428
+ function testErrorAddress ( combined , type ) {
429
+ tape ( 'error should have client address on it:' + type , function ( t ) {
430
+ check = function ( id , cb ) {
431
+ throw new Error ( 'should never happen' )
432
+ }
433
+ var close = combined . server ( function ( stream ) {
434
+ throw new Error ( 'should never happen' )
435
+ } , function ( err ) {
436
+ var addr = err . address
437
+ t . ok ( err . address . indexOf ( type ) == 0 ) //net or ws
438
+ t . ok ( / \~ s h s \: / . test ( err . address ) )
439
+ //the shs address won't actually parse, because it doesn't have the key in it
440
+ //because the key is not known in a wrong number.
441
+ } , function ( ) {
442
+ //very unlikely this is the address, which will give a wrong number at the server.
443
+ var addr = combined . stringify ( ) . replace ( / s h s : ......../ , 'shs:XXXXXXXX' )
444
+ combined . client ( addr , function ( err , stream ) {
445
+ //client should see client auth rejected
446
+ t . ok ( err )
447
+ close ( ( ) => {
448
+ if ( type === 'ws' ) // we need to wait for the kill
449
+ setTimeout ( t . end , 1100 )
450
+ else
451
+ t . end ( )
452
+ } )
453
+ } )
435
454
} )
436
455
} )
437
- } )
456
+ }
457
+
458
+ testErrorAddress ( combined , 'net' )
459
+ testErrorAddress ( combined_ws , 'ws' )
438
460
439
461
tape ( 'multiple public different hosts' , function ( t ) {
440
462
var net1 = Net ( { host : '127.0.0.1' , port : 4854 , scope : 'public' } )
@@ -593,4 +615,4 @@ tape('ws: external is an array w/ multiple entries & shs transform', function (t
593
615
'ws://domain.de:9966~shs:+y42DK+BGzqvU00EWMKiyj4fITskSm+Drxq1Dt2s3Yw=;ws://funtime.net:9966~shs:+y42DK+BGzqvU00EWMKiyj4fITskSm+Drxq1Dt2s3Yw='
594
616
)
595
617
t . end ( )
596
- } )
618
+ } )
0 commit comments