3434
3535import oauth2 as oauth
3636
37+ try :
38+ unicode
39+ except NameError : #pragma NO COVER Py3k
40+ def u (x , encoding = 'ascii' ):
41+ if isinstance (x , str ):
42+ return x
43+ try :
44+ return x .decode (encoding )
45+ except AttributeError :
46+ return x
47+ raise ValueError ('WTF: %s' % x )
48+ else :
49+ def u (x , encoding = 'ascii' ):
50+ if isinstance (x , unicode ):
51+ return x
52+ try :
53+ return x .decode (encoding )
54+ except AttributeError :
55+ return x
56+ raise ValueError ('WTF: %s' % x )
57+
58+ _UEMPTY = u ('' )
59+ _UBLANK = u (' ' )
60+ _BSMILEY = b':-)'
61+ _USMILEY = u (_BSMILEY )
62+ _GLYPH = b'\xae '
63+ _UGLYPH = u (_GLYPH , 'latin1' )
64+ _U2019 = u (b'\xe2 \x80 \x99 ' , 'utf8' ) # u'\u2019'
65+ _U2766 = u (b'\xe2 \x9d \xa6 ' , 'utf8' ) # u'\u2766'
3766
3867class TestError (unittest .TestCase ):
3968 def test_message (self ):
@@ -80,7 +109,7 @@ def test_build_xoauth_string(self):
80109 parts = oauth_string .split (',' )
81110 for part in parts :
82111 var , val = part .split ('=' )
83- returned [var ] = val .strip ('"' )
112+ returned [var ] = val .strip ('"' )
84113
85114 self .assertEquals ('HMAC-SHA1' , returned ['oauth_signature_method' ])
86115 self .assertEquals ('user_token' , returned ['oauth_token' ])
@@ -271,51 +300,51 @@ def test_to_unicode(self):
271300 self .failUnlessRaises (TypeError ,
272301 oauth .to_unicode_optional_iterator , ['\xae ' ])
273302
274- self .failUnlessEqual (oauth .to_unicode (':-)' ), u':-)' )
275- self .failUnlessEqual (oauth .to_unicode (u' \u00ae ' ), u' \u00ae ' )
276- self .failUnlessEqual (oauth .to_unicode ('\xc2 \xae ' ), u' \u00ae ' )
303+ self .failUnlessEqual (oauth .to_unicode (_BSMILEY ), _USMILEY )
304+ self .failUnlessEqual (oauth .to_unicode (_UGLYPH ), _UGLYPH )
305+ self .failUnlessEqual (oauth .to_unicode ('\xc2 \xae ' ), _UGLYPH )
277306
278307 def test_to_utf8 (self ):
279308 self .failUnlessRaises (TypeError , oauth .to_utf8 , 0 )
280309 self .failUnlessRaises (TypeError , oauth .to_utf8 , '\x81 ' )
281- self .failUnlessEqual (oauth .to_utf8 (':-)' ), ':-)' )
282- self .failUnlessEqual (oauth .to_utf8 (u' \u00ae ' ),
283- u' \u00ae ' .encode ('utf8' ))
310+ self .failUnlessEqual (oauth .to_utf8 (_BSMILEY ), _BSMILEY )
311+ self .failUnlessEqual (oauth .to_utf8 (_UGLYPH ),
312+ _UGLYPH .encode ('utf8' ))
284313
285314 def test_to_unicode_if_string (self ):
286315 self .failUnless (oauth .to_unicode_if_string (self ) is self )
287- self .failUnlessEqual (oauth .to_unicode_if_string (':-)' ), u':-)' )
316+ self .failUnlessEqual (oauth .to_unicode_if_string (_BSMILEY ), _USMILEY )
288317
289318 def test_to_utf8_if_string (self ):
290319 self .failUnless (oauth .to_utf8_if_string (self ) is self )
291- self .failUnlessEqual (oauth .to_utf8_if_string (u':-)' ), u':-)' )
292- self .failUnlessEqual (oauth .to_utf8_if_string (u' \u00ae ' ),
293- u' \u00ae ' .encode ('utf8' ))
320+ self .failUnlessEqual (oauth .to_utf8_if_string (_USMILEY ), _USMILEY )
321+ self .failUnlessEqual (oauth .to_utf8_if_string (_UGLYPH ),
322+ _UGLYPH .encode ('utf8' ))
294323
295324 def test_to_unicode_optional_iterator (self ):
296- self .failUnlessEqual (oauth .to_unicode_optional_iterator (':-)' ),
297- u':-)' )
298- self .failUnlessEqual (oauth .to_unicode_optional_iterator (u' \u00ae ' ),
299- u' \u00ae ' )
300- self .failUnlessEqual (oauth .to_unicode_optional_iterator ([':-)' ]),
301- [u':-)' ])
302- self .failUnlessEqual (oauth .to_unicode_optional_iterator ([u' \u00ae ' ]),
303- [u' \u00ae ' ])
304- self .failUnlessEqual (oauth .to_unicode_optional_iterator ((u' \u00ae ' ,)),
305- [u' \u00ae ' ])
325+ self .failUnlessEqual (oauth .to_unicode_optional_iterator (_BSMILEY ),
326+ _USMILEY )
327+ self .failUnlessEqual (oauth .to_unicode_optional_iterator (_UGLYPH ),
328+ _UGLYPH )
329+ self .failUnlessEqual (oauth .to_unicode_optional_iterator ([_BSMILEY ]),
330+ [_USMILEY ])
331+ self .failUnlessEqual (oauth .to_unicode_optional_iterator ([_UGLYPH ]),
332+ [_UGLYPH ])
333+ self .failUnlessEqual (oauth .to_unicode_optional_iterator ((_UGLYPH ,)),
334+ [_UGLYPH ])
306335 self .failUnless (oauth .to_unicode_optional_iterator (self ) is self )
307336
308337 def test_to_utf8_optional_iterator (self ):
309- self .failUnlessEqual (oauth .to_utf8_optional_iterator (':-)' ),
310- ':-)' )
311- self .failUnlessEqual (oauth .to_utf8_optional_iterator (u' \u00ae ' ),
312- u' \u00ae ' .encode ('utf8' ))
313- self .failUnlessEqual (oauth .to_utf8_optional_iterator ([':-)' ]),
314- [u':-)' ])
315- self .failUnlessEqual (oauth .to_utf8_optional_iterator ([u' \u00ae ' ]),
316- [u' \u00ae ' .encode ('utf8' )])
317- self .failUnlessEqual (oauth .to_utf8_optional_iterator ((u' \u00ae ' ,)),
318- [u' \u00ae ' .encode ('utf8' )])
338+ self .failUnlessEqual (oauth .to_utf8_optional_iterator (_BSMILEY ),
339+ _BSMILEY )
340+ self .failUnlessEqual (oauth .to_utf8_optional_iterator (_UGLYPH ),
341+ _UGLYPH .encode ('utf8' ))
342+ self .failUnlessEqual (oauth .to_utf8_optional_iterator ([_BSMILEY ]),
343+ [_USMILEY ])
344+ self .failUnlessEqual (oauth .to_utf8_optional_iterator ([_UGLYPH ]),
345+ [_UGLYPH .encode ('utf8' )])
346+ self .failUnlessEqual (oauth .to_utf8_optional_iterator ((_UGLYPH ,)),
347+ [_UGLYPH .encode ('utf8' )])
319348 self .failUnless (oauth .to_utf8_optional_iterator (self ) is self )
320349
321350class TestRequest (unittest .TestCase , ReallyEqualMixin ):
@@ -414,12 +443,12 @@ def test_get_nonoauth_parameters(self):
414443 }
415444
416445 other_params = {
417- u'foo' : u'baz' ,
418- u'bar' : u'foo' ,
419- u'multi' : [u'FOO' , u 'BAR' ],
420- u'uni_utf8' : u'\xae ' ,
421- u'uni_unicode' : u' \u00ae ' ,
422- u'uni_unicode_2' : u'åÅøØ' ,
446+ u ( 'foo' ) : u ( 'baz' ) ,
447+ u ( 'bar' ) : u ( 'foo' ) ,
448+ u ( 'multi' ) : [u ( 'FOO' ), u ( 'BAR' ) ],
449+ u ( 'uni_utf8' ) : u ( b '\xae ', 'latin1' ) ,
450+ u ( 'uni_unicode' ): _UGLYPH ,
451+ u ( 'uni_unicode_2' ) : u ( b 'åÅøØ', 'latin1' ) ,
423452 }
424453
425454 params = oauth_params
@@ -465,7 +494,7 @@ def test_to_postdata_nonascii(self):
465494 realm = "http://sp.example.com/"
466495
467496 params = {
468- 'nonasciithing' : u'q\xbf u\xe9 ,aasp u?..a.s' ,
497+ 'nonasciithing' : u ( 'q\xbf u\xe9 ,aasp u?..a.s' , 'latin1' ) ,
469498 'oauth_version' : "1.0" ,
470499 'oauth_nonce' : "4572616e48616d6d65724c61686176" ,
471500 'oauth_timestamp' : "137131200" ,
@@ -535,7 +564,7 @@ def test_to_url(self):
535564 a = urlparse .parse_qs (exp .query )
536565 b = urlparse .parse_qs (res .query )
537566 self .assertEquals (a , b )
538-
567+
539568 def test_to_url_with_query (self ):
540569 url = ("https://www.google.com/m8/feeds/contacts/default/full/"
541570 "?alt=json&max-contacts=10" )
@@ -570,13 +599,13 @@ def test_to_url_with_query(self):
570599 def test_signature_base_string_nonascii_nonutf8 (self ):
571600 consumer = oauth .Consumer ('consumer_token' , 'consumer_secret' )
572601
573- url = ( u 'http://api.simplegeo.com:80/1.0/places/address.json'
574- u '?q=monkeys&category=animal'
575- u '&address=41+Decatur+St,+San+Francisc\u2766 ,+CA' )
602+ url = u ( 'http://api.simplegeo.com:80/1.0/places/address.json'
603+ '?q=monkeys&category=animal'
604+ '&address=41+Decatur+St,+San+Francisc' ) + _U2766 + u ( ' ,+CA' )
576605 req = oauth .Request ("GET" , url )
577606 self .failUnlessReallyEqual (
578607 req .normalized_url ,
579- u'http://api.simplegeo.com/1.0/places/address.json' )
608+ u ( 'http://api.simplegeo.com/1.0/places/address.json' ) )
580609 req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), consumer , None )
581610 self .failUnlessReallyEqual (
582611 req ['oauth_signature' ], 'WhufgeZKyYpKsI70GZaiDaYwl6g=' )
@@ -587,7 +616,7 @@ def test_signature_base_string_nonascii_nonutf8(self):
587616 req = oauth .Request ("GET" , url )
588617 self .failUnlessReallyEqual (
589618 req .normalized_url ,
590- u'http://api.simplegeo.com/1.0/places/address.json' )
619+ u ( 'http://api.simplegeo.com/1.0/places/address.json' ) )
591620 req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), consumer , None )
592621 self .failUnlessReallyEqual (
593622 req ['oauth_signature' ], 'WhufgeZKyYpKsI70GZaiDaYwl6g=' )
@@ -598,18 +627,18 @@ def test_signature_base_string_nonascii_nonutf8(self):
598627 req = oauth .Request ("GET" , url )
599628 self .failUnlessReallyEqual (
600629 req .normalized_url ,
601- u'http://api.simplegeo.com/1.0/places/address.json' )
630+ u ( 'http://api.simplegeo.com/1.0/places/address.json' ) )
602631 req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), consumer , None )
603632 self .failUnlessReallyEqual (
604633 req ['oauth_signature' ], 'WhufgeZKyYpKsI70GZaiDaYwl6g=' )
605634
606- url = ( u 'http://api.simplegeo.com:80/1.0/places/address.json'
607- u '?q=monkeys&category=animal'
608- u '&address=41+Decatur+St,+San+Francisc%E2%9D%A6,+CA' )
635+ url = u ( 'http://api.simplegeo.com:80/1.0/places/address.json'
636+ '?q=monkeys&category=animal'
637+ '&address=41+Decatur+St,+San+Francisc%E2%9D%A6,+CA' )
609638 req = oauth .Request ("GET" , url )
610639 self .failUnlessReallyEqual (
611640 req .normalized_url ,
612- u'http://api.simplegeo.com/1.0/places/address.json' )
641+ u ( 'http://api.simplegeo.com/1.0/places/address.json' ) )
613642 req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), consumer , None )
614643 self .failUnlessReallyEqual (
615644 req ['oauth_signature' ], 'WhufgeZKyYpKsI70GZaiDaYwl6g=' )
@@ -731,10 +760,10 @@ def test_get_normalized_parameters(self):
731760 'oauth_consumer_key' : "0685bd9184jfhq22" ,
732761 'oauth_signature_method' : "HMAC-SHA1" ,
733762 'oauth_token' : "ad180jjd733klru7" ,
734- 'multi' : ['FOO' ,'BAR' , u' \u00ae ' , '\xc2 \xae ' ],
763+ 'multi' : ['FOO' ,'BAR' , _UGLYPH , b '\xc2 \xae ' ],
735764 'multi_same' : ['FOO' ,'FOO' ],
736- 'uni_utf8_bytes' : '\xc2 \xae ' ,
737- 'uni_unicode_object' : u' \u00ae '
765+ 'uni_utf8_bytes' : b '\xc2 \xae ' ,
766+ 'uni_unicode_object' : _UGLYPH
738767 }
739768
740769 req = oauth .Request ("GET" , url , params )
@@ -830,7 +859,7 @@ def test_request_nonutf8_bytes(self, mock_make_nonce, mock_make_timestamp):
830859 oauth .Request , method = "GET" , url = url , parameters = params )
831860
832861 # And if they pass an unicode, then we'll use it.
833- url = u'http://sp.example.com/\u2019 '
862+ url = u ( 'http://sp.example.com/' ) + _U2019
834863 req = oauth .Request (method = "GET" , url = url , parameters = params )
835864 req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), con , None )
836865 self .failUnlessReallyEqual (
@@ -854,7 +883,7 @@ def test_request_nonutf8_bytes(self, mock_make_nonce, mock_make_timestamp):
854883 oauth .Request , method = "GET" , url = url , parameters = params )
855884
856885 # And if they pass a unicode, then we'll use it.
857- params ['non_oauth_thing' ] = u' \u2019 '
886+ params ['non_oauth_thing' ] = _U2019
858887 req = oauth .Request (method = "GET" , url = url , parameters = params )
859888 req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), con , None )
860889 self .failUnlessReallyEqual (
@@ -891,7 +920,7 @@ def test_request_hash_of_body(self):
891920 'oauth_consumer_key' : con .key
892921 }
893922
894- url = u'http://www.example.com/resource'
923+ url = u ( 'http://www.example.com/resource' )
895924 req = oauth .Request (method = "PUT" , url = url , parameters = params ,
896925 body = "Hello World!" , is_form_encoded = False )
897926 req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), con , None )
@@ -972,7 +1001,7 @@ def test_sign_request(self):
9721001 self .assertEquals (
9731002 req ['oauth_signature' ], 'loFvp5xC7YbOgd9exIO6TxB7H4s=' )
9741003
975- url = u'http://sp.example.com/\u2019 ' # Python unicode object
1004+ url = u ( 'http://sp.example.com/' ) + _U2019 # Python unicode object
9761005 req = oauth .Request (method = "GET" , url = url , parameters = params )
9771006 req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), con , tok )
9781007 self .assertEquals (
@@ -985,7 +1014,7 @@ def test_sign_request(self):
9851014 self .assertEquals (
9861015 req ['oauth_signature' ], 'IBw5mfvoCsDjgpcsVKbyvsDqQaU=' )
9871016
988- url = u'http://sp.example.com/?q=\u2019 ' # Python unicode object
1017+ url = u ( 'http://sp.example.com/?q=' ) + _U2019 # Python unicode object
9891018 req = oauth .Request (method = "GET" , url = url , parameters = params )
9901019 req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), con , tok )
9911020 self .assertEquals (
@@ -1173,7 +1202,7 @@ def test_build_authenticate_header(self):
11731202 server = oauth .Server ()
11741203 headers = server .build_authenticate_header ('example.com' )
11751204 self .assertTrue ('WWW-Authenticate' in headers )
1176- self .assertEquals ('OAuth realm="example.com"' ,
1205+ self .assertEquals ('OAuth realm="example.com"' ,
11771206 headers ['WWW-Authenticate' ])
11781207
11791208 def test_no_version (self ):
0 commit comments