diff --git a/tests/unit/http/test_http_client.py b/tests/unit/http/test_http_client.py index 7fd727949c..c5dfa11891 100644 --- a/tests/unit/http/test_http_client.py +++ b/tests/unit/http/test_http_client.py @@ -134,6 +134,20 @@ def test_last_response_empty_on_error(self): self.assertIsNotNone(self.client.last_request) self.assertIsNone(self.client.last_response) + def test_request_with_proxy(self): + self.client.proxy = { + 'http': 'http://proxy.twilio.com:8118', + 'https': 'https://proxy.twilio.com:8118', + } + self.request_mock.url = 'https://api.twilio.com/' + self.request_mock.headers = {'Host': 'other.twilio.com'} + + response = self.client.request( + 'doesnt matter', 'doesnt matter', None, None, None, None, None, None) + + self.assertEqual('other.twilio.com', self.request_mock.headers['Host']) + self.assertEqual(200, response.status_code) + class TestHttpClientSession(unittest.TestCase): diff --git a/twilio/http/http_client.py b/twilio/http/http_client.py index 740b6520bf..0a1a314188 100644 --- a/twilio/http/http_client.py +++ b/twilio/http/http_client.py @@ -13,7 +13,7 @@ class TwilioHttpClient(HttpClient): """ General purpose HTTP Client for interacting with the Twilio API """ - def __init__(self, pool_connections=True, request_hooks=None, timeout=None, logger=_logger): + def __init__(self, pool_connections=True, request_hooks=None, timeout=None, logger=_logger, proxy=None): """ Constructor for the TwilioHttpClient @@ -22,6 +22,7 @@ def __init__(self, pool_connections=True, request_hooks=None, timeout=None, logg :param int timeout: Timeout for the requests. Timeout should never be zero (0) or less. :param logger + :param dict proxy: Http proxy for the requests session """ self.session = Session() if pool_connections else None self.last_request = None @@ -32,6 +33,7 @@ def __init__(self, pool_connections=True, request_hooks=None, timeout=None, logg if timeout is not None and timeout <= 0: raise ValueError(timeout) self.timeout = timeout + self.proxy = proxy def request(self, method, url, params=None, data=None, headers=None, auth=None, timeout=None, allow_redirects=False): @@ -74,6 +76,8 @@ def request(self, method, url, params=None, data=None, headers=None, auth=None, self.last_response = None session = self.session or Session() + if self.proxy: + session.proxies = proxy request = Request(**kwargs) self.last_request = TwilioRequest(**kwargs)