Skip to content

Commit e652d11

Browse files
author
Claudio
committed
adding one second rul shortening tolerance and its tests
1 parent 3ffdcfa commit e652d11

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

bitly.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616

1717
import simplejson
18-
import urllib,urllib2
18+
import urllib, urllib2
1919
import urlparse
2020
import string
2121

@@ -118,38 +118,40 @@ def setUrllib(self, urllib):
118118
def _getURL(self, verb, paramVal, more_params={}):
119119
if not isinstance(paramVal, list):
120120
paramVal = [paramVal]
121-
121+
122122
params = {
123123
'version':BITLY_API_VERSION,
124124
'format':'json',
125125
'login':self.login,
126126
'apiKey':self.apikey,
127127
}
128-
128+
129129
params.update(more_params)
130130
params = params.items()
131131

132132
verbParam = VERBS_PARAM[verb]
133133
if verbParam:
134134
for val in paramVal:
135-
params.append(( verbParam,val ))
136-
135+
params.append(( verbParam, val ))
136+
137137
encoded_params = urllib.urlencode(params)
138-
return "%s%s?%s" % (BITLY_BASE_URL,verb,encoded_params)
138+
return "%s%s?%s" % (BITLY_BASE_URL, verb, encoded_params)
139139

140140
def _fetchUrl(self, url):
141141
'''Fetch a URL
142-
142+
143143
Args:
144144
url: The URL to retrieve
145-
145+
146146
Returns:
147147
A string containing the body of the response.
148148
'''
149-
149+
150150
# Open and return the URL
151-
url_data = self._urllib.urlopen(url).read()
152-
return url_data
151+
152+
url_data = self._urllib.urlopen(url=url, timeout=1).read()
153+
return url_data
154+
153155

154156
@classmethod
155157
def _CheckForError(cls, data):
@@ -184,7 +186,7 @@ def __init__(self,user_clicks=None,total_clicks=None):
184186
@staticmethod
185187
def NewFromJsonDict(data):
186188
'''Create a new instance based on a JSON dict.
187-
189+
188190
Args:
189191
data: A JSON dict, as converted from the JSON in the bitly API
190192
Returns:

test/test_shortening_urls.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
#-*- coding:utf-8 -*-
33

44
from nose.tools import assert_raises
5+
from urllib2 import URLError
56
from mox import Mox
67

78
import bitly
8-
9+
from bitly import urllib2
910

1011
def test_urls_shortening_scenario_for_plain_urls():
1112
api = bitly.Api(login='jcfigueiredo', apikey='R_1cf5dc0fa14c2df34261fb620bd256aa')
@@ -95,4 +96,24 @@ def verifying_invalid_item():
9596
}
9697
assert_raises(bitly.BitlyError, bitly.Api._CheckForError, invalid_data)
9798

98-
99+
100+
def test_checking_for_timeout():
101+
api = bitly.Api(login='jcfigueiredo', apikey='R_1cf5dc0fa14c2df34261fb620bd256aa')
102+
yield verifying_the_the_original_url_return_on_timeout, api
103+
104+
def verifying_the_the_original_url_return_on_timeout(api):
105+
mox = Mox()
106+
107+
bitly_shortening_url = 'http://api.bit.ly/shorten?login=jcfigueiredo&version=2.0.1&apiKey=R_1cf5dc0fa14c2df34261fb620bd256aa&format=json&longUrl=http%3A%2F%2Fwww.matandorobosgigantes.com'
108+
109+
mox.StubOutWithMock(urllib2, "urlopen")
110+
urllib2.urlopen(url=bitly_shortening_url, timeout=1).AndRaise(URLError('urlopen error timed out'))
111+
112+
url_to_be_shortened = 'http://www.matandorobosgigantes.com'
113+
114+
try:
115+
mox.ReplayAll()
116+
assert_raises(URLError, api.shorten, url_to_be_shortened)
117+
mox.VerifyAll()
118+
except Exception:
119+
mox.UnsetStubs()

0 commit comments

Comments
 (0)