<s> import pytest <EOL> from future . backports . http . cookiejar import FileCookieJar <EOL> from future . backports . http . cookiejar import http2time <EOL> from future . backports . http . cookies import SimpleCookie <EOL> from future . backports . urllib . parse import urlparse <EOL> from future . backports . urllib . parse import parse_qs <EOL> from oic . exception import UnSupported <EOL> from oic . oic import AuthorizationRequest <EOL> from oic . oic . message import AccessTokenRequest <EOL> from oic . oauth2 import util <EOL> __author__ = '<STR_LIT>' <EOL> def query_string_compare ( query_str1 , query_str2 ) : <EOL> return parse_qs ( query_str1 ) == parse_qs ( query_str2 ) <EOL> def url_compare ( url1 , url2 ) : <EOL> url1 = urlparse ( url1 ) <EOL> url2 = urlparse ( url2 ) <EOL> if url1 . scheme != url2 . scheme : <EOL> return False <EOL> if url1 . netloc != url2 . netloc : <EOL> return False <EOL> if url1 . path != url2 . path : <EOL> return False <EOL> if not query_string_compare ( url1 . query , url2 . query ) : <EOL> return False <EOL> if not query_string_compare ( url1 . fragment , url2 . fragment ) : <EOL> return False <EOL> return True <EOL> def test_get_or_post ( ) : <EOL> uri = u'<STR_LIT>' <EOL> method = '<STR_LIT:GET>' <EOL> values = { '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:code>' , '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> request = AuthorizationRequest ( ** values ) <EOL> path , body , ret_kwargs = util . get_or_post ( uri , method , request ) <EOL> assert url_compare ( path , <EOL> u"<STR_LIT>" ) <EOL> assert not body <EOL> assert not ret_kwargs <EOL> method = '<STR_LIT:POST>' <EOL> uri = u'<STR_LIT>' <EOL> values = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:code>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> request = AccessTokenRequest ( ** values ) <EOL> kwargs = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:key>' : [ ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> } <EOL> path , body , ret_kwargs = util . get_or_post ( uri , method , request , ** kwargs ) <EOL> assert path == u'<STR_LIT>' <EOL> assert url_compare ( "<STR_LIT>" . format ( body ) , <EOL> '<STR_LIT>' ) <EOL> assert ret_kwargs == { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:key>' : [ ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } <EOL> method = '<STR_LIT>' <EOL> with pytest . raises ( UnSupported ) : <EOL> util . get_or_post ( uri , method , request , ** kwargs ) <EOL> def test_set_cookie ( ) : <EOL> cookiejar = FileCookieJar ( ) <EOL> _cookie = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> c = SimpleCookie ( _cookie ) <EOL> domain_0 = "<STR_LIT>" <EOL> domain_1 = "<STR_LIT>" <EOL> max_age = "<STR_LIT>" <EOL> expires = http2time ( max_age ) <EOL> path = "<STR_LIT>" <EOL> c [ "<STR_LIT>" ] [ "<STR_LIT>" ] = max_age <EOL> c [ "<STR_LIT>" ] [ "<STR_LIT>" ] = domain_0 <EOL> c [ "<STR_LIT>" ] [ "<STR_LIT:path>" ] = path <EOL> c [ "<STR_LIT>" ] [ "<STR_LIT>" ] = domain_1 <EOL> util . set_cookie ( cookiejar , c ) <EOL> cookies = cookiejar . _cookies <EOL> c_0 = cookies [ domain_0 ] [ path ] [ "<STR_LIT>" ] <EOL> c_1 = cookies [ domain_1 ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> c_2 = cookies [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> assert not ( c_2 . domain_specified and c_2 . path_specified ) <EOL> assert c_1 . domain_specified and not c_1 . domain_initial_dot and not c_1 . path_specified <EOL> assert c_0 . domain_specified and c_0 . domain_initial_dot and c_0 . path_specified <EOL> assert c_0 . expires == expires <EOL> assert c_0 . domain == domain_0 <EOL> assert c_0 . name == "<STR_LIT>" <EOL> assert c_0 . path == path <EOL> assert c_0 . value == "<STR_LIT>" <EOL> assert not c_1 . expires <EOL> assert c_1 . domain == domain_1 <EOL> assert c_1 . name == "<STR_LIT>" <EOL> assert c_1 . path == "<STR_LIT>" <EOL> assert c_1 . value == "<STR_LIT>" <EOL> assert not c_2 . expires <EOL> assert c_2 . domain == "<STR_LIT>" <EOL> assert c_2 . name == "<STR_LIT>" <EOL> assert c_2 . path == "<STR_LIT>" <EOL> assert c_2 . value == "<STR_LIT>" <EOL> def test_match_to ( ) : <EOL> str0 = "<STR_LIT:abc>" <EOL> str1 = "<STR_LIT>" <EOL> str3 = "<STR_LIT>" <EOL> test_string = "<STR_LIT>" . format ( str0 , str1 , str3 ) <EOL> assert util . match_to_ ( str0 , test_string ) <EOL> assert not util . match_to_ ( str3 , test_string ) <EOL> list_of_str = [ "<STR_LIT>" , test_string , "<STR_LIT>" , str1 ] <EOL> assert util . match_to_ ( str0 , list_of_str ) <EOL> assert util . match_to_ ( str1 , list_of_str ) <EOL> assert not util . match_to_ ( str3 , list_of_str ) <EOL> def test_verify_header ( ) : <EOL> class FakeResponse ( ) : <EOL> def __init__ ( self , header ) : <EOL> self . headers = { "<STR_LIT>" : header } <EOL> self . text = "<STR_LIT>" <EOL> json_header = "<STR_LIT:application/json>" <EOL> jwt_header = "<STR_LIT>" <EOL> default_header = util . DEFAULT_POST_CONTENT_TYPE <EOL> plain_text_header = "<STR_LIT>" <EOL> undefined_header = "<STR_LIT>" <EOL> assert util . verify_header ( FakeResponse ( json_header ) , "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert util . verify_header ( FakeResponse ( jwt_header ) , "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert util . verify_header ( FakeResponse ( jwt_header ) , "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert util . verify_header ( FakeResponse ( default_header ) , <EOL> "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert util . verify_header ( FakeResponse ( plain_text_header ) , <EOL> "<STR_LIT>" ) == "<STR_LIT>" <EOL> with pytest . raises ( AssertionError ) : <EOL> util . verify_header ( FakeResponse ( json_header ) , "<STR_LIT>" ) <EOL> util . verify_header ( FakeResponse ( jwt_header ) , "<STR_LIT>" ) <EOL> util . verify_header ( FakeResponse ( default_header ) , "<STR_LIT>" ) <EOL> util . verify_header ( FakeResponse ( plain_text_header ) , "<STR_LIT>" ) <EOL> util . verify_header ( FakeResponse ( undefined_header ) , "<STR_LIT>" ) <EOL> with pytest . raises ( ValueError ) : <EOL> util . verify_header ( FakeResponse ( json_header ) , "<STR_LIT>" ) </s>
