@@ -1472,11 +1472,15 @@ def setUp(self):
14721472 f .write (self .tls_password .encode ())
14731473 self .addCleanup (os_helper .unlink , self .tls_password_file )
14741474
1475- def fetch_file (self , path ):
1475+ @unittest .skipIf (ssl is None , "requires ssl" )
1476+ def get_ssl_context (self ):
14761477 context = ssl .create_default_context ()
14771478 # allow self-signed certificates
14781479 context .check_hostname = False
14791480 context .verify_mode = ssl .CERT_NONE
1481+ return context
1482+
1483+ def fetch_file (self , path , context = None ):
14801484 req = urllib .request .Request (path , method = 'GET' )
14811485 with urllib .request .urlopen (req , context = context ) as res :
14821486 return res .read ()
@@ -1510,8 +1514,7 @@ def wait_for_server(self, proc, protocol, port, bind, timeout=50):
15101514 return False
15111515
15121516 def test_http_client (self ):
1513- port = find_unused_port ()
1514- bind = '127.0.0.1'
1517+ _ , (bind , port ) = server ._get_best_family (None , find_unused_port ())
15151518 proc = spawn_python ('-u' , '-m' , 'http.server' , str (port ), '-b' , bind ,
15161519 bufsize = 1 , text = True )
15171520 self .addCleanup (kill_python , proc )
@@ -1521,8 +1524,7 @@ def test_http_client(self):
15211524 self .assertEqual (res , self .served_data )
15221525
15231526 def test_https_client (self ):
1524- port = find_unused_port ()
1525- bind = '127.0.0.1'
1527+ _ , (bind , port ) = server ._get_best_family (None , find_unused_port ())
15261528 proc = spawn_python ('-u' , '-m' , 'http.server' , str (port ), '-b' , bind ,
15271529 '--tls-cert' , self .tls_cert ,
15281530 '--tls-key' , self .tls_key ,
@@ -1531,7 +1533,8 @@ def test_https_client(self):
15311533 self .addCleanup (kill_python , proc )
15321534 self .addCleanup (proc .terminate )
15331535 self .assertTrue (self .wait_for_server (proc , 'https' , port , bind ))
1534- res = self .fetch_file (f'https://{ bind } :{ port } /{ self .served_file_name } ' )
1536+ url = f'https://{ bind } :{ port } /{ self .served_file_name } '
1537+ res = self .fetch_file (url , context = self .get_ssl_context ())
15351538 self .assertEqual (res , self .served_data )
15361539
15371540
0 commit comments