@@ -248,7 +248,24 @@ def test_from_string(self):
248
248
new = oauth .Token .from_string (string )
249
249
self ._compare_tokens (new )
250
250
251
- class TestRequest (unittest .TestCase ):
251
+ class ReallyEqualMixin :
252
+ def failUnlessReallyEqual (self , a , b , msg = None ):
253
+ self .failUnlessEqual (a , b , msg = msg )
254
+ self .failUnlessEqual (type (a ), type (b ), msg = "a :: %r, b :: %r, %r" % (a , b , msg ))
255
+
256
+ class TestFuncs (unittest .TestCase ):
257
+ def test_to_unicode (self ):
258
+ self .failUnlessRaises (TypeError , oauth .to_unicode , '\xae ' )
259
+ self .failUnlessRaises (TypeError , oauth .to_unicode_optional_iterator , '\xae ' )
260
+ self .failUnlessRaises (TypeError , oauth .to_unicode_optional_iterator , ['\xae ' ])
261
+
262
+ self .failUnlessEqual (oauth .to_unicode (':-)' ), u':-)' )
263
+ self .failUnlessEqual (oauth .to_unicode (u'\u00ae ' ), u'\u00ae ' )
264
+ self .failUnlessEqual (oauth .to_unicode ('\xc2 \xae ' ), u'\u00ae ' )
265
+ self .failUnlessEqual (oauth .to_unicode_optional_iterator ([':-)' ]), [u':-)' ])
266
+ self .failUnlessEqual (oauth .to_unicode_optional_iterator ([u'\u00ae ' ]), [u'\u00ae ' ])
267
+
268
+ class TestRequest (unittest .TestCase , ReallyEqualMixin ):
252
269
def test_setter (self ):
253
270
url = "http://example.com"
254
271
method = "GET"
@@ -342,9 +359,11 @@ def test_get_nonoauth_parameters(self):
342
359
}
343
360
344
361
other_params = {
345
- 'foo' : 'baz' ,
346
- 'bar' : 'foo' ,
347
- 'multi' : ['FOO' ,'BAR' ]
362
+ u'foo' : u'baz' ,
363
+ u'bar' : u'foo' ,
364
+ u'multi' : [u'FOO' ,u'BAR' ],
365
+ u'uni_utf8' : u'\xae ' ,
366
+ u'uni_unicode' : u'\u00ae '
348
367
}
349
368
350
369
params = oauth_params
@@ -461,6 +480,24 @@ def test_to_url_with_query(self):
461
480
self .assertEquals (b ['max-contacts' ], ['10' ])
462
481
self .assertEquals (a , b )
463
482
483
+ def test_signature_base_string_nonascii (self ):
484
+ consumer = oauth .Consumer ('consumer_token' , 'consumer_secret' )
485
+
486
+ url = "http://api.simplegeo.com:80/1.0/places/address.json?q=monkeys&category=animal&address=41+Decatur+St%2C+San+Francisc%E2%9D%A6%2C+CA"
487
+ req = oauth .Request ("GET" , url )
488
+ self .failUnlessReallyEqual (req .normalized_url , u'http://api.simplegeo.com/1.0/places/address.json' )
489
+ self .assertEquals (req .url , u'http://api.simplegeo.com:80/1.0/places/address.json?q=monkeys&category=animal&address=41+Decatur+St%2C+San+Francisc%E2%9D%A6%2C+CA' )
490
+ req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), consumer , None )
491
+
492
+ def test_signature_base_string_nonascii_nonutf8 (self ):
493
+ consumer = oauth .Consumer ('consumer_token' , 'consumer_secret' )
494
+
495
+ url = "http://api.simplegeo.com:80/1.0/places/address.json?q=monkeys&category=animal&address=41+Decatur+St%2C+San+Francisc%E2%9D%A6%2C+CA"
496
+ req = oauth .Request ("GET" , url )
497
+ self .failUnlessReallyEqual (req .normalized_url , u'http://api.simplegeo.com/1.0/places/address.json' )
498
+ self .assertEquals (req .url , u'http://api.simplegeo.com:80/1.0/places/address.json?q=monkeys&category=animal&address=41+Decatur+St%2C+San+Francisc%E2%9D%A6%2C+CA' )
499
+ req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), consumer , None )
500
+
464
501
def test_signature_base_string_with_query (self ):
465
502
url = "https://www.google.com/m8/feeds/contacts/default/full/?alt=json&max-contacts=10"
466
503
params = {
@@ -495,16 +532,18 @@ def test_get_normalized_parameters(self):
495
532
'oauth_consumer_key' : "0685bd9184jfhq22" ,
496
533
'oauth_signature_method' : "HMAC-SHA1" ,
497
534
'oauth_token' : "ad180jjd733klru7" ,
498
- 'multi' : ['FOO' ,'BAR' ],
535
+ 'multi' : ['FOO' ,'BAR' , u'\u00ae ' , '\xc2 \xae ' ],
536
+ 'uni_utf8' : '\xc2 \xae ' ,
537
+ 'uni_unicode' : u'\u00ae '
499
538
}
500
539
501
540
req = oauth .Request ("GET" , url , params )
502
541
503
542
res = req .get_normalized_parameters ()
504
-
505
- srtd = [(k , v if type (v ) != ListType else sorted (v )) for k ,v in sorted (params .items ())]
506
543
507
- self .assertEquals (urllib .urlencode (srtd , True ), res )
544
+ expected = 'multi=BAR&multi=FOO&multi=%C2%AE&multi=%C2%AE&oauth_consumer_key=0685bd9184jfhq22&oauth_nonce=4572616e48616d6d65724c61686176&oauth_signature_method=HMAC-SHA1&oauth_timestamp=137131200&oauth_token=ad180jjd733klru7&oauth_version=1.0&uni_unicode=%C2%AE&uni_utf8=%C2%AE'
545
+
546
+ self .assertEquals (expected , res )
508
547
509
548
def test_get_normalized_parameters_ignores_auth_signature (self ):
510
549
url = "http://sp.example.com/"
@@ -558,24 +597,63 @@ def test_get_normalized_string_escapes_spaces_properly(self):
558
597
expected = urllib .urlencode (sorted (params .items ())).replace ('+' , '%20' )
559
598
self .assertEqual (expected , res )
560
599
561
- def test_request_nonascii_bytes (self ):
562
- # If someone has a sequence of bytes which is not ascii, we'll
563
- # raise an exception as early as possible.
564
- url = "http://sp.example.com/\x92 "
600
+ @mock .patch ('oauth2.Request.make_timestamp' )
601
+ @mock .patch ('oauth2.Request.make_nonce' )
602
+ def test_request_nonascii_bytes (self , mock_make_nonce , mock_make_timestamp ):
603
+ mock_make_nonce .return_value = 5
604
+ mock_make_timestamp .return_value = 6
565
605
606
+ tok = oauth .Token (key = "tok-test-key" , secret = "tok-test-secret" )
607
+ con = oauth .Consumer (key = "con-test-key" , secret = "con-test-secret" )
566
608
params = {
567
609
'oauth_version' : "1.0" ,
568
610
'oauth_nonce' : "4572616e48616d6d65724c61686176" ,
569
- 'oauth_timestamp' : "137131200"
611
+ 'oauth_timestamp' : "137131200" ,
612
+ 'oauth_token' : tok .key ,
613
+ 'oauth_consumer_key' : con .key
570
614
}
571
615
572
- tok = oauth .Token (key = "tok-test-key" , secret = "tok-test-secret" )
573
- con = oauth .Consumer (key = "con-test-key" , secret = "con-test-secret" )
616
+ # If someone passes a sequence of bytes which is not ascii for
617
+ # url, we'll raise an exception as early as possible.
618
+ url = "http://sp.example.com/\x92 " # It's actually cp1252-encoding...
619
+ self .assertRaises (TypeError , oauth .Request , method = "GET" , url = url , parameters = params )
574
620
575
- params ['oauth_token' ] = tok .key
576
- params ['oauth_consumer_key' ] = con .key
621
+ # And if they pass an unicode, then we'll use it.
622
+ url = u'http://sp.example.com/\u2019 '
623
+ req = oauth .Request (method = "GET" , url = url , parameters = params )
624
+ req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), con , None )
625
+ self .failUnlessReallyEqual (req ['oauth_signature' ], '/DgF7cY2friC01cmOAFdu8S0z+A=' )
626
+
627
+ # And if it is a utf-8-encoded-then-percent-encoded non-ascii
628
+ # thing, we'll decode it and use it.
629
+ url = "http://sp.example.com/%E2%80%99"
630
+ req = oauth .Request (method = "GET" , url = url , parameters = params )
631
+ req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), con , None )
632
+ self .failUnlessReallyEqual (req ['oauth_signature' ], 'anzjnpdqCUJWvePgDiwMb7Q8g28=' )
633
+
634
+ # Same thing with the params.
635
+ url = "http://sp.example.com/"
636
+
637
+ # If someone passes a sequence of bytes which is not ascii in
638
+ # params, we'll raise an exception as early as possible.
639
+ params ['non_oauth_thing' ] = '\xae ' , # It's actually cp1252-encoding...
577
640
self .assertRaises (TypeError , oauth .Request , method = "GET" , url = url , parameters = params )
578
641
642
+ # And if they pass a unicode, then we'll use it.
643
+ params ['non_oauth_thing' ] = u'\u2019 '
644
+ req = oauth .Request (method = "GET" , url = url , parameters = params )
645
+ req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), con , None )
646
+ self .failUnlessReallyEqual (req ['oauth_signature' ], 'QcgQMe9XzNxDWpechlQKFCd2orw=' )
647
+
648
+ # And if it is a utf-8-encoded non-ascii thing, we'll decode
649
+ # it and use it.
650
+ params ['non_oauth_thing' ] = '\xc2 \xae '
651
+ req = oauth .Request (method = "GET" , url = url , parameters = params )
652
+ req .sign_request (oauth .SignatureMethod_HMAC_SHA1 (), con , None )
653
+ self .failUnlessReallyEqual (req ['oauth_signature' ], 'OuMkgNFhlgcmEA1gIMII7aWLDgE=' )
654
+
655
+
656
+
579
657
def test_sign_request (self ):
580
658
url = "http://sp.example.com/"
581
659
0 commit comments