@@ -3,33 +3,99 @@ var assert = require('assert');
33var net = require ( 'net' ) ;
44
55var tcpPort = common . PORT ;
6+ var expectedConnections = 7 ;
67var clientConnected = 0 ;
78var serverConnected = 0 ;
89
910var server = net . createServer ( function ( socket ) {
1011 socket . end ( ) ;
11- if ( ++ serverConnected === 4 ) {
12+ if ( ++ serverConnected === expectedConnections ) {
1213 server . close ( ) ;
1314 }
1415} ) ;
16+
1517server . listen ( tcpPort , 'localhost' , function ( ) {
1618 function cb ( ) {
1719 ++ clientConnected ;
1820 }
1921
22+ function fail ( opts , errtype , msg ) {
23+ assert . throws ( function ( ) {
24+ var client = net . createConnection ( opts , cb ) ;
25+ } , function ( err ) {
26+ return err instanceof errtype && msg === err . message ;
27+ } ) ;
28+ }
29+
2030 net . createConnection ( tcpPort ) . on ( 'connect' , cb ) ;
2131 net . createConnection ( tcpPort , 'localhost' ) . on ( 'connect' , cb ) ;
2232 net . createConnection ( tcpPort , cb ) ;
2333 net . createConnection ( tcpPort , 'localhost' , cb ) ;
34+ net . createConnection ( tcpPort + '' , 'localhost' , cb ) ;
35+ net . createConnection ( { port : tcpPort + '' } ) . on ( 'connect' , cb ) ;
36+ net . createConnection ( { port : '0x' + tcpPort . toString ( 16 ) } , cb ) ;
37+
38+ fail ( {
39+ port : true
40+ } , TypeError , 'port should be a number or string: true' ) ;
41+
42+ fail ( {
43+ port : false
44+ } , TypeError , 'port should be a number or string: false' ) ;
45+
46+ fail ( {
47+ port : [ ]
48+ } , TypeError , 'port should be a number or string: ' ) ;
49+
50+ fail ( {
51+ port : { }
52+ } , TypeError , 'port should be a number or string: [object Object]' ) ;
53+
54+ fail ( {
55+ port : null
56+ } , TypeError , 'port should be a number or string: null' ) ;
57+
58+ fail ( {
59+ port : ''
60+ } , RangeError , 'port should be >= 0 and < 65536: ' ) ;
61+
62+ fail ( {
63+ port : ' '
64+ } , RangeError , 'port should be >= 0 and < 65536: ' ) ;
2465
25- assert . throws ( function ( ) {
26- net . createConnection ( {
27- port : 'invalid!'
28- } , cb ) ;
29- } ) ;
66+ fail ( {
67+ port : '0x'
68+ } , RangeError , 'port should be >= 0 and < 65536: 0x' ) ;
69+
70+ fail ( {
71+ port : '-0x1'
72+ } , RangeError , 'port should be >= 0 and < 65536: -0x1' ) ;
73+
74+ fail ( {
75+ port : NaN
76+ } , RangeError , 'port should be >= 0 and < 65536: NaN' ) ;
77+
78+ fail ( {
79+ port : Infinity
80+ } , RangeError , 'port should be >= 0 and < 65536: Infinity' ) ;
81+
82+ fail ( {
83+ port : - 1
84+ } , RangeError , 'port should be >= 0 and < 65536: -1' ) ;
85+
86+ fail ( {
87+ port : 65536
88+ } , RangeError , 'port should be >= 0 and < 65536: 65536' ) ;
3089} ) ;
3190
32- process . on ( 'exit' , function ( ) {
33- assert . equal ( clientConnected , 4 ) ;
91+ // Try connecting to random ports, but do so once the server is closed
92+ server . on ( 'close' , function ( ) {
93+ function nop ( ) { }
94+
95+ net . createConnection ( { port : 0 } ) . on ( 'error' , nop ) ;
96+ net . createConnection ( { port : undefined } ) . on ( 'error' , nop ) ;
3497} ) ;
3598
99+ process . on ( 'exit' , function ( ) {
100+ assert . equal ( clientConnected , expectedConnections ) ;
101+ } ) ;
0 commit comments