<s> from django . contrib . messages import constants <EOL> from django . contrib . messages . tests . base import BaseTest <EOL> from django . contrib . messages . storage . cookie import ( CookieStorage , <EOL> MessageEncoder , MessageDecoder ) <EOL> from django . contrib . messages . storage . base import Message <EOL> from django . test . utils import override_settings <EOL> from django . utils import simplejson as json <EOL> def set_cookie_data ( storage , messages , invalid = False , encode_empty = False ) : <EOL> """<STR_LIT>""" <EOL> encoded_data = storage . _encode ( messages , encode_empty = encode_empty ) <EOL> if invalid : <EOL> encoded_data = encoded_data [ <NUM_LIT:1> : ] <EOL> storage . request . COOKIES = { CookieStorage . cookie_name : encoded_data } <EOL> if hasattr ( storage , '<STR_LIT>' ) : <EOL> del storage . _loaded_data <EOL> def stored_cookie_messages_count ( storage , response ) : <EOL> """<STR_LIT>""" <EOL> cookie = response . cookies . get ( storage . cookie_name ) <EOL> if not cookie or cookie [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> data = storage . _decode ( cookie . value ) <EOL> if not data : <EOL> return <NUM_LIT:0> <EOL> if data [ - <NUM_LIT:1> ] == CookieStorage . not_finished : <EOL> data . pop ( ) <EOL> return len ( data ) <EOL> class CookieTest ( BaseTest ) : <EOL> storage_class = CookieStorage <EOL> def stored_messages_count ( self , storage , response ) : <EOL> return stored_cookie_messages_count ( storage , response ) <EOL> def test_get ( self ) : <EOL> storage = self . storage_class ( self . get_request ( ) ) <EOL> example_messages = [ '<STR_LIT:test>' , '<STR_LIT>' ] <EOL> set_cookie_data ( storage , example_messages ) <EOL> self . assertEqual ( list ( storage ) , example_messages ) <EOL> def test_domain ( self ) : <EOL> """<STR_LIT>""" <EOL> storage = self . get_storage ( ) <EOL> response = self . get_response ( ) <EOL> storage . add ( constants . INFO , '<STR_LIT:test>' ) <EOL> storage . update ( response ) <EOL> self . assertTrue ( '<STR_LIT:test>' in response . cookies [ '<STR_LIT>' ] . value ) <EOL> self . assertEqual ( response . cookies [ '<STR_LIT>' ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( response . cookies [ '<STR_LIT>' ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> storage = self . get_storage ( ) <EOL> response = self . get_response ( ) <EOL> storage . add ( constants . INFO , '<STR_LIT:test>' ) <EOL> for m in storage : <EOL> pass <EOL> storage . update ( response ) <EOL> self . assertEqual ( response . cookies [ '<STR_LIT>' ] . value , '<STR_LIT>' ) <EOL> self . assertEqual ( response . cookies [ '<STR_LIT>' ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( response . cookies [ '<STR_LIT>' ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_get_bad_cookie ( self ) : <EOL> request = self . get_request ( ) <EOL> storage = self . storage_class ( request ) <EOL> example_messages = [ '<STR_LIT:test>' , '<STR_LIT>' ] <EOL> set_cookie_data ( storage , example_messages , invalid = True ) <EOL> self . assertEqual ( list ( storage ) , [ ] ) <EOL> def test_max_cookie_length ( self ) : <EOL> """<STR_LIT>""" <EOL> storage = self . get_storage ( ) <EOL> response = self . get_response ( ) <EOL> msg_size = int ( ( CookieStorage . max_cookie_size - <NUM_LIT> ) / <NUM_LIT> - <NUM_LIT> ) <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> storage . add ( constants . INFO , str ( i ) * msg_size ) <EOL> unstored_messages = storage . update ( response ) <EOL> cookie_storing = self . stored_messages_count ( storage , response ) <EOL> self . assertEqual ( cookie_storing , <NUM_LIT:4> ) <EOL> self . assertEqual ( len ( unstored_messages ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( unstored_messages [ <NUM_LIT:0> ] . message == '<STR_LIT:0>' * msg_size ) <EOL> def test_json_encoder_decoder ( self ) : <EOL> """<STR_LIT>""" <EOL> messages = [ <EOL> { <EOL> '<STR_LIT:message>' : Message ( constants . INFO , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : [ Message ( constants . INFO , '<STR_LIT>' ) for x in xrange ( <NUM_LIT:5> ) ] + [ { '<STR_LIT>' : Message ( constants . ERROR , '<STR_LIT:error>' ) } ] , <EOL> } , <EOL> Message ( constants . INFO , '<STR_LIT>' ) , <EOL> ] <EOL> encoder = MessageEncoder ( separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) ) <EOL> value = encoder . encode ( messages ) <EOL> decoded_messages = json . loads ( value , cls = MessageDecoder ) <EOL> self . assertEqual ( messages , decoded_messages ) <EOL> CookieTest = override_settings ( <EOL> SESSION_COOKIE_DOMAIN = '<STR_LIT>' ) ( CookieTest ) </s>
