Skip to content

Commit d904bcd

Browse files
Implement support for HTTP proxies.
1 parent 7f85e5f commit d904bcd

6 files changed

Lines changed: 48 additions & 3 deletions

File tree

test/helper.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
import os
77
import httpretty
8+
from nose.exc import SkipTest
89

910
if sys.version_info < (2, 7):
1011
import unittest2 as unittest
@@ -34,8 +35,9 @@ def tearDown(self):
3435
httpretty.disable()
3536
httpretty.reset()
3637

37-
tinify.app_identifier = None
3838
tinify.key = None
39+
tinify.app_identifier = None
40+
tinify.proxy = None
3941
tinify.compression_count
4042

4143
def assertJsonEqual(self, expected, actual):

test/integration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class ClientIntegrationTest(unittest.TestCase):
99
tinify.key = os.environ.get("TINIFY_KEY")
10+
tinify.proxy = os.environ.get("TINIFY_PROXY")
1011

1112
unoptimized_path = os.path.join(os.path.dirname(__file__), 'examples', 'voormedia.png')
1213
optimized = tinify.from_file(unoptimized_path)

test/tinify_client_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ def test_should_issue_request_with_user_agent(self):
6666

6767
self.assertEqual(self.request.headers['user-agent'], Client.USER_AGENT + ' TestApp/0.2')
6868

69+
class TinifyClientRequestWhenValidWithProxy(TestHelper):
70+
def setUp(self):
71+
super(type(self), self).setUp()
72+
httpretty.register_uri(httpretty.CONNECT, 'http://localhost:8080', **{
73+
'compression-count': 12
74+
})
75+
76+
def test_should_issue_request_with_proxy_authorization(self):
77+
raise SkipTest('https://github.com/gabrielfalcao/HTTPretty/issues/122')
78+
Client('key', None, 'http://user:pass@localhost:8080').request('GET', '/')
79+
80+
self.assertEqual(self.request.headers['proxy-authorization'], 'Basic dXNlcjpwYXNz')
81+
6982
class TinifyClientRequestWithTimeout(TestHelper):
7083
@patch('requests.sessions.Session.request', RaiseException(requests.exceptions.Timeout))
7184
def test_should_raise_connection_error(self):

test/tinify_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ def test_should_reset_client_with_new_app_identifier(self):
2626
tinify.get_client().request('GET', '/')
2727
self.assertEqual(self.request.headers['user-agent'], tinify.Client.USER_AGENT + " MyApp/2.0")
2828

29+
class TinifyProxy(TestHelper):
30+
def test_should_reset_client_with_new_proxy(self):
31+
httpretty.register_uri(httpretty.CONNECT, 'http://localhost:8080')
32+
tinify.key = 'abcde'
33+
tinify.proxy = 'http://localhost:8080'
34+
tinify.get_client()
35+
tinify.proxy = 'http://localhost:8080'
36+
raise SkipTest('https://github.com/gabrielfalcao/HTTPretty/issues/122')
37+
tinify.get_client().request('GET', '/')
38+
self.assertEqual(self.request.headers['user-agent'], tinify.Client.USER_AGENT + " MyApp/2.0")
39+
2940
class TinifyClient(TestHelper):
3041
def test_with_key_should_return_client(self):
3142
tinify.key = 'abcde'
@@ -35,6 +46,12 @@ def test_without_key_should_raise_error(self):
3546
with self.assertRaises(tinify.AccountError):
3647
tinify.get_client()
3748

49+
def test_with_invalid_proxy_should_raise_error(self):
50+
with self.assertRaises(tinify.ConnectionError):
51+
tinify.key = 'abcde'
52+
tinify.proxy = 'http-bad-url'
53+
tinify.get_client().request('GET', '/')
54+
3855
class TinifyValidate(TestHelper):
3956
def test_with_valid_key_should_return_true(self):
4057
httpretty.register_uri(httpretty.POST, 'https://api.tinify.com/shrink', status=400,

tinify/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def __init__(self, module):
1212
self._client = None
1313
self._key = None
1414
self._app_identifier = None
15+
self._proxy = None
1516
self._compression_count = None
1617

1718
@property
@@ -32,6 +33,15 @@ def app_identifier(self, value):
3233
self._app_identifier = value
3334
self._client = None
3435

36+
@property
37+
def proxy(self):
38+
return self._key
39+
40+
@key.setter
41+
def proxy(self, value):
42+
self._proxy = value
43+
self._client = None
44+
3545
@property
3646
def compression_count(self):
3747
return self._compression_count
@@ -47,7 +57,7 @@ def get_client(self):
4757
if not self._client:
4858
with self._lock:
4959
if not self._client:
50-
self._client = Client(self._key, self._app_identifier)
60+
self._client = Client(self._key, self._app_identifier, self._proxy)
5161

5262
return self._client
5363

tinify/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ class Client(object):
1616
API_ENDPOINT = 'https://api.tinify.com'
1717
USER_AGENT = 'Tinify/{0} Python/{1} ({2})'.format(tinify.__version__, platform.python_version(), platform.python_implementation())
1818

19-
def __init__(self, key, app_identifier=None):
19+
def __init__(self, key, app_identifier=None, proxy=None):
2020
self.session = requests.sessions.Session()
21+
if proxy:
22+
self.session.proxies = {'https': proxy}
2123
self.session.auth = ('api', key)
2224
self.session.headers = {
2325
'user-agent': self.USER_AGENT + ' ' + app_identifier if app_identifier else self.USER_AGENT,

0 commit comments

Comments
 (0)