@@ -1472,6 +1472,14 @@ def setUp(self):
14721472            f .write (self .tls_password .encode ())
14731473        self .addCleanup (os_helper .unlink , self .tls_password_file )
14741474
1475+     @unittest .skipIf (ssl  is  None , "requires ssl" ) 
1476+     def  get_ssl_context (self ):
1477+         context  =  ssl .create_default_context ()
1478+         # allow self-signed certificates 
1479+         context .check_hostname  =  False 
1480+         context .verify_mode  =  ssl .CERT_NONE 
1481+         return  context 
1482+ 
14751483    def  fetch_file (self , path , context = None ):
14761484        req  =  urllib .request .Request (path , method = 'GET' )
14771485        with  urllib .request .urlopen (req , context = context ) as  res :
@@ -1506,8 +1514,7 @@ def wait_for_server(self, proc, protocol, port, bind, timeout=50):
15061514        return  False 
15071515
15081516    def  test_http_client (self ):
1509-         port  =  find_unused_port ()
1510-         bind  =  '127.0.0.1' 
1517+         _ , (bind , port ) =  server ._get_best_family (None , find_unused_port ())
15111518        proc  =  spawn_python ('-u' , '-m' , 'http.server' , str (port ), '-b' , bind ,
15121519                            bufsize = 1 , text = True )
15131520        self .addCleanup (kill_python , proc )
@@ -1516,15 +1523,8 @@ def test_http_client(self):
15161523        res  =  self .fetch_file (f'http://{ bind } { port } { self .served_file_name }  )
15171524        self .assertEqual (res , self .served_data )
15181525
1519-     @unittest .skipIf (ssl  is  None , "requires ssl" ) 
15201526    def  test_https_client (self ):
1521-         context  =  ssl .create_default_context ()
1522-         # allow self-signed certificates 
1523-         context .check_hostname  =  False 
1524-         context .verify_mode  =  ssl .CERT_NONE 
1525- 
1526-         port  =  find_unused_port ()
1527-         bind  =  '127.0.0.1' 
1527+         _ , (bind , port ) =  server ._get_best_family (None , find_unused_port ())
15281528        proc  =  spawn_python ('-u' , '-m' , 'http.server' , str (port ), '-b' , bind ,
15291529                            '--tls-cert' , self .tls_cert ,
15301530                            '--tls-key' , self .tls_key ,
@@ -1533,8 +1533,8 @@ def test_https_client(self):
15331533        self .addCleanup (kill_python , proc )
15341534        self .addCleanup (proc .terminate )
15351535        self .assertTrue (self .wait_for_server (proc , 'https' , port , bind ))
1536-         res  =  self . fetch_file ( f'https://{ bind } { port } { self .served_file_name }  , 
1537-                                context = context )
1536+         url  =  f'https://{ bind } { port } { self .served_file_name }  
1537+         res   =   self . fetch_file ( url ,  context = self . get_ssl_context () )
15381538        self .assertEqual (res , self .served_data )
15391539
15401540
0 commit comments