@@ -1472,6 +1472,14 @@ def setUp(self):
1472
1472
f .write (self .tls_password .encode ())
1473
1473
self .addCleanup (os_helper .unlink ,self .tls_password_file )
1474
1474
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
+
1475
1483
def fetch_file (self ,path ,context = None ):
1476
1484
req = urllib .request .Request (path ,method = 'GET' )
1477
1485
with urllib .request .urlopen (req ,context = context )as res :
@@ -1506,8 +1514,7 @@ def wait_for_server(self, proc, protocol, port, bind, timeout=50):
1506
1514
return False
1507
1515
1508
1516
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 ())
1511
1518
proc = spawn_python ('-u' ,'-m' ,'http.server' ,str (port ),'-b' ,bind ,
1512
1519
bufsize = 1 ,text = True )
1513
1520
self .addCleanup (kill_python ,proc )
@@ -1516,15 +1523,8 @@ def test_http_client(self):
1516
1523
res = self .fetch_file (f'http://{ bind } :{ port } /{ self .served_file_name } ' )
1517
1524
self .assertEqual (res ,self .served_data )
1518
1525
1519
- @unittest .skipIf (ssl is None ,"requires ssl" )
1520
1526
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 ())
1528
1528
proc = spawn_python ('-u' ,'-m' ,'http.server' ,str (port ),'-b' ,bind ,
1529
1529
'--tls-cert' ,self .tls_cert ,
1530
1530
'--tls-key' ,self .tls_key ,
@@ -1533,8 +1533,8 @@ def test_https_client(self):
1533
1533
self .addCleanup (kill_python ,proc )
1534
1534
self .addCleanup (proc .terminate )
1535
1535
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 () )
1538
1538
self .assertEqual (res ,self .served_data )
1539
1539
1540
1540