@@ -1472,13 +1472,7 @@ 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
- def fetch_file (self , path ):
1476
- context = None
1477
- if ssl is not None :
1478
- context = ssl .create_default_context ()
1479
- # allow self-signed certificates
1480
- context .check_hostname = False
1481
- context .verify_mode = ssl .CERT_NONE
1475
+ def fetch_file (self , path , context = None ):
1482
1476
req = urllib .request .Request (path , method = 'GET' )
1483
1477
with urllib .request .urlopen (req , context = context ) as res :
1484
1478
return res .read ()
@@ -1524,6 +1518,11 @@ def test_http_client(self):
1524
1518
1525
1519
@unittest .skipIf (ssl is None , "requires ssl" )
1526
1520
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
+
1527
1526
port = find_unused_port ()
1528
1527
bind = '127.0.0.1'
1529
1528
proc = spawn_python ('-u' , '-m' , 'http.server' , str (port ), '-b' , bind ,
@@ -1534,7 +1533,8 @@ def test_https_client(self):
1534
1533
self .addCleanup (kill_python , proc )
1535
1534
self .addCleanup (proc .terminate )
1536
1535
self .assertTrue (self .wait_for_server (proc , 'https' , port , bind ))
1537
- res = self .fetch_file (f'https://{ bind } :{ port } /{ self .served_file_name } ' )
1536
+ res = self .fetch_file (f'https://{ bind } :{ port } /{ self .served_file_name } ' ,
1537
+ context = context )
1538
1538
self .assertEqual (res , self .served_data )
1539
1539
1540
1540
0 commit comments