Skip to content

Commit

Permalink
Merge commit '81326a07d1936838d844690b468660452aafdea9'
Browse files Browse the repository at this point in the history
  • Loading branch information
Simple Geebus committed Feb 15, 2011
2 parents 7ea87e3 + 81326a0 commit e9eda24
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
27 changes: 23 additions & 4 deletions oauth2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,26 @@ def to_unicode_optional_iterator(x):
assert 'is not iterable' in str(e)
return x
else:
return [ to_unicode(e) for e in l ]
return [ to_unicode(e) for e in l ]

def to_utf8_optional_iterator(x):
"""
Raise TypeError if x is a str or if x is an iterable which
contains a str.
"""
if isinstance(x, basestring):
return to_utf8(x)

try:
l = list(x)
except TypeError, e:
assert 'is not iterable' in str(e)
return x
else:
return [ to_utf8_if_string(e) for e in l ]

def escape(s):
"""Escape a URL including any /."""
s = to_unicode(s)
return urllib.quote(s.encode('utf-8'), safe='~')

def generate_timestamp():
Expand Down Expand Up @@ -386,10 +401,14 @@ def to_header(self, realm=''):

def to_postdata(self):
"""Serialize as post data for a POST request."""
d = {}
for k, v in self.iteritems():
d[k.encode('utf-8')] = to_utf8_optional_iterator(v)

# tell urlencode to deal with sequence values and map them correctly
# to resulting querystring. for example self["k"] = ["v1", "v2"] will
# result in 'k=v1&k=v2' and not k=%5B%27v1%27%2C+%27v2%27%5D
return urllib.urlencode(self, True).replace('+', '%20')
return urllib.urlencode(d, True).replace('+', '%20')

def to_url(self):
"""Serialize as a URL for a GET request."""
Expand Down Expand Up @@ -797,7 +816,7 @@ def check(self, request, consumer, token, signature):

class SignatureMethod_HMAC_SHA1(SignatureMethod):
name = 'HMAC-SHA1'

def signing_base(self, request, consumer, token):
if not hasattr(request, 'normalized_url') or request.normalized_url is None:
raise ValueError("Base URL for request is not set.")
Expand Down
18 changes: 18 additions & 0 deletions tests/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,24 @@ def test_to_header(self):
for key, val in res.items():
self.assertEquals(val, params.get(key))

def test_to_postdata_nonascii(self):
realm = "http://sp.example.com/"

params = {
'nonasciithing': u'q\xbfu\xe9 ,aasp u?..a.s',
'oauth_version': "1.0",
'oauth_nonce': "4572616e48616d6d65724c61686176",
'oauth_timestamp': "137131200",
'oauth_consumer_key': "0685bd9184jfhq22",
'oauth_signature_method': "HMAC-SHA1",
'oauth_token': "ad180jjd733klru7",
'oauth_signature': "wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D",
}

req = oauth.Request("GET", realm, params)

self.failUnlessReallyEqual(req.to_postdata(), 'nonasciithing=q%C2%BFu%C3%A9%20%2Caasp%20u%3F..a.s&oauth_nonce=4572616e48616d6d65724c61686176&oauth_timestamp=137131200&oauth_consumer_key=0685bd9184jfhq22&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_token=ad180jjd733klru7&oauth_signature=wOJIO9A2W5mFwDgiDvZbTSMK%252FPY%253D')

def test_to_postdata(self):
realm = "http://sp.example.com/"

Expand Down

0 comments on commit e9eda24

Please sign in to comment.