Skip to content

Commit

Permalink
Merge pull request #271 from jdufresne/bytes
Browse files Browse the repository at this point in the history
Fix bytes/str mixups
  • Loading branch information
Lukasa authored Apr 17, 2017
2 parents 47748f6 + cee229f commit 6d341c1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 25 deletions.
27 changes: 6 additions & 21 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,11 @@
from StringIO import StringIO # python 2
import unittest

if sys.version[0] == '3':
bytes_type = bytes
else:
bytes_type = str


@mock.patch('oauthlib.oauth1.rfc5849.generate_timestamp')
@mock.patch('oauthlib.oauth1.rfc5849.generate_nonce')
class OAuth1Test(unittest.TestCase):

def setUp(self):
def converting_equals(a, b):
if isinstance(a, bytes_type):
a = a.decode('utf-8')
if isinstance(b, bytes_type):
b = b.decode('utf-8')
self.assertEquals(a, b)

self.assertEqual = converting_equals

def testFormEncoded(self, generate_nonce, generate_timestamp):
"""OAuth1 assumes form encoded if content type is not specified."""
generate_nonce.return_value = 'abc'
Expand All @@ -43,16 +28,16 @@ def testFormEncoded(self, generate_nonce, generate_timestamp):
a = r.prepare()

self.assertEqual(a.url, 'http://a.b/path?query=retain')
self.assertEqual(a.body, 'this=really&is=&+form=encoded')
self.assertEqual(a.headers.get('Content-Type'), 'application/x-www-form-urlencoded')
self.assertEqual(a.body, b'this=really&is=&+form=encoded')
self.assertEqual(a.headers.get('Content-Type'), b'application/x-www-form-urlencoded')

# guess content-type
r = requests.Request(method='POST', url='http://a.b/path?query=retain',
auth=oauth, data='this=really&is=&+form=encoded')
b = r.prepare()
self.assertEqual(b.url, 'http://a.b/path?query=retain')
self.assertEqual(b.body, 'this=really&is=&+form=encoded')
self.assertEqual(b.headers.get('Content-Type'), 'application/x-www-form-urlencoded')
self.assertEqual(b.body, b'this=really&is=&+form=encoded')
self.assertEqual(b.headers.get('Content-Type'), b'application/x-www-form-urlencoded')

self.assertEqual(a.headers.get('Authorization'),
b.headers.get('Authorization'))
Expand Down Expand Up @@ -120,11 +105,11 @@ def test_content_type_override(self, generate_nonce, generate_timestamp):
data = 'a'
r = requests.post('http://httpbin.org/get', data=data, auth=oauth)
self.assertEqual(r.request.headers.get('Content-Type'),
'application/x-www-form-urlencoded')
b'application/x-www-form-urlencoded')
r = requests.post('http://httpbin.org/get', auth=oauth, data=data,
headers={'Content-type': 'application/json'})
self.assertEqual(r.request.headers.get('Content-Type'),
'application/json')
b'application/json')


def test_register_client_class(self, generate_timestamp, generate_nonce):
Expand Down
5 changes: 1 addition & 4 deletions tests/test_oauth2_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ def test_add_token(self):
token = 'Bearer ' + self.token['access_token']

def verifier(r, **kwargs):
auth_header = r.headers.get('Authorization', None)
if 'Authorization'.encode('utf-8') in r.headers:
auth_header = r.headers['Authorization'.encode('utf-8')]
auth_header = r.headers.get(str('Authorization'), None)
self.assertEqual(auth_header, token)
resp = mock.MagicMock()
resp.cookes = []
Expand Down Expand Up @@ -248,4 +246,3 @@ def fake_send(r, **kwargs):
self.assertFalse(sess.authorized)
sess.fetch_token(url)
self.assertTrue(sess.authorized)

0 comments on commit 6d341c1

Please sign in to comment.