<s> import pycurl <EOL> import json <EOL> from grab import Grab <EOL> from grab . error import GrabMisuseError <EOL> from grab . cookie import CookieManager , create_cookie <EOL> import pickle <EOL> from test . util import temp_file , build_grab , exclude_transport <EOL> from test . util import BaseGrabTestCase <EOL> class TestCookies ( BaseGrabTestCase ) : <EOL> def setUp ( self ) : <EOL> self . server . reset ( ) <EOL> def test_parsing_response_cookies ( self ) : <EOL> g = build_grab ( ) <EOL> self . server . response [ '<STR_LIT>' ] = { '<STR_LIT:foo>' : '<STR_LIT:bar>' , '<STR_LIT:1>' : '<STR_LIT:2>' } . items ( ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> self . assertEqual ( g . response . cookies [ '<STR_LIT:foo>' ] , '<STR_LIT:bar>' ) <EOL> def test_multiple_cookies ( self ) : <EOL> g = build_grab ( ) <EOL> self . server . response [ '<STR_LIT>' ] = [ ] <EOL> g . setup ( cookies = { '<STR_LIT:foo>' : '<STR_LIT:1>' , '<STR_LIT:bar>' : '<STR_LIT:2>' } ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> self . assertEqual ( <EOL> set ( map ( lambda item : item . strip ( ) , <EOL> self . server . request [ '<STR_LIT>' ] [ '<STR_LIT>' ] . split ( '<STR_LIT>' ) ) ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> def test_session ( self ) : <EOL> g = build_grab ( ) <EOL> g . setup ( reuse_cookies = True ) <EOL> self . server . response [ '<STR_LIT>' ] = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } . items ( ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> self . assertEqual ( g . response . cookies [ '<STR_LIT:foo>' ] , '<STR_LIT:bar>' ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> self . assertEqual ( self . server . request [ '<STR_LIT>' ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> self . assertEqual ( self . server . request [ '<STR_LIT>' ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> g = build_grab ( ) <EOL> g . setup ( reuse_cookies = False ) <EOL> self . server . response [ '<STR_LIT>' ] = { '<STR_LIT:foo>' : '<STR_LIT>' } . items ( ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> self . assertEqual ( g . response . cookies [ '<STR_LIT:foo>' ] , '<STR_LIT>' ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> self . assertTrue ( len ( self . server . request [ '<STR_LIT>' ] ) == <NUM_LIT:0> ) <EOL> g = build_grab ( ) <EOL> g . setup ( reuse_cookies = True ) <EOL> self . server . response [ '<STR_LIT>' ] = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } . items ( ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> self . assertEqual ( g . response . cookies [ '<STR_LIT:foo>' ] , '<STR_LIT:bar>' ) <EOL> g . clear_cookies ( ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> self . assertTrue ( len ( self . server . request [ '<STR_LIT>' ] ) == <NUM_LIT:0> ) <EOL> def test_redirect_session ( self ) : <EOL> g = build_grab ( ) <EOL> self . server . response [ '<STR_LIT>' ] = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } . items ( ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> self . assertEqual ( g . response . cookies [ '<STR_LIT:foo>' ] , '<STR_LIT:bar>' ) <EOL> g = build_grab ( ) <EOL> self . server . response [ '<STR_LIT>' ] = { } <EOL> self . server . response_once [ '<STR_LIT>' ] = [ <EOL> ( '<STR_LIT>' , self . server . get_url ( ) ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> self . server . response_once [ '<STR_LIT:code>' ] = <NUM_LIT> <EOL> g . go ( self . server . get_url ( ) ) <EOL> self . assertEqual ( self . server . request [ '<STR_LIT>' ] [ '<STR_LIT:foo>' ] . value , '<STR_LIT:bar>' ) <EOL> def test_load_dump ( self ) : <EOL> with temp_file ( ) as tmp_file : <EOL> g = build_grab ( ) <EOL> cookies = { '<STR_LIT:foo>' : '<STR_LIT:bar>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> g . setup ( cookies = cookies ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> g . dump_cookies ( tmp_file ) <EOL> self . assertEqual ( set ( cookies . items ( ) ) , <EOL> set ( ( x [ '<STR_LIT:name>' ] , x [ '<STR_LIT:value>' ] ) <EOL> for x in json . load ( open ( tmp_file ) ) ) ) <EOL> g = build_grab ( ) <EOL> cookies = { '<STR_LIT:foo>' : '<STR_LIT:bar>' , '<STR_LIT>' : u'<STR_LIT>' } <EOL> g . setup ( cookies = cookies ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> g . dump_cookies ( tmp_file ) <EOL> self . assertEqual ( set ( cookies . items ( ) ) , <EOL> set ( ( x [ '<STR_LIT:name>' ] , x [ '<STR_LIT:value>' ] ) <EOL> for x in json . load ( open ( tmp_file ) ) ) ) <EOL> g = build_grab ( ) <EOL> cookies = [ { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:value>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT>' : self . server . address } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:value>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : self . server . address } ] <EOL> json . dump ( cookies , open ( tmp_file , '<STR_LIT:w>' ) ) <EOL> g . load_cookies ( tmp_file ) <EOL> self . assertEqual ( set ( g . cookies . items ( ) ) , <EOL> set ( ( x [ '<STR_LIT:name>' ] , x [ '<STR_LIT:value>' ] ) for x in cookies ) ) <EOL> def test_cookiefile_empty ( self ) : <EOL> with temp_file ( ) as tmp_file : <EOL> g = build_grab ( ) <EOL> open ( tmp_file , '<STR_LIT:w>' ) . write ( '<STR_LIT>' ) <EOL> g . setup ( cookiefile = tmp_file ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> def test_cookiefile ( self ) : <EOL> with temp_file ( ) as tmp_file : <EOL> g = build_grab ( ) <EOL> cookies = [ { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . server . address } ] <EOL> json . dump ( cookies , open ( tmp_file , '<STR_LIT:w>' ) ) <EOL> self . server . response [ '<STR_LIT>' ] = { '<STR_LIT>' : '<STR_LIT>' } . items ( ) <EOL> g . setup ( cookiefile = tmp_file , debug = True ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> self . assertEqual ( self . server . request [ '<STR_LIT>' ] [ '<STR_LIT>' ] . value , '<STR_LIT>' ) <EOL> MERGED_COOKIES = [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> self . assertEqual ( set ( MERGED_COOKIES ) , <EOL> set ( g . cookies . items ( ) ) ) <EOL> self . assertEqual ( set ( MERGED_COOKIES ) , <EOL> set ( ( x [ '<STR_LIT:name>' ] , x [ '<STR_LIT:value>' ] ) <EOL> for x in json . load ( open ( tmp_file ) ) ) ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> @ exclude_transport ( '<STR_LIT>' ) <EOL> def test_manual_dns ( self ) : <EOL> g = build_grab ( ) <EOL> g . transport . curl . setopt ( pycurl . RESOLVE , <EOL> [ '<STR_LIT>' % self . server . port ] ) <EOL> self . server . response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> g . go ( '<STR_LIT>' % self . server . port ) <EOL> self . assertEqual ( b'<STR_LIT>' , g . response . body ) <EOL> @ exclude_transport ( '<STR_LIT>' ) <EOL> def test_different_domains ( self ) : <EOL> g = build_grab ( ) <EOL> names = [ <EOL> '<STR_LIT>' % self . server . port , <EOL> '<STR_LIT>' % self . server . port , <EOL> ] <EOL> g . transport . curl . setopt ( pycurl . RESOLVE , names ) <EOL> self . server . response [ '<STR_LIT>' ] = { '<STR_LIT:foo>' : '<STR_LIT:foo>' } . items ( ) <EOL> g . go ( '<STR_LIT>' % self . server . port ) <EOL> self . assertEqual ( dict ( g . response . cookies . items ( ) ) , { '<STR_LIT:foo>' : '<STR_LIT:foo>' } ) <EOL> self . server . response [ '<STR_LIT>' ] = { '<STR_LIT:bar>' : '<STR_LIT:bar>' } . items ( ) <EOL> g . go ( '<STR_LIT>' % self . server . port ) <EOL> self . assertEqual ( dict ( g . response . cookies . items ( ) ) , { '<STR_LIT:foo>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT:bar>' : '<STR_LIT:bar>' } ) <EOL> @ exclude_transport ( '<STR_LIT>' ) <EOL> def test_cookie_domain ( self ) : <EOL> g = Grab ( ) <EOL> names = [ <EOL> '<STR_LIT>' % self . server . port , <EOL> ] <EOL> g . transport . curl . setopt ( pycurl . RESOLVE , names ) <EOL> g . cookies . set ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , domain = '<STR_LIT>' ) <EOL> g . go ( '<STR_LIT>' % self . server . port ) <EOL> def test_update_invalid_cookie ( self ) : <EOL> g = build_grab ( ) <EOL> self . assertRaises ( GrabMisuseError , g . cookies . update , None ) <EOL> self . assertRaises ( GrabMisuseError , g . cookies . update , '<STR_LIT>' ) <EOL> self . assertRaises ( GrabMisuseError , g . cookies . update , [ '<STR_LIT>' ] ) <EOL> def test_from_cookie_list ( self ) : <EOL> cookie = create_cookie ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , self . server . address ) <EOL> mgr = CookieManager . from_cookie_list ( [ cookie ] ) <EOL> test_cookie = [ x for x in mgr . cookiejar if x . name == '<STR_LIT:foo>' ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( cookie . name , test_cookie . name ) <EOL> mgr = CookieManager . from_cookie_list ( [ ] ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( list ( mgr . cookiejar ) ) ) <EOL> def test_pickle_serialization ( self ) : <EOL> cookie = create_cookie ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , self . server . address ) <EOL> mgr = CookieManager . from_cookie_list ( [ cookie ] ) <EOL> dump = pickle . dumps ( mgr ) <EOL> mgr2 = pickle . loads ( dump ) <EOL> self . assertEqual ( list ( mgr . cookiejar ) [ <NUM_LIT:0> ] . value , <EOL> list ( mgr2 . cookiejar ) [ <NUM_LIT:0> ] . value ) <EOL> def test_get_item ( self ) : <EOL> cookie = create_cookie ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , self . server . address ) <EOL> mgr = CookieManager . from_cookie_list ( [ cookie ] ) <EOL> self . assertEqual ( '<STR_LIT:bar>' , mgr [ '<STR_LIT:foo>' ] ) <EOL> self . assertRaises ( KeyError , lambda : mgr [ '<STR_LIT>' ] ) <EOL> @ exclude_transport ( '<STR_LIT>' ) <EOL> def test_dot_domain ( self ) : <EOL> g = build_grab ( debug = True ) <EOL> names = [ <EOL> '<STR_LIT>' % self . server . port , <EOL> '<STR_LIT>' % self . server . port , <EOL> ] <EOL> g . transport . curl . setopt ( pycurl . RESOLVE , names ) <EOL> self . server . response [ '<STR_LIT>' ] = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> ] <EOL> g . go ( '<STR_LIT>' % self . server . port ) <EOL> self . assertEqual ( dict ( g . response . cookies . items ( ) ) , { '<STR_LIT:foo>' : '<STR_LIT:foo>' } ) <EOL> g . go ( '<STR_LIT>' % self . server . port ) <EOL> self . assertEqual ( '<STR_LIT:foo>' , <EOL> self . server . request [ '<STR_LIT>' ] . get ( '<STR_LIT:foo>' ) . value ) <EOL> def test_path ( self ) : <EOL> self . server . response [ '<STR_LIT>' ] = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> g = build_grab ( ) <EOL> g . go ( self . server . get_url ( '<STR_LIT:/>' ) ) <EOL> g . go ( self . server . get_url ( '<STR_LIT:/>' ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( self . server . request [ '<STR_LIT>' ] ) ) <EOL> g = build_grab ( ) <EOL> g . go ( self . server . get_url ( '<STR_LIT:/>' ) ) <EOL> g . go ( self . server . get_url ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( self . server . request [ '<STR_LIT>' ] ) ) <EOL> @ exclude_transport ( '<STR_LIT>' ) <EOL> def test_common_case_www_domain ( self ) : <EOL> g = build_grab ( ) <EOL> names = [ <EOL> '<STR_LIT>' % self . server . port , <EOL> ] <EOL> g . transport . curl . setopt ( pycurl . RESOLVE , names ) <EOL> self . server . response [ '<STR_LIT>' ] = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> g . go ( '<STR_LIT>' % self . server . port ) <EOL> g . go ( '<STR_LIT>' % self . server . port ) <EOL> self . assertEqual ( '<STR_LIT:1>' , self . server . request [ '<STR_LIT>' ] . get ( '<STR_LIT:foo>' ) . value ) <EOL> self . assertEqual ( '<STR_LIT:2>' , self . server . request [ '<STR_LIT>' ] . get ( '<STR_LIT:bar>' ) . value ) <EOL> def test_cookie_merging_replace_with_cookies_option ( self ) : <EOL> with temp_file ( ) as tmp_file : <EOL> init_cookies = [ { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:value>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT>' : self . server . address } ] <EOL> json . dump ( init_cookies , open ( tmp_file , '<STR_LIT:w>' ) ) <EOL> g = build_grab ( debug = True ) <EOL> g . cookies . load_from_file ( tmp_file ) <EOL> cookies = { <EOL> '<STR_LIT:foo>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> g . setup ( cookies = cookies ) <EOL> g . go ( self . server . get_url ( ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( self . server . request [ '<STR_LIT>' ] . items ( ) ) ) <EOL> def test_cookie_merging_replace ( self ) : <EOL> g = Grab ( ) <EOL> g . cookies . set ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , self . server . address ) <EOL> g . cookies . set ( '<STR_LIT:foo>' , '<STR_LIT>' , self . server . address ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( g . cookies . items ( ) ) ) <EOL> g . cookies . set ( '<STR_LIT:foo>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( g . cookies . items ( ) ) ) <EOL> g . cookies . set ( '<STR_LIT:foo>' , '<STR_LIT>' , domain = '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( g . cookies . items ( ) ) ) </s>
