2121from unittest .mock import Mock , patch
2222
2323import requests_mock
24+ from requests .adapters import HTTPAdapter
2425from requests .exceptions import ConnectTimeout
2526
2627import libcloud .common .base
@@ -36,6 +37,7 @@ class BaseConnectionClassTestCase(unittest.TestCase):
3637 def setUp (self ):
3738 self .orig_http_proxy = os .environ .pop ("http_proxy" , None )
3839 self .orig_https_proxy = os .environ .pop ("https_proxy" , None )
40+ self .orig_no_proxy = os .environ .pop ("no_proxy" , None )
3941
4042 def tearDown (self ):
4143 if self .orig_http_proxy :
@@ -48,6 +50,11 @@ def tearDown(self):
4850 elif "https_proxy" in os .environ :
4951 del os .environ ["https_proxy" ]
5052
53+ if self .orig_no_proxy :
54+ os .environ ["no_proxy" ] = self .orig_no_proxy
55+ elif "no_proxy" in os .environ :
56+ del os .environ ["no_proxy" ]
57+
5158 libcloud .common .base .ALLOW_PATH_DOUBLE_SLASHES = False
5259
5360 def test_parse_proxy_url (self ):
@@ -104,22 +111,12 @@ def test_parse_proxy_url(self):
104111 )
105112
106113 def test_constructor (self ):
107- proxy_url = "http://127.0.0.2:3128"
108- os .environ ["http_proxy" ] = proxy_url
109- conn = LibcloudConnection (host = "localhost" , port = 80 )
110- self .assertEqual (conn .proxy_scheme , "http" )
111- self .assertEqual (conn .proxy_host , "127.0.0.2" )
112- self .assertEqual (conn .proxy_port , 3128 )
113- self .assertEqual (
114- conn .session .proxies ,
115- {"http" : "http://127.0.0.2:3128" , "https" : "http://127.0.0.2:3128" },
116- )
117-
118114 _ = os .environ .pop ("http_proxy" , None )
119115 conn = LibcloudConnection (host = "localhost" , port = 80 )
120116 self .assertIsNone (conn .proxy_scheme )
121117 self .assertIsNone (conn .proxy_host )
122118 self .assertIsNone (conn .proxy_port )
119+ self .assertTrue (conn .session .proxies is None or not conn .session .proxies )
123120
124121 proxy_url = "http://127.0.0.3:3128"
125122 conn .set_http_proxy (proxy_url = proxy_url )
@@ -143,7 +140,8 @@ def test_constructor(self):
143140
144141 os .environ ["http_proxy" ] = proxy_url
145142 proxy_url = "http://127.0.0.5:3128"
146- conn = LibcloudConnection (host = "localhost" , port = 80 , proxy_url = proxy_url )
143+ conn = LibcloudConnection (host = "localhost" , port = 80 )
144+ conn .set_http_proxy (proxy_url = proxy_url )
147145 self .assertEqual (conn .proxy_scheme , "http" )
148146 self .assertEqual (conn .proxy_host , "127.0.0.5" )
149147 self .assertEqual (conn .proxy_port , 3128 )
@@ -163,6 +161,43 @@ def test_constructor(self):
163161 {"http" : "https://127.0.0.6:3129" , "https" : "https://127.0.0.6:3129" },
164162 )
165163
164+ def test_proxy_environment_variables_respected (self ):
165+ """
166+ Test that proxy environment variables are respected by the underlying Requests library
167+ """
168+
169+ def mock_send (self , request , ** kwargs ):
170+ captured_proxies .update (kwargs .get ("proxies" , {}))
171+ nonlocal captured_url
172+ captured_url = request .url
173+ mock_response = Mock ()
174+ mock_response .status_code = 200
175+ mock_response .headers = {"content-type" : "application/json" , "location" : "" }
176+ mock_response .text = "OK"
177+ mock_response .history = [] # No redirects
178+ return mock_response
179+
180+ with patch .object (HTTPAdapter , "send" , mock_send ):
181+ os .environ ["http_proxy" ] = "http://proxy.example.com:8080"
182+ os .environ ["https_proxy" ] = "https://secure-proxy.example.com:8443"
183+ os .environ ["no_proxy" ] = "localhost,127.0.0.1"
184+ captured_proxies = {}
185+ captured_url = None
186+
187+ conn = LibcloudConnection (host = "localhost" , port = 80 )
188+ conn .request ("GET" , "/get" )
189+
190+ self .assertEqual (captured_proxies , {})
191+ self .assertIn ("localhost" , captured_url )
192+
193+ conn = LibcloudConnection (host = "test.com" , port = 80 )
194+ conn .request ("GET" , "/get" )
195+
196+ self .assertEqual (captured_proxies .get ("http" , None ), "http://proxy.example.com:8080" )
197+ self .assertEqual (
198+ captured_proxies .get ("https" , None ), "https://secure-proxy.example.com:8443"
199+ )
200+
166201 def test_connection_to_unusual_port (self ):
167202 conn = LibcloudConnection (host = "localhost" , port = 8080 )
168203 self .assertIsNone (conn .proxy_scheme )
0 commit comments