Skip to content

Commit d830961

Browse files
committed
Fix proxy tests for real this time, and actually test the proxy values make it to post
1 parent a2db8d8 commit d830961

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
lines changed

segment/analytics/request.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ def post(write_key, host=None, gzip=False, timeout=15, proxies=None, oauth_manag
4545
kwargs = {
4646
"data": data,
4747
"headers": headers,
48-
"timeout": 15,
48+
"timeout": timeout,
4949
}
5050

5151
if proxies:
5252
kwargs['proxies'] = proxies
53-
res = None
53+
5454
try:
55-
res = _session.post(url, data=data, headers=headers, timeout=timeout)
55+
res = _session.post(url, **kwargs)
5656
except Exception as e:
5757
log.error(e)
5858
raise e

segment/analytics/test/test_client.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,19 @@ def test_default_timeout_15(self):
344344
self.assertEqual(consumer.timeout, 15)
345345

346346
def test_proxies(self):
347-
client = Client('testsecret', proxies={'http':'203.243.63.16:80','https':'203.243.63.16:80'})
348-
success, msg = client.identify('userId', {'trait': 'value'})
349-
self.assertTrue(success)
347+
proxies={'http':'203.243.63.16:80','https':'203.243.63.16:80'}
348+
client = Client('testsecret', proxies=proxies)
349+
def mock_post_fn(*args, **kwargs):
350+
res = mock.Mock()
351+
res.status_code = 200
352+
res.json.return_value = {'code': 'success', 'message': 'success'}
353+
return res
354+
355+
with mock.patch('segment.analytics.request._session.post', side_effect=mock_post_fn) as mock_post:
356+
success, msg = client.identify('userId', {'trait': 'value'})
357+
client.flush()
358+
self.assertTrue(success)
359+
mock_post.assert_called_once()
360+
args, kwargs = mock_post.call_args
361+
self.assertIn('proxies', kwargs)
362+
self.assertEqual(kwargs['proxies'], proxies)

segment/analytics/test/test_consumer.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,23 @@ def mock_post_fn(_, data, **kwargs):
200200

201201
@classmethod
202202
def test_proxies(cls):
203-
consumer = Consumer(None, 'testsecret', proxies={'http':'203.243.63.16:80','https':'203.243.63.16:80'})
203+
proxies = {'http': '203.243.63.16:80', 'https': '203.243.63.16:80'}
204+
consumer = Consumer(None, 'testsecret', proxies=proxies)
204205
track = {
205206
'type': 'track',
206207
'event': 'python event',
207208
'userId': 'userId'
208209
}
209-
consumer.request([track])
210+
211+
def mock_post_fn(*args, **kwargs):
212+
res = mock.Mock()
213+
res.status_code = 200
214+
res.json.return_value = {'code': 'success', 'message': 'success'}
215+
return res
216+
217+
with mock.patch('segment.analytics.request._session.post', side_effect=mock_post_fn) as mock_post:
218+
consumer.request([track])
219+
mock_post.assert_called_once()
220+
args, kwargs = mock_post.call_args
221+
cls().assertIn('proxies', kwargs)
222+
cls().assertEqual(kwargs['proxies'], proxies)

segment/analytics/test/test_request.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33
import json
44
import requests
5+
from unittest import mock
56

67
from segment.analytics.request import post, DatetimeSerializer
78

@@ -53,10 +54,21 @@ def test_should_timeout(self):
5354
}], timeout=0.0001)
5455

5556
def test_proxies(self):
56-
res = post('testsecret', batch=[{
57-
'userId': 'userId',
58-
'event': 'python event',
59-
'type': 'track',
60-
'proxies': {'http':'203.243.63.16:80','https':'203.243.63.16:80'}
61-
}])
62-
self.assertEqual(res.status_code, 200)
57+
proxies = {'http': '203.243.63.16:80', 'https': '203.243.63.16:80'}
58+
def mock_post_fn(*args, **kwargs):
59+
res = mock.Mock()
60+
res.status_code = 200
61+
res.json.return_value = {'code': 'success', 'message': 'success'}
62+
return res
63+
64+
with mock.patch('segment.analytics.request._session.post', side_effect=mock_post_fn) as mock_post:
65+
res = post('testsecret', proxies= proxies, batch=[{
66+
'userId': 'userId',
67+
'event': 'python event',
68+
'type': 'track'
69+
}])
70+
self.assertEqual(res.status_code, 200)
71+
mock_post.assert_called_once()
72+
args, kwargs = mock_post.call_args
73+
self.assertIn('proxies', kwargs)
74+
self.assertEqual(kwargs['proxies'], proxies)

0 commit comments

Comments
 (0)