<s> from __future__ import absolute_import , unicode_literals <EOL> import sys <EOL> from . unittest import TestCase <EOL> from oauthlib . common import add_params_to_uri <EOL> from oauthlib . common import CaseInsensitiveDict <EOL> from oauthlib . common import extract_params <EOL> from oauthlib . common import generate_client_id <EOL> from oauthlib . common import generate_nonce <EOL> from oauthlib . common import generate_timestamp <EOL> from oauthlib . common import generate_token <EOL> from oauthlib . common import Request <EOL> from oauthlib . common import unicode_type <EOL> from oauthlib . common import urldecode <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> bytes_type = bytes <EOL> else : <EOL> bytes_type = lambda s , e : str ( s ) <EOL> PARAMS_DICT = { '<STR_LIT:foo>' : '<STR_LIT:bar>' , '<STR_LIT>' : '<STR_LIT>' , } <EOL> PARAMS_TWOTUPLE = [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> PARAMS_FORMENCODED = '<STR_LIT>' <EOL> URI = '<STR_LIT>' <EOL> class EncodingTest ( TestCase ) : <EOL> def test_urldecode ( self ) : <EOL> self . assertItemsEqual ( urldecode ( '<STR_LIT>' ) , [ ] ) <EOL> self . assertItemsEqual ( urldecode ( '<STR_LIT:=>' ) , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertItemsEqual ( urldecode ( '<STR_LIT>' ) , [ ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) ] ) <EOL> self . assertItemsEqual ( urldecode ( '<STR_LIT:+>' ) , [ ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) ] ) <EOL> self . assertItemsEqual ( urldecode ( '<STR_LIT>' ) , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertItemsEqual ( urldecode ( '<STR_LIT>' ) , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertItemsEqual ( urldecode ( '<STR_LIT>' ) , [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) ] ) <EOL> self . assertItemsEqual ( urldecode ( '<STR_LIT>' ) , <EOL> [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertItemsEqual ( urldecode ( '<STR_LIT>' ) , [ ( '<STR_LIT:foo>' , '<STR_LIT>' ) ] ) <EOL> self . assertItemsEqual ( urldecode ( '<STR_LIT>' ) , [ ( '<STR_LIT:foo>' , '<STR_LIT>' ) ] ) <EOL> self . assertItemsEqual ( urldecode ( '<STR_LIT>' ) , [ ( '<STR_LIT:foo>' , '<STR_LIT>' ) ] ) <EOL> self . assertItemsEqual ( urldecode ( '<STR_LIT>' ) , [ ( '<STR_LIT:foo>' , '<STR_LIT>' ) ] ) <EOL> self . assertItemsEqual ( urldecode ( '<STR_LIT>' ) , [ ( '<STR_LIT:foo>' , '<STR_LIT>' ) ] ) <EOL> self . assertItemsEqual ( urldecode ( '<STR_LIT>' ) , [ ( '<STR_LIT:foo>' , '<STR_LIT>' ) ] ) <EOL> self . assertRaises ( ValueError , urldecode , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , urldecode , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , urldecode , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , urldecode , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , urldecode , '<STR_LIT>' ) <EOL> class ParameterTest ( TestCase ) : <EOL> def test_extract_params_dict ( self ) : <EOL> self . assertItemsEqual ( extract_params ( PARAMS_DICT ) , PARAMS_TWOTUPLE ) <EOL> def test_extract_params_twotuple ( self ) : <EOL> self . assertItemsEqual ( extract_params ( PARAMS_TWOTUPLE ) , PARAMS_TWOTUPLE ) <EOL> def test_extract_params_formencoded ( self ) : <EOL> self . assertItemsEqual ( extract_params ( PARAMS_FORMENCODED ) , <EOL> PARAMS_TWOTUPLE ) <EOL> def test_extract_params_blank_string ( self ) : <EOL> self . assertItemsEqual ( extract_params ( '<STR_LIT>' ) , [ ] ) <EOL> def test_extract_params_empty_list ( self ) : <EOL> self . assertItemsEqual ( extract_params ( [ ] ) , [ ] ) <EOL> def test_extract_non_formencoded_string ( self ) : <EOL> self . assertEqual ( extract_params ( '<STR_LIT>' ) , None ) <EOL> def test_extract_invalid ( self ) : <EOL> self . assertEqual ( extract_params ( object ( ) ) , None ) <EOL> self . assertEqual ( extract_params ( [ ( '<STR_LIT>' ) ] ) , None ) <EOL> def test_add_params_to_uri ( self ) : <EOL> correct = '<STR_LIT>' % ( URI , PARAMS_FORMENCODED ) <EOL> self . assertURLEqual ( add_params_to_uri ( URI , PARAMS_DICT ) , correct ) <EOL> self . assertURLEqual ( add_params_to_uri ( URI , PARAMS_TWOTUPLE ) , correct ) <EOL> class GeneratorTest ( TestCase ) : <EOL> def test_generate_timestamp ( self ) : <EOL> timestamp = generate_timestamp ( ) <EOL> self . assertIsInstance ( timestamp , unicode_type ) <EOL> self . assertTrue ( int ( timestamp ) ) <EOL> self . assertGreater ( int ( timestamp ) , <NUM_LIT> ) <EOL> def test_generate_nonce ( self ) : <EOL> """<STR_LIT>""" <EOL> nonce = generate_nonce ( ) <EOL> for i in range ( <NUM_LIT:50> ) : <EOL> self . assertNotEqual ( nonce , generate_nonce ( ) ) <EOL> def test_generate_token ( self ) : <EOL> token = generate_token ( ) <EOL> self . assertEqual ( len ( token ) , <NUM_LIT:30> ) <EOL> token = generate_token ( length = <NUM_LIT> ) <EOL> self . assertEqual ( len ( token ) , <NUM_LIT> ) <EOL> token = generate_token ( length = <NUM_LIT:6> , chars = "<STR_LIT>" ) <EOL> self . assertEqual ( len ( token ) , <NUM_LIT:6> ) <EOL> for c in token : <EOL> self . assertIn ( c , "<STR_LIT>" ) <EOL> def test_generate_client_id ( self ) : <EOL> client_id = generate_client_id ( ) <EOL> self . assertEqual ( len ( client_id ) , <NUM_LIT:30> ) <EOL> client_id = generate_client_id ( length = <NUM_LIT> ) <EOL> self . assertEqual ( len ( client_id ) , <NUM_LIT> ) <EOL> client_id = generate_client_id ( length = <NUM_LIT:6> , chars = "<STR_LIT>" ) <EOL> self . assertEqual ( len ( client_id ) , <NUM_LIT:6> ) <EOL> for c in client_id : <EOL> self . assertIn ( c , "<STR_LIT>" ) <EOL> class RequestTest ( TestCase ) : <EOL> def test_non_unicode_params ( self ) : <EOL> r = Request ( <EOL> bytes_type ( '<STR_LIT>' , '<STR_LIT:utf-8>' ) , <EOL> http_method = bytes_type ( '<STR_LIT:GET>' , '<STR_LIT:utf-8>' ) , <EOL> body = bytes_type ( '<STR_LIT>' , '<STR_LIT:utf-8>' ) , <EOL> headers = { <EOL> bytes_type ( '<STR_LIT:a>' , '<STR_LIT:utf-8>' ) : bytes_type ( '<STR_LIT:b>' , '<STR_LIT:utf-8>' ) <EOL> } <EOL> ) <EOL> self . assertEqual ( r . uri , '<STR_LIT>' ) <EOL> self . assertEqual ( r . http_method , '<STR_LIT:GET>' ) <EOL> self . assertEqual ( r . body , '<STR_LIT>' ) <EOL> self . assertEqual ( r . decoded_body , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( r . headers , { '<STR_LIT:a>' : '<STR_LIT:b>' } ) <EOL> def test_none_body ( self ) : <EOL> r = Request ( URI ) <EOL> self . assertEqual ( r . decoded_body , None ) <EOL> def test_empty_list_body ( self ) : <EOL> r = Request ( URI , body = [ ] ) <EOL> self . assertEqual ( r . decoded_body , [ ] ) <EOL> def test_empty_dict_body ( self ) : <EOL> r = Request ( URI , body = { } ) <EOL> self . assertEqual ( r . decoded_body , [ ] ) <EOL> def test_empty_string_body ( self ) : <EOL> r = Request ( URI , body = '<STR_LIT>' ) <EOL> self . assertEqual ( r . decoded_body , [ ] ) <EOL> def test_non_formencoded_string_body ( self ) : <EOL> body = '<STR_LIT>' <EOL> r = Request ( URI , body = body ) <EOL> self . assertEqual ( r . decoded_body , None ) <EOL> def test_param_free_sequence_body ( self ) : <EOL> body = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:8> , <NUM_LIT> ] <EOL> r = Request ( URI , body = body ) <EOL> self . assertEqual ( r . decoded_body , None ) <EOL> def test_list_body ( self ) : <EOL> r = Request ( URI , body = PARAMS_TWOTUPLE ) <EOL> self . assertItemsEqual ( r . decoded_body , PARAMS_TWOTUPLE ) <EOL> def test_dict_body ( self ) : <EOL> r = Request ( URI , body = PARAMS_DICT ) <EOL> self . assertItemsEqual ( r . decoded_body , PARAMS_TWOTUPLE ) <EOL> def test_getattr_existing_attribute ( self ) : <EOL> r = Request ( URI , body = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , getattr ( r , '<STR_LIT:body>' ) ) <EOL> def test_getattr_return_default ( self ) : <EOL> r = Request ( URI , body = '<STR_LIT>' ) <EOL> actual_value = getattr ( r , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , actual_value ) <EOL> def test_getattr_raise_attribute_error ( self ) : <EOL> r = Request ( URI , body = '<STR_LIT>' ) <EOL> with self . assertRaises ( AttributeError ) : <EOL> getattr ( r , '<STR_LIT>' ) <EOL> def test_sanitizing_authorization_header ( self ) : <EOL> r = Request ( URI , headers = { '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> self . assertNotIn ( '<STR_LIT>' , repr ( r ) ) <EOL> self . assertIn ( '<STR_LIT>' , repr ( r ) ) <EOL> self . assertEqual ( r . headers [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_token_body ( self ) : <EOL> payload = '<STR_LIT>' <EOL> r = Request ( URI , body = payload ) <EOL> self . assertNotIn ( '<STR_LIT:bar>' , repr ( r ) ) <EOL> self . assertIn ( '<STR_LIT>' , repr ( r ) ) <EOL> payload = '<STR_LIT>' <EOL> r = Request ( URI , body = payload ) <EOL> self . assertNotIn ( '<STR_LIT:bar>' , repr ( r ) ) <EOL> self . assertIn ( '<STR_LIT>' , repr ( r ) ) <EOL> def test_password_body ( self ) : <EOL> payload = '<STR_LIT>' <EOL> r = Request ( URI , body = payload ) <EOL> self . assertNotIn ( '<STR_LIT:bar>' , repr ( r ) ) <EOL> self . assertIn ( '<STR_LIT>' , repr ( r ) ) <EOL> payload = '<STR_LIT>' <EOL> r = Request ( URI , body = payload ) <EOL> self . assertNotIn ( '<STR_LIT:bar>' , repr ( r ) ) <EOL> self . assertIn ( '<STR_LIT>' , repr ( r ) ) <EOL> class CaseInsensitiveDictTest ( TestCase ) : <EOL> def test_basic ( self ) : <EOL> cid = CaseInsensitiveDict ( { } ) <EOL> cid [ '<STR_LIT:a>' ] = '<STR_LIT:b>' <EOL> cid [ '<STR_LIT:c>' ] = '<STR_LIT:d>' <EOL> del cid [ '<STR_LIT:c>' ] <EOL> self . assertEqual ( cid [ '<STR_LIT:A>' ] , '<STR_LIT:b>' ) <EOL> self . assertEqual ( cid [ '<STR_LIT:a>' ] , '<STR_LIT:b>' ) </s>
