diff options
| author | David Cramer <dcramer@gmail.com> | 2015-02-20 10:32:21 -0800 |
|---|---|---|
| committer | David Cramer <dcramer@gmail.com> | 2015-02-20 10:32:21 -0800 |
| commit | 05cdaaaaa42de58ee50a315cb59838abccc0e443 (patch) | |
| tree | dae17be25b12fade21493ce8bf69f694bb3e0f69 | |
| parent | 3c4ec2d861df6ff75337aaf1a2a29ffa1c3369b7 (diff) | |
| download | raven-add-proxy.tar.gz | |
Add proxy option to requests transportadd-proxy
| -rw-r--r-- | raven/transport/registry.py | 9 | ||||
| -rw-r--r-- | raven/transport/requests.py | 11 | ||||
| -rwxr-xr-x | setup.py | 1 | ||||
| -rw-r--r-- | tests/transport/requests/tests.py | 30 |
4 files changed, 34 insertions, 17 deletions
diff --git a/raven/transport/registry.py b/raven/transport/registry.py index 11b8e86..7f22159 100644 --- a/raven/transport/registry.py +++ b/raven/transport/registry.py @@ -29,7 +29,6 @@ class TransportRegistry(object): def __init__(self, transports=None): # setup a default list of senders self._schemes = {} - self._transports = {} if transports: for transport in transports: @@ -58,11 +57,9 @@ class TransportRegistry(object): def get_transport(self, parsed_url, **options): full_url = parsed_url.geturl() - if full_url not in self._transports: - # Remove the options from the parsed_url - parsed_url = urlparse.urlparse(full_url.split('?')[0]) - self._transports[full_url] = self._schemes[parsed_url.scheme](parsed_url, **options) - return self._transports[full_url] + # Remove the options from the parsed_url + parsed_url = urlparse.urlparse(full_url.split('?')[0]) + return self._schemes[parsed_url.scheme](parsed_url, **options) def compute_scope(self, url, scope): """ diff --git a/raven/transport/requests.py b/raven/transport/requests.py index 29d9da4..1101a14 100644 --- a/raven/transport/requests.py +++ b/raven/transport/requests.py @@ -22,10 +22,9 @@ class RequestsHTTPTransport(HTTPTransport): scheme = ['requests+http', 'requests+https'] def __init__(self, parsed_url, timeout=defaults.TIMEOUT, verify_ssl=True, - ca_certs=defaults.CA_BUNDLE): + ca_certs=defaults.CA_BUNDLE, proxy=None): if not has_requests: raise ImportError('RequestsHTTPTransport requires requests.') - super(RequestsHTTPTransport, self).__init__(parsed_url, timeout=timeout, verify_ssl=verify_ssl, @@ -33,11 +32,17 @@ class RequestsHTTPTransport(HTTPTransport): # remove the requests+ from the protocol, as it is not a real protocol self._url = self._url.split('+', 1)[-1] + self.proxy = proxy def send(self, data, headers): if self.verify_ssl: # If SSL verification is enabled use the provided CA bundle to # perform the verification. self.verify_ssl = self.ca_certs + if self.proxy: + proxies = {self._url.split('://', 1)[0]: self.proxy} + else: + proxies = {} requests.post(self._url, data=data, headers=headers, - verify=self.verify_ssl, timeout=self.timeout) + verify=self.verify_ssl, timeout=self.timeout, + proxies=proxies) @@ -73,6 +73,7 @@ tests_require = [ 'pytest-cov>=1.4', 'pytest-django', 'requests', + 'responses', 'tornado', 'webob', 'webtest', diff --git a/tests/transport/requests/tests.py b/tests/transport/requests/tests.py index 97bc483..6e30f6f 100644 --- a/tests/transport/requests/tests.py +++ b/tests/transport/requests/tests.py @@ -1,20 +1,34 @@ from __future__ import absolute_import import mock +import responses from raven.utils.testutils import TestCase from raven.base import Client class RequestsTransportTest(TestCase): - def setUp(self): - self.client = Client( + @responses.activate + def test_does_send(self): + responses.add(responses.POST, 'http://localhost:8143/api/1/store/') + + client = Client( dsn="requests+http://some_username:some_password@localhost:8143/1", + raise_send_errors=True, ) - @mock.patch('raven.transport.requests.post') - def test_does_send(self, post): - self.client.captureMessage(message='foo') - self.assertEqual(post.call_count, 1) - expected_url = 'http://localhost:8143/api/1/store/' - self.assertEqual(expected_url, post.call_args[0][0]) + client.captureMessage(message='foo') + + @mock.patch('requests.Session.send') + def test_passes_proxies(self, mock_send): + client = Client( + dsn="requests+http://some_username:some_password@localhost:8143/1?proxy=https://example.com", + raise_send_errors=True, + ) + + client.captureMessage(message='foo') + + args, kwargs = mock_send.call_args + request = args[0] + assert request.url == 'http://localhost:8143/api/1/store/' + assert kwargs['proxies'] == {'http': 'https://example.com'} |
