@@ -53,6 +53,16 @@ def test_serialize_unserialize(self):
5353 r = cookie .unserialize (r )
5454 assert r == case
5555
56+ def test_serialize_str_unserialize_str (self ):
57+ for Cookie in [self .Cookie , self .RawCookie ]:
58+ cookie = Cookie (b'my little key' )
59+ for case in [{'a' : 'b' }, {'a' : 'próba' }, {'próba' : '123' }]:
60+ r = cookie .serialize_str (case )
61+ assert isinstance (r , str )
62+
63+ r = cookie .unserialize_str (r )
64+ assert r == case
65+
5666 def test_unserialize_binary (self ):
5767 """
5868 Test unserialize compatibility with existing binary data.
@@ -104,6 +114,31 @@ def test_fail_when_corrupted(self):
104114 r = cookie .unserialize (r [:20 ] + r [21 :])
105115 assert not r
106116
117+ def test_fail_incorrect_str (self ):
118+ cookie = self .Cookie (b'my little key' )
119+ raw = self .RawCookie (b'my little key' ).serialize ({'a' : 'próba' })
120+
121+ # Send somthing which can't be base64 decoded
122+ r = cookie .unserialize_str (raw .hex ())
123+ assert r == {}
124+
125+ # Send somthing not ascii at all
126+ r = cookie .unserialize_str (raw .decode ('latin-1' ))
127+ assert r == {}
128+
129+ def test_fail_incorrect_raw_str (self ):
130+ b64 = self .Cookie (b'my little key' ).serialize ({'a' : 'próba' })
131+ raw_cookie = self .RawCookie (b'my little key' )
132+
133+ # Send somthing which can't be hex decoded
134+ r = raw_cookie .unserialize_str (b64 .decode ())
135+ assert r == {}
136+
137+ # Send somthing not ascii at all
138+ raw = raw_cookie .serialize ({'a' : 'próba' })
139+ r = raw_cookie .unserialize_str (raw .decode ('latin-1' ))
140+ assert r == {}
141+
107142 def test_compression_and_decompression (self ):
108143 key = b'my little key'
109144 case = {'a' : 'próba' }
0 commit comments