From aa912033aef97c0c16b7b49dafd61c0ef190483f Mon Sep 17 00:00:00 2001 From: Mathieu Parent Date: Thu, 27 Jun 2024 23:28:25 +0200 Subject: [PATCH] Ensure body is consumed only once Fixes: #846 Signed-off-by: Mathieu Parent --- vcr/stubs/__init__.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/vcr/stubs/__init__.py b/vcr/stubs/__init__.py index 4d4bb39dc..2ea9fdf66 100644 --- a/vcr/stubs/__init__.py +++ b/vcr/stubs/__init__.py @@ -254,6 +254,12 @@ def getresponse(self, _=False, **kwargs): """Retrieve the response""" # Check to see if the cassette has a response for this request. If so, # then return it + if self._vcr_request.headers.get('Transfer-Encoding', '') == 'chunked': + if not (isinstance(self._vcr_request.body, str) or isinstance(self._vcr_request.body, bytes) or isinstance(self._vcr_request.body, bytearray)): + if hasattr(self._vcr_request.body, 'read'): + self._vcr_request.body = self._vcr_request.body.read() + else: + self._vcr_request.body = list(self._vcr_request.body) if self.cassette.can_play_response_for(self._vcr_request): log.info(f"Playing response for {self._vcr_request} from cassette") response = self.cassette.play_response(self._vcr_request)