@@ -37,16 +37,6 @@ const options = {
3737 cert : fixtures . readKey ( 'agent1-cert.pem' )
3838} ;
3939
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-
5040const body = 'hello world\n' ;
5141
5242const serverCallback = common . mustCall ( function ( req , res ) {
@@ -57,61 +47,44 @@ const serverCallback = common.mustCall(function(req, res) {
5747const server = https . createServer ( options , serverCallback ) ;
5848
5949server . listen ( 0 , common . mustCall ( ( ) => {
50+ let tests = 0 ;
51+
52+ function done ( ) {
53+ if ( -- tests === 0 )
54+ server . close ( ) ;
55+ }
56+
6057 // Do a request ignoring the unauthorized server certs
6158 const port = server . address ( ) . port ;
6259
63- const noCertCheckOptions = {
60+ const options = {
6461 hostname : '127.0.0.1' ,
6562 port : port ,
6663 path : '/' ,
6764 method : 'GET' ,
6865 rejectUnauthorized : false
6966 } ;
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 ) => {
7469 let responseBody = '' ;
7570 res . on ( 'data' , function ( d ) {
7671 responseBody = responseBody + d ;
7772 } ) ;
7873
7974 res . on ( 'end' , common . mustCall ( ( ) => {
8075 assert . strictEqual ( responseBody , body ) ;
81- testSucceeded ( ) ;
76+ done ( ) ;
8277 } ) ) ;
8378 } ) ) ;
8479 req . end ( ) ;
8580
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 ( ) ;
10885
10986 checkCertReq . on ( 'error' , common . mustCall ( ( e ) => {
11087 assert . strictEqual ( e . code , 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' ) ;
111- testSucceeded ( ) ;
88+ done ( ) ;
11289 } ) ) ;
11390} ) ) ;
114-
115- process . on ( 'exit' , function ( ) {
116- assert . strictEqual ( successful , tests ) ;
117- } ) ;
0 commit comments