<s> import requests <EOL> from mock import Mock , patch <EOL> from cachecontrol . compat import pickle <EOL> from cachecontrol . serialize import Serializer <EOL> from cachecontrol . serialize import _b64_encode <EOL> from cachecontrol . serialize import _b64_decode_str <EOL> class TestSerializer ( object ) : <EOL> def setup ( self ) : <EOL> self . serializer = Serializer ( ) <EOL> self . response_data = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:body>' : '<STR_LIT>' . encode ( '<STR_LIT:utf-8>' ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT:status>' : <NUM_LIT:200> , <EOL> '<STR_LIT:version>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:strict>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } , <EOL> } <EOL> def test_load_by_version_one ( self ) : <EOL> data = b'<STR_LIT>' <EOL> req = Mock ( ) <EOL> resp = self . serializer . loads ( req , data ) <EOL> assert resp is None <EOL> def test_read_version_two ( self ) : <EOL> req = Mock ( ) <EOL> resp = self . serializer . _loads_v1 ( req , pickle . dumps ( self . response_data ) ) <EOL> assert resp . data == '<STR_LIT>' . encode ( '<STR_LIT:utf-8>' ) <EOL> def test_read_v1_serialized_with_py2_TypeError ( self ) : <EOL> req = Mock ( ) <EOL> py2_pickled_data = b"<STR_LIT>" . join ( [ <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" ] ) <EOL> resp = self . serializer . _loads_v1 ( req , py2_pickled_data ) <EOL> assert resp . data == '<STR_LIT>' . encode ( '<STR_LIT:utf-8>' ) <EOL> def test_read_version_three_streamable ( self , url ) : <EOL> original_resp = requests . get ( url , stream = True ) <EOL> req = original_resp . request <EOL> resp = self . serializer . loads ( <EOL> req , self . serializer . dumps ( <EOL> req , <EOL> original_resp . raw <EOL> ) <EOL> ) <EOL> assert resp . read ( ) <EOL> def test_read_version_three ( self , url ) : <EOL> original_resp = requests . get ( url ) <EOL> data = original_resp . content <EOL> req = original_resp . request <EOL> resp = self . serializer . loads ( <EOL> req , self . serializer . dumps ( <EOL> req , <EOL> original_resp . raw , <EOL> body = data <EOL> ) <EOL> ) <EOL> assert resp . read ( ) == data <EOL> def test_no_vary_header ( self , url ) : <EOL> original_resp = requests . get ( url ) <EOL> data = original_resp . content <EOL> req = original_resp . request <EOL> original_resp . raw . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> assert self . serializer . loads ( <EOL> req , self . serializer . dumps ( <EOL> req , <EOL> original_resp . raw , <EOL> body = data <EOL> ) <EOL> ) <EOL> class TestEncoding ( object ) : <EOL> unicode_string = b'<STR_LIT>' . decode ( '<STR_LIT:utf-8>' ) <EOL> b64_result = '<STR_LIT>' <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_b64_encode_with_bytes ( self , encode_bytes ) : <EOL> _b64_encode ( self . unicode_string . encode ( '<STR_LIT:utf-8>' ) ) <EOL> assert encode_bytes . called <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_b64_encode_with_str ( self , encode_str ) : <EOL> _b64_encode ( self . unicode_string ) <EOL> assert encode_str . called <EOL> def test_b64_encode_with_unicode_encoded_as_unicode ( self ) : <EOL> """<STR_LIT>""" <EOL> unicode_result = _b64_encode ( self . unicode_string . encode ( '<STR_LIT:utf-8>' ) ) <EOL> assert _b64_decode_str ( unicode_result ) == self . unicode_string <EOL> bytes_result = _b64_encode ( self . unicode_string ) <EOL> assert _b64_decode_str ( bytes_result ) == self . unicode_string </s>
