1
+ import msgpack
1
2
import requests
2
3
3
- from mock import Mock , patch
4
+ from mock import Mock
4
5
5
6
from cachecontrol .compat import pickle
6
7
from cachecontrol .serialize import Serializer
7
- from cachecontrol .serialize import _b64_encode
8
- from cachecontrol .serialize import _b64_decode_str
9
8
10
9
11
10
class TestSerializer (object ):
@@ -30,17 +29,29 @@ def setup(self):
30
29
},
31
30
}
32
31
33
- def test_load_by_version_one (self ):
32
+ def test_load_by_version_v0 (self ):
34
33
data = b'cc=0,somedata'
35
34
req = Mock ()
36
35
resp = self .serializer .loads (req , data )
37
36
assert resp is None
38
37
39
- def test_read_version_two (self ):
38
+ def test_read_version_v1 (self ):
40
39
req = Mock ()
41
40
resp = self .serializer ._loads_v1 (req , pickle .dumps (self .response_data ))
42
- # We have to decode our urllib3 data back into a unicode
43
- # string.
41
+ # We have to decode our urllib3 data back into a unicode string.
42
+ assert resp .data == 'Hello World' .encode ('utf-8' )
43
+
44
+ def test_read_version_v2 (self ):
45
+ req = Mock ()
46
+ compressed_base64_json = b"x\x9c %O\xb9 \n \x83 @\x10 \xfd \x97 \xa9 -\x92 %E\x14 R\xe4 +\x16 \t \xe6 \x10 \xbb \xb0 \xc7 \xe0 \x81 \xb8 \xb2 \xbb *A\xfc \xf7 \x8c \xa6 |\xe7 \xbc \x99 \xc0 \xa2 \xeb L\xeb \x10 \xa2 \t \xa4 \xd1 _\x88 \xe0 \xc9 3'\xf9 \xbe \xc8 X\xf8 \x95 <=@\x00 \x1a \x95 \xd1 \xf8 Q\xa6 \xf5 \xd8 z\x88 \xbc \xed 1\x80 \x12 \x85 F\xeb \x96 h\xca \xc2 ^\xf3 \xac \xd7 \xe7 \xed \x1b \xf3 SC5\x04 w\xfa \x1c \x8e \x92 _;Y\x1c \x96 \x9a \x94 ]k\xc1 \xdf ~u\xc7 \xc9 \x8f DG\xa0 \xe2 \xac \x92 \xbc \xa9 \xc9 \xf1 \xc8 \xcb Q\xe4 I\xa3 \xc6 U\xb9 _\x14 \xbb \xbd h\xc2 \x1c \xd0 R\xe1 LK$\xd9 \x9c \x17 \xbe \xa7 \xc3 l\xb3 Y\x80 \xad \x94 \xff \x0b \x03 \xed \xa9 V\x17 [2\x83 \xb0 \xf4 \xd1 4\xcf ?E\x03 Im"
47
+ resp = self .serializer ._loads_v2 (req , compressed_base64_json )
48
+ # We have to decode our urllib3 data back into a unicode string.
49
+ assert resp .data == 'Hello World' .encode ('utf-8' )
50
+
51
+ def test_read_version_v3 (self ):
52
+ req = Mock ()
53
+ resp = self .serializer ._loads_v3 (req , msgpack .dumps (self .response_data ))
54
+ # We have to decode our urllib3 data back into a unicode string.
44
55
assert resp .data == 'Hello World' .encode ('utf-8' )
45
56
46
57
def test_read_v1_serialized_with_py2_TypeError (self ):
@@ -65,7 +76,7 @@ def test_read_v2_corrupted_cache(self):
65
76
req = Mock ()
66
77
assert self .serializer ._loads_v2 (req , b'' ) is None
67
78
68
- def test_read_version_three_streamable (self , url ):
79
+ def test_read_latest_version_streamable (self , url ):
69
80
original_resp = requests .get (url , stream = True )
70
81
req = original_resp .request
71
82
@@ -78,7 +89,7 @@ def test_read_version_three_streamable(self, url):
78
89
79
90
assert resp .read ()
80
91
81
- def test_read_version_three (self , url ):
92
+ def test_read_latest_version (self , url ):
82
93
original_resp = requests .get (url )
83
94
data = original_resp .content
84
95
req = original_resp .request
@@ -110,33 +121,3 @@ def test_no_vary_header(self, url):
110
121
)
111
122
)
112
123
113
-
114
- class TestEncoding (object ):
115
-
116
- unicode_string = b'\u201cmax-age=31536000\u2033' .decode ('utf-8' )
117
- b64_result = '4oCcbWF4LWFnZT0zMTUzNjAwMOKAsw=='
118
-
119
- @patch ('cachecontrol.serialize._b64_encode_bytes' )
120
- def test_b64_encode_with_bytes (self , encode_bytes ):
121
- _b64_encode (self .unicode_string .encode ('utf-8' ))
122
- assert encode_bytes .called
123
-
124
- @patch ('cachecontrol.serialize._b64_encode_str' )
125
- def test_b64_encode_with_str (self , encode_str ):
126
- _b64_encode (self .unicode_string )
127
- assert encode_str .called
128
-
129
- def test_b64_encode_with_unicode_encoded_as_unicode (self ):
130
- """Some servers will respond with unicode encoded strings. The
131
- test below uses unicode open and close quotes around the max
132
- age setting, which raises an exception if we treat it as a
133
- string.
134
-
135
- This test ensures we recognize the unicode encoded string act
136
- accordingly.
137
- """
138
- unicode_result = _b64_encode (self .unicode_string .encode ('utf-8' ))
139
- assert _b64_decode_str (unicode_result ) == self .unicode_string
140
-
141
- bytes_result = _b64_encode (self .unicode_string )
142
- assert _b64_decode_str (bytes_result ) == self .unicode_string
0 commit comments