Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: double reading error on urllib3 2.0 #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cachecontrol/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def dumps(self, request, response, body=None):
# sure it acts as though it was never read.
body = response.read(decode_content=False)
response._fp = io.BytesIO(body)
response.length_remaining = len(body)

# NOTE: This is all a bit weird, but it's really important that on
# Python 2.x these objects are unicode and not str, even when
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ def stream(self, env, start_response):
for i in range(10):
yield pformat(i).encode("utf8")

def fixed_length(self, env, start_response):
body = b"0123456789"
headers = [
("Content-Type", "text/plain"),
("Cache-Control", "max-age=5000"),
("Content-Length", str(len(body)))
]
start_response("200 OK", headers)
return [body]

def __call__(self, env, start_response):
func = self.dispatch(env)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,9 @@ def test_no_body_creates_response_file_handle_on_dumps(self, url):
# handle. Reading it again proves we're resetting the internal
# file handle with a buffer.
assert original_resp.raw.read()

def test_no_incomplete_read_on_dumps(self, url):
resp = requests.get(url + "fixed_length", stream=True)
self.serializer.dumps(resp.request, resp.raw)

assert resp.content == b"0123456789"