Skip to content

Commit 7d0b8d7

Browse files
committed
Closes #91. Added robust UTF-8 support to Request.to_url().
1 parent a1f0bb4 commit 7d0b8d7

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

oauth2/__init__.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -418,24 +418,24 @@ def to_url(self):
418418
except AttributeError:
419419
# must be python <2.5
420420
query = base_url[4]
421-
query = parse_qs(query)
421+
query = parse_qs(to_utf8(query))
422422
for k, v in self.items():
423-
query.setdefault(k, []).append(to_utf8_optional_iterator(v))
423+
query.setdefault(to_utf8(k), []).append(to_utf8_optional_iterator(v))
424424

425425
try:
426-
scheme = base_url.scheme
427-
netloc = base_url.netloc
428-
path = base_url.path
429-
params = base_url.params
430-
fragment = base_url.fragment
426+
scheme = to_utf8(base_url.scheme)
427+
netloc = to_utf8(base_url.netloc)
428+
path = to_utf8(base_url.path)
429+
params = to_utf8(base_url.params)
430+
fragment = to_utf8(base_url.fragment)
431431
except AttributeError:
432432
# must be python <2.5
433-
scheme = base_url[0]
434-
netloc = base_url[1]
435-
path = base_url[2]
436-
params = base_url[3]
437-
fragment = base_url[5]
438-
433+
scheme = to_utf8(base_url[0])
434+
netloc = to_utf8(base_url[1])
435+
path = to_utf8(base_url[2])
436+
params = to_utf8(base_url[3])
437+
fragment = to_utf8(base_url[5])
438+
439439
url = (scheme, netloc, path, params,
440440
urllib.urlencode(query, True), fragment)
441441
return urlparse.urlunparse(url)

tests/test_oauth.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,33 @@ def test_get_nonoauth_parameters(self):
374374
req = oauth.Request("GET", "http://example.com", params)
375375
self.assertEquals(other_params, req.get_nonoauth_parameters())
376376

377+
def test_to_url_nonascii(self):
378+
url = "http://sp.example.com/"
379+
380+
params = {
381+
'nonasciithing': u'q\xbfu\xe9 ,aasp u?..a.s',
382+
'oauth_version': "1.0",
383+
'oauth_nonce': "4572616e48616d6d65724c61686176",
384+
'oauth_timestamp': "137131200",
385+
'oauth_consumer_key': "0685bd9184jfhq22",
386+
'oauth_signature_method': "HMAC-SHA1",
387+
'oauth_token': "ad180jjd733klru7",
388+
'oauth_signature': "wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D",
389+
}
390+
391+
req = oauth.Request("GET", url, params)
392+
res = urlparse.urlparse(req.to_url())
393+
394+
params['nonasciithing'] = params['nonasciithing'].encode('utf-8')
395+
exp = urlparse.urlparse("%s?%s" % (url, urllib.urlencode(params)))
396+
397+
self.assertEquals(exp.netloc, res.netloc)
398+
self.assertEquals(exp.path, res.path)
399+
400+
a = parse_qs(exp.query)
401+
b = parse_qs(res.query)
402+
self.assertEquals(a, b)
403+
377404
def test_to_url_works_with_non_ascii_parameters(self):
378405

379406
oauth_params = {

0 commit comments

Comments
 (0)