Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixing parsing of unicode query string via parse_qsl

  • Loading branch information...
commit 5202c1c61d6c0c0e12b76ebc6fdc22d9193798da 1 parent 79b15dd
Max Countryman maxcountryman authored
Showing with 36 additions and 3 deletions.
  1. +1 −1  rauth/__init__.py
  2. +12 −2 rauth/service.py
  3. +23 −0 tests/test_service.py
2  rauth/__init__.py
View
@@ -6,4 +6,4 @@
'''
-__version__ = '0.4.5'
+__version__ = '0.4.6'
14 rauth/service.py
View
@@ -16,6 +16,14 @@
from datetime import datetime
+def parse_utf8_qsl(s):
+ if isinstance(s, unicode):
+ s = s.encode('utf-8')
+ else:
+ s = unicode(s, 'utf-8').encode('utf-8')
+ return parse_qsl(s)
+
+
class Request(object):
'''A container for common HTTP request methods.'''
def get(self, url, **kwargs):
@@ -70,7 +78,7 @@ def content(self):
try:
content = json.loads(self.response.content)
except ValueError:
- content = dict(parse_qsl(self.response.content))
+ content = dict(parse_utf8_qsl(self.response.content))
else:
content = self.response.content
return content
@@ -406,7 +414,9 @@ def get_request_token(self, method='GET', **kwargs):
response.raise_for_status()
- data = dict(parse_qsl(response.content))
+ encoded_content = response.content
+
+ data = dict(parse_utf8_qsl(encoded_content))
return data['oauth_token'], data['oauth_token_secret']
def get_authorize_url(self, request_token, **params):
23 tests/test_service.py
View
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
'''
rauth.test_oauth
----------------
@@ -432,3 +433,25 @@ def test_other_response(self, mock_request):
request_token='123',
request_token_secret='456')
self.assertEqual({'a': 'b'}, access_resp.content)
+
+ @patch.object(requests.Session, 'request')
+ def test_parse_utf8_qsl_non_unicode(self, mock_request):
+ mock_request.return_value = self.response
+
+ self.response.content = {'ü': 'b'}
+ access_resp = self.service.get_access_token(method='GET',
+ request_token='123',
+ request_token_secret='456')
+ self.assertEqual({'ü': 'b'}, access_resp.content)
+
+ @patch.object(requests.Session, 'request')
+ def test_parse_utf8_qsl_unicode(self, mock_request):
+ mock_request.return_value = self.response
+
+ self.response.content = unicode('oauth_token=a&oauth_token_secret=b',
+ 'utf-8')
+
+ request_token, request_token_secret = \
+ self.service.get_request_token('GET')
+ self.assertEqual(request_token, 'a')
+ self.assertEqual(request_token_secret, 'b')
Please sign in to comment.
Something went wrong with that request. Please try again.