Skip to content

Commit 6a74972

Browse files
authored
fix: double reading error on urllib3 2.0 (#296)
Fixes #295 Signed-off-by: Frost Ming <[email protected]>
1 parent 8f037ba commit 6a74972

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

cachecontrol/serialize.py

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def dumps(self, request, response, body=None):
3434
# sure it acts as though it was never read.
3535
body = response.read(decode_content=False)
3636
response._fp = io.BytesIO(body)
37+
response.length_remaining = len(body)
3738

3839
# NOTE: This is all a bit weird, but it's really important that on
3940
# Python 2.x these objects are unicode and not str, even when

tests/conftest.py

+10
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ def stream(self, env, start_response):
104104
for i in range(10):
105105
yield pformat(i).encode("utf8")
106106

107+
def fixed_length(self, env, start_response):
108+
body = b"0123456789"
109+
headers = [
110+
("Content-Type", "text/plain"),
111+
("Cache-Control", "max-age=5000"),
112+
("Content-Length", str(len(body)))
113+
]
114+
start_response("200 OK", headers)
115+
return [body]
116+
107117
def __call__(self, env, start_response):
108118
func = self.dispatch(env)
109119

tests/test_serialization.py

+6
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,9 @@ def test_no_body_creates_response_file_handle_on_dumps(self, url):
135135
# handle. Reading it again proves we're resetting the internal
136136
# file handle with a buffer.
137137
assert original_resp.raw.read()
138+
139+
def test_no_incomplete_read_on_dumps(self, url):
140+
resp = requests.get(url + "fixed_length", stream=True)
141+
self.serializer.dumps(resp.request, resp.raw)
142+
143+
assert resp.content == b"0123456789"

0 commit comments

Comments
 (0)