@@ -1542,6 +1542,113 @@ def test_xmlrpcserver_has_use_builtin_types_flag(self):
15421542 self .assertTrue (server .use_builtin_types )
15431543
15441544
1545+ class SafeTransportTestCase (unittest .TestCase ):
1546+ def test_init_default_context (self ):
1547+ t = xmlrpclib .SafeTransport ()
1548+ self .assertIsNone (t .context )
1549+
1550+ def test_init_with_context (self ):
1551+ import ssl
1552+ ctx = ssl .create_default_context ()
1553+ t = xmlrpclib .SafeTransport (context = ctx )
1554+ self .assertIs (t .context , ctx )
1555+
1556+ def test_init_with_datetime_and_builtin_types (self ):
1557+ import ssl
1558+ ctx = ssl .create_default_context ()
1559+ t = xmlrpclib .SafeTransport (use_datetime = True , use_builtin_types = True ,
1560+ context = ctx )
1561+ self .assertTrue (t ._use_datetime )
1562+ self .assertTrue (t ._use_builtin_types )
1563+ self .assertIs (t .context , ctx )
1564+
1565+ def test_init_with_headers (self ):
1566+ import ssl
1567+ ctx = ssl .create_default_context ()
1568+ headers = [('X-Custom' , 'value' )]
1569+ t = xmlrpclib .SafeTransport (headers = headers , context = ctx )
1570+ self .assertEqual (t ._headers , headers )
1571+ self .assertIs (t .context , ctx )
1572+
1573+ @mock .patch ('http.client.HTTPSConnection' )
1574+ def test_make_connection (self , mock_https_conn ):
1575+ mock_conn = mock .MagicMock ()
1576+ mock_https_conn .return_value = mock_conn
1577+
1578+ t = xmlrpclib .SafeTransport ()
1579+ result = t .make_connection ('example.com' )
1580+
1581+ mock_https_conn .assert_called_once_with ('example.com' , None ,
1582+ context = None )
1583+ self .assertIs (result , mock_conn )
1584+ self .assertEqual (t ._connection , ('example.com' , mock_conn ))
1585+
1586+ @mock .patch ('http.client.HTTPSConnection' )
1587+ def test_make_connection_with_context (self , mock_https_conn ):
1588+ import ssl
1589+ ctx = ssl .create_default_context ()
1590+ mock_conn = mock .MagicMock ()
1591+ mock_https_conn .return_value = mock_conn
1592+
1593+ t = xmlrpclib .SafeTransport (context = ctx )
1594+ result = t .make_connection ('example.com' )
1595+
1596+ mock_https_conn .assert_called_once_with ('example.com' , None ,
1597+ context = ctx )
1598+ self .assertIs (result , mock_conn )
1599+
1600+ @mock .patch ('http.client.HTTPSConnection' )
1601+ def test_make_connection_with_tuple_host (self , mock_https_conn ):
1602+ mock_conn = mock .MagicMock ()
1603+ mock_https_conn .return_value = mock_conn
1604+
1605+ x509 = {'certfile' : '/path/to/cert.pem' }
1606+ t = xmlrpclib .SafeTransport ()
1607+ result = t .make_connection (('example.com' , x509 ))
1608+
1609+ mock_https_conn .assert_called_once_with ('example.com' , None ,
1610+ context = None ,
1611+ certfile = '/path/to/cert.pem' )
1612+ self .assertIs (result , mock_conn )
1613+
1614+ @mock .patch ('http.client.HTTPSConnection' )
1615+ def test_make_connection_caches (self , mock_https_conn ):
1616+ mock_conn = mock .MagicMock ()
1617+ mock_https_conn .return_value = mock_conn
1618+
1619+ t = xmlrpclib .SafeTransport ()
1620+ conn1 = t .make_connection ('example.com' )
1621+ conn2 = t .make_connection ('example.com' )
1622+
1623+ self .assertEqual (mock_https_conn .call_count , 1 )
1624+ self .assertIs (conn1 , conn2 )
1625+
1626+ @mock .patch ('http.client.HTTPSConnection' )
1627+ def test_make_connection_different_hosts (self , mock_https_conn ):
1628+ mock_conn1 = mock .MagicMock ()
1629+ mock_conn2 = mock .MagicMock ()
1630+ mock_https_conn .side_effect = [mock_conn1 , mock_conn2 ]
1631+
1632+ t = xmlrpclib .SafeTransport ()
1633+ conn1 = t .make_connection ('host1.example.com' )
1634+ conn2 = t .make_connection ('host2.example.com' )
1635+
1636+ self .assertEqual (mock_https_conn .call_count , 2 )
1637+ self .assertIs (conn1 , mock_conn1 )
1638+ self .assertIs (conn2 , mock_conn2 )
1639+
1640+ def test_make_connection_no_https (self ):
1641+ old = http .client .HTTPSConnection
1642+ try :
1643+ del http .client .HTTPSConnection
1644+ t = xmlrpclib .SafeTransport ()
1645+ with self .assertRaises (NotImplementedError ) as cm :
1646+ t .make_connection ('example.com' )
1647+ self .assertIn ('HTTPS' , str (cm .exception ))
1648+ finally :
1649+ http .client .HTTPSConnection = old
1650+
1651+
15451652def setUpModule ():
15461653 thread_info = threading_helper .threading_setup ()
15471654 unittest .addModuleCleanup (threading_helper .threading_cleanup , * thread_info )
0 commit comments