@@ -37,16 +37,6 @@ const options = {
37
37
cert : fixtures . readKey ( 'agent1-cert.pem' )
38
38
} ;
39
39
40
- const tests = 2 ;
41
- let successful = 0 ;
42
-
43
- const testSucceeded = ( ) => {
44
- successful = successful + 1 ;
45
- if ( successful === tests ) {
46
- server . close ( ) ;
47
- }
48
- } ;
49
-
50
40
const body = 'hello world\n' ;
51
41
52
42
const serverCallback = common . mustCall ( function ( req , res ) {
@@ -57,61 +47,44 @@ const serverCallback = common.mustCall(function(req, res) {
57
47
const server = https . createServer ( options , serverCallback ) ;
58
48
59
49
server . listen ( 0 , common . mustCall ( ( ) => {
50
+ let tests = 0 ;
51
+
52
+ function done ( ) {
53
+ if ( -- tests === 0 )
54
+ server . close ( ) ;
55
+ }
56
+
60
57
// Do a request ignoring the unauthorized server certs
61
58
const port = server . address ( ) . port ;
62
59
63
- const noCertCheckOptions = {
60
+ const options = {
64
61
hostname : '127.0.0.1' ,
65
62
port : port ,
66
63
path : '/' ,
67
64
method : 'GET' ,
68
65
rejectUnauthorized : false
69
66
} ;
70
-
71
- noCertCheckOptions . Agent = new https . Agent ( noCertCheckOptions ) ;
72
-
73
- const req = https . request ( noCertCheckOptions , common . mustCall ( ( res ) => {
67
+ tests ++ ;
68
+ const req = https . request ( options , common . mustCall ( ( res ) => {
74
69
let responseBody = '' ;
75
70
res . on ( 'data' , function ( d ) {
76
71
responseBody = responseBody + d ;
77
72
} ) ;
78
73
79
74
res . on ( 'end' , common . mustCall ( ( ) => {
80
75
assert . strictEqual ( responseBody , body ) ;
81
- testSucceeded ( ) ;
76
+ done ( ) ;
82
77
} ) ) ;
83
78
} ) ) ;
84
79
req . end ( ) ;
85
80
86
- req . on ( 'error' , function ( e ) {
87
- throw e ;
88
- } ) ;
89
-
90
- // Do a request that throws error due to the invalid server certs
91
- const checkCertOptions = {
92
- hostname : '127.0.0.1' ,
93
- port : port ,
94
- path : '/' ,
95
- method : 'GET'
96
- } ;
97
-
98
- const checkCertReq = https . request ( checkCertOptions , function ( res ) {
99
- res . on ( 'data' , function ( ) {
100
- throw new Error ( 'data should not be received' ) ;
101
- } ) ;
102
-
103
- res . on ( 'end' , function ( ) {
104
- throw new Error ( 'connection should not be established' ) ;
105
- } ) ;
106
- } ) ;
107
- checkCertReq . end ( ) ;
81
+ // Do a request that errors due to the invalid server certs
82
+ options . rejectUnauthorized = true ;
83
+ tests ++ ;
84
+ const checkCertReq = https . request ( options , common . mustNotCall ( ) ) . end ( ) ;
108
85
109
86
checkCertReq . on ( 'error' , common . mustCall ( ( e ) => {
110
87
assert . strictEqual ( e . code , 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' ) ;
111
- testSucceeded ( ) ;
88
+ done ( ) ;
112
89
} ) ) ;
113
90
} ) ) ;
114
-
115
- process . on ( 'exit' , function ( ) {
116
- assert . strictEqual ( successful , tests ) ;
117
- } ) ;
0 commit comments