Skip to content

Commit 902490b

Browse files
committed
fix flaws pointed out by ieure, plus test failures
I'm not entirely sure it is appropriate to allow .url and .normalized_url to be non-existent in addition to allowing them to be None, but I don't really understand the intent of the url setter.
1 parent fcd8c32 commit 902490b

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

oauth2/__init__.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,18 @@ def build_xoauth_string(url, consumer, token=None):
8989

9090

9191
def check_for_bad_encoding(s):
92-
""" Returns None if s is a unicode or an ascii string, else
93-
returns the UnicodeDecodeError that results from attempting to
94-
decode it as ascii. """
95-
try:
96-
s.decode('ascii')
97-
except UnicodeDecodeError, le:
98-
return le
92+
""" Raise exception with instructive error message if s is not unicode or ascii. """
93+
if not isinstance(s, unicode):
94+
try:
95+
s.decode('ascii')
96+
except UnicodeDecodeError, le:
97+
raise TypeError('You are required to pass either a unicode object or an ascii string here. You passed a Python string object which contained non-ascii: %r. The UnicodeDecodeError that resulted from attempting to interpret it as ascii was: %s' % (s, le,))
9998

10099
def escape(s):
101100
"""Escape a URL including any /."""
102-
encerr = check_for_bad_encoding(s)
103-
if encerr:
104-
raise Error('You are required to pass either a unicode object or an ascii string here. You passed a Python string object which contained non-ascii: %r. The UnicodeDecodeError that resulted from attempting to interpret it as ascii was: %s' % (encerr,))
101+
check_for_bad_encoding(s)
105102
return urllib.quote(s.encode('utf-8'), safe='~')
106103

107-
108104
def generate_timestamp():
109105
"""Get seconds since epoch (UTC)."""
110106
return int(time.time())
@@ -288,11 +284,10 @@ class Request(dict):
288284
version = OAUTH_VERSION
289285

290286
def __init__(self, method=HTTP_METHOD, url=None, parameters=None):
291-
encerr = check_for_bad_encoding(url)
292-
if encerr:
293-
raise ValueError("You are required to pass either a unicode object or an ascii string for `url'. You passed a Python string object which contained non-ascii: %r. The UnicodeDecodeError that resulted from attempting to interpret it as ascii was: %s" % (url, encerr,))
287+
if url is not None:
288+
check_for_bad_encoding(url)
289+
self.url = unicode(url)
294290
self.method = method
295-
self.url = unicode(url)
296291
if parameters is not None:
297292
self.update(parameters)
298293

@@ -732,7 +727,7 @@ class SignatureMethod_HMAC_SHA1(SignatureMethod):
732727
name = 'HMAC-SHA1'
733728

734729
def signing_base(self, request, consumer, token):
735-
if request.normalized_url is None:
730+
if not hasattr(request, 'normalized_url') or request.normalized_url is None:
736731
raise ValueError("Base URL for request is not set.")
737732

738733
sig = (
@@ -750,7 +745,6 @@ def signing_base(self, request, consumer, token):
750745
def sign(self, request, consumer, token):
751746
"""Builds the base signature string."""
752747
key, raw = self.signing_base(request, consumer, token)
753-
print "raw: type: %s, str: %s, repr: %s, hexbytes: %s" % (type(raw), str(raw), repr(raw), ["0x%x" % ord(c) for c in raw])
754748

755749
hashed = hmac.new(key, raw, sha)
756750

tests/test_oauth.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,14 @@ def test_from_string(self):
249249
self._compare_tokens(new)
250250

251251
class TestRequest(unittest.TestCase):
252+
# def test_split(self):
253+
# r = 253G
252254
def test_setter(self):
253255
url = "http://example.com"
254256
method = "GET"
255257
req = oauth.Request(method)
256-
self.assertTrue(req.url is None)
257-
self.assertTrue(req.normalized_url is None)
258+
self.assertTrue(not hasattr(req, 'url') or req.url is None)
259+
self.assertTrue(not hasattr(req, 'normalized_url') or req.normalized_url is None)
258260

259261
def test_deleter(self):
260262
url = "http://example.com"
@@ -574,7 +576,7 @@ def test_request_nonascii_bytes(self):
574576

575577
params['oauth_token'] = tok.key
576578
params['oauth_consumer_key'] = con.key
577-
self.assertRaises(ValueError, oauth.Request, method="GET", url=url, parameters=params)
579+
self.assertRaises(TypeError, oauth.Request, method="GET", url=url, parameters=params)
578580

579581
def test_sign_request(self):
580582
url = "http://sp.example.com/"

0 commit comments

Comments
 (0)