34
34
35
35
import oauth2 as oauth
36
36
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'
37
66
38
67
class TestError (unittest .TestCase ):
39
68
def test_message (self ):
@@ -80,7 +109,7 @@ def test_build_xoauth_string(self):
80
109
parts = oauth_string .split (',' )
81
110
for part in parts :
82
111
var , val = part .split ('=' )
83
- returned [var ] = val .strip ('"' )
112
+ returned [var ] = val .strip ('"' )
84
113
85
114
self .assertEquals ('HMAC-SHA1' , returned ['oauth_signature_method' ])
86
115
self .assertEquals ('user_token' , returned ['oauth_token' ])
@@ -271,51 +300,51 @@ def test_to_unicode(self):
271
300
self .failUnlessRaises (TypeError ,
272
301
oauth .to_unicode_optional_iterator , ['\xae ' ])
273
302
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 )
277
306
278
307
def test_to_utf8 (self ):
279
308
self .failUnlessRaises (TypeError , oauth .to_utf8 , 0 )
280
309
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' ))
284
313
285
314
def test_to_unicode_if_string (self ):
286
315
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 )
288
317
289
318
def test_to_utf8_if_string (self ):
290
319
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' ))
294
323
295
324
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 ])
306
335
self .failUnless (oauth .to_unicode_optional_iterator (self ) is self )
307
336
308
337
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' )])
319
348
self .failUnless (oauth .to_utf8_optional_iterator (self ) is self )
320
349
321
350
class TestRequest (unittest .TestCase , ReallyEqualMixin ):
@@ -414,12 +443,12 @@ def test_get_nonoauth_parameters(self):
414
443
}
415
444
416
445
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' ) ,
423
452
}
424
453
425
454
params = oauth_params
@@ -465,7 +494,7 @@ def test_to_postdata_nonascii(self):
465
494
realm = "http://sp.example.com/"
466
495
467
496
params = {
468
- 'nonasciithing' : u'q\xbf u\xe9 ,aasp u?..a.s' ,
497
+ 'nonasciithing' : u ( 'q\xbf u\xe9 ,aasp u?..a.s' , 'latin1' ) ,
469
498
'oauth_version' : "1.0" ,
470
499
'oauth_nonce' : "4572616e48616d6d65724c61686176" ,
471
500
'oauth_timestamp' : "137131200" ,
@@ -535,7 +564,7 @@ def test_to_url(self):
535
564
a = urlparse .parse_qs (exp .query )
536
565
b = urlparse .parse_qs (res .query )
537
566
self .assertEquals (a , b )
538
-
567
+
539
568
def test_to_url_with_query (self ):
540
569
url = ("https://www.google.com/m8/feeds/contacts/default/full/"
541
570
"?alt=json&max-contacts=10" )
@@ -570,13 +599,13 @@ def test_to_url_with_query(self):
570
599
def test_signature_base_string_nonascii_nonutf8 (self ):
571
600
consumer = oauth .Consumer ('consumer_token' , 'consumer_secret' )
572
601
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' )
576
605
req = oauth .Request ("GET" , url )
577
606
self .failUnlessReallyEqual (
578
607
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' ) )
580
609
req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), consumer , None )
581
610
self .failUnlessReallyEqual (
582
611
req ['oauth_signature' ], 'WhufgeZKyYpKsI70GZaiDaYwl6g=' )
@@ -587,7 +616,7 @@ def test_signature_base_string_nonascii_nonutf8(self):
587
616
req = oauth .Request ("GET" , url )
588
617
self .failUnlessReallyEqual (
589
618
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' ) )
591
620
req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), consumer , None )
592
621
self .failUnlessReallyEqual (
593
622
req ['oauth_signature' ], 'WhufgeZKyYpKsI70GZaiDaYwl6g=' )
@@ -598,18 +627,18 @@ def test_signature_base_string_nonascii_nonutf8(self):
598
627
req = oauth .Request ("GET" , url )
599
628
self .failUnlessReallyEqual (
600
629
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' ) )
602
631
req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), consumer , None )
603
632
self .failUnlessReallyEqual (
604
633
req ['oauth_signature' ], 'WhufgeZKyYpKsI70GZaiDaYwl6g=' )
605
634
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' )
609
638
req = oauth .Request ("GET" , url )
610
639
self .failUnlessReallyEqual (
611
640
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' ) )
613
642
req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), consumer , None )
614
643
self .failUnlessReallyEqual (
615
644
req ['oauth_signature' ], 'WhufgeZKyYpKsI70GZaiDaYwl6g=' )
@@ -731,10 +760,10 @@ def test_get_normalized_parameters(self):
731
760
'oauth_consumer_key' : "0685bd9184jfhq22" ,
732
761
'oauth_signature_method' : "HMAC-SHA1" ,
733
762
'oauth_token' : "ad180jjd733klru7" ,
734
- 'multi' : ['FOO' ,'BAR' , u' \u00ae ' , '\xc2 \xae ' ],
763
+ 'multi' : ['FOO' ,'BAR' , _UGLYPH , b '\xc2 \xae ' ],
735
764
'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
738
767
}
739
768
740
769
req = oauth .Request ("GET" , url , params )
@@ -830,7 +859,7 @@ def test_request_nonutf8_bytes(self, mock_make_nonce, mock_make_timestamp):
830
859
oauth .Request , method = "GET" , url = url , parameters = params )
831
860
832
861
# 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
834
863
req = oauth .Request (method = "GET" , url = url , parameters = params )
835
864
req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), con , None )
836
865
self .failUnlessReallyEqual (
@@ -854,7 +883,7 @@ def test_request_nonutf8_bytes(self, mock_make_nonce, mock_make_timestamp):
854
883
oauth .Request , method = "GET" , url = url , parameters = params )
855
884
856
885
# And if they pass a unicode, then we'll use it.
857
- params ['non_oauth_thing' ] = u' \u2019 '
886
+ params ['non_oauth_thing' ] = _U2019
858
887
req = oauth .Request (method = "GET" , url = url , parameters = params )
859
888
req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), con , None )
860
889
self .failUnlessReallyEqual (
@@ -891,7 +920,7 @@ def test_request_hash_of_body(self):
891
920
'oauth_consumer_key' : con .key
892
921
}
893
922
894
- url = u'http://www.example.com/resource'
923
+ url = u ( 'http://www.example.com/resource' )
895
924
req = oauth .Request (method = "PUT" , url = url , parameters = params ,
896
925
body = "Hello World!" , is_form_encoded = False )
897
926
req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), con , None )
@@ -972,7 +1001,7 @@ def test_sign_request(self):
972
1001
self .assertEquals (
973
1002
req ['oauth_signature' ], 'loFvp5xC7YbOgd9exIO6TxB7H4s=' )
974
1003
975
- url = u'http://sp.example.com/\u2019 ' # Python unicode object
1004
+ url = u ( 'http://sp.example.com/' ) + _U2019 # Python unicode object
976
1005
req = oauth .Request (method = "GET" , url = url , parameters = params )
977
1006
req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), con , tok )
978
1007
self .assertEquals (
@@ -985,7 +1014,7 @@ def test_sign_request(self):
985
1014
self .assertEquals (
986
1015
req ['oauth_signature' ], 'IBw5mfvoCsDjgpcsVKbyvsDqQaU=' )
987
1016
988
- url = u'http://sp.example.com/?q=\u2019 ' # Python unicode object
1017
+ url = u ( 'http://sp.example.com/?q=' ) + _U2019 # Python unicode object
989
1018
req = oauth .Request (method = "GET" , url = url , parameters = params )
990
1019
req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), con , tok )
991
1020
self .assertEquals (
@@ -1173,7 +1202,7 @@ def test_build_authenticate_header(self):
1173
1202
server = oauth .Server ()
1174
1203
headers = server .build_authenticate_header ('example.com' )
1175
1204
self .assertTrue ('WWW-Authenticate' in headers )
1176
- self .assertEquals ('OAuth realm="example.com"' ,
1205
+ self .assertEquals ('OAuth realm="example.com"' ,
1177
1206
headers ['WWW-Authenticate' ])
1178
1207
1179
1208
def test_no_version (self ):
0 commit comments