@@ -3,33 +3,99 @@ var assert = require('assert');
3
3
var net = require ( 'net' ) ;
4
4
5
5
var tcpPort = common . PORT ;
6
+ var expectedConnections = 7 ;
6
7
var clientConnected = 0 ;
7
8
var serverConnected = 0 ;
8
9
9
10
var server = net . createServer ( function ( socket ) {
10
11
socket . end ( ) ;
11
- if ( ++ serverConnected === 4 ) {
12
+ if ( ++ serverConnected === expectedConnections ) {
12
13
server . close ( ) ;
13
14
}
14
15
} ) ;
16
+
15
17
server . listen ( tcpPort , 'localhost' , function ( ) {
16
18
function cb ( ) {
17
19
++ clientConnected ;
18
20
}
19
21
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
+
20
30
net . createConnection ( tcpPort ) . on ( 'connect' , cb ) ;
21
31
net . createConnection ( tcpPort , 'localhost' ) . on ( 'connect' , cb ) ;
22
32
net . createConnection ( tcpPort , cb ) ;
23
33
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: ' ) ;
24
65
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' ) ;
30
89
} ) ;
31
90
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 ) ;
34
97
} ) ;
35
98
99
+ process . on ( 'exit' , function ( ) {
100
+ assert . equal ( clientConnected , expectedConnections ) ;
101
+ } ) ;
0 commit comments