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

Chunked body as an iterator doesn't work when body is a streamed iterator #846

Open
sathieu opened this issue Jun 27, 2024 · 2 comments · May be fixed by #847 or #851
Open

Chunked body as an iterator doesn't work when body is a streamed iterator #846

sathieu opened this issue Jun 27, 2024 · 2 comments · May be fixed by #847 or #851

Comments

@sathieu
Copy link

sathieu commented Jun 27, 2024

#739 fixed chunked response for most of the usecases, but this is not enough 😉 .

When list(body) is processed, this consumes the iterator.:

body = list(body)

But this code is called many times, and all but the first call will return an empty list.

This could be worked-around with:

diff --git a/vcr/stubs/__init__.py b/vcr/stubs/__init__.py
index 4d4bb39..c2af59d 100644
--- a/vcr/stubs/__init__.py
+++ b/vcr/stubs/__init__.py
@@ -254,6 +254,9 @@ class VCRConnection:
         """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)):
+                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)

But this is hackish and probably not the right place ...

@sathieu
Copy link
Author

sathieu commented Jun 27, 2024

Here is a better patch, for stream objects (BytesIO) too:

diff --git a/vcr/stubs/__init__.py b/vcr/stubs/__init__.py
index 4d4bb39..2ea9fdf 100644
--- a/vcr/stubs/__init__.py
+++ b/vcr/stubs/__init__.py
@@ -254,6 +254,12 @@ class VCRConnection:
         """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)

sathieu added a commit to sathieu/vcrpy that referenced this issue Jun 27, 2024
Fixes: kevin1024#846
Signed-off-by: Mathieu Parent <math.parent@gmail.com>
@sathieu
Copy link
Author

sathieu commented Jun 27, 2024

Created: #847

sathieu added a commit to sathieu/vcrpy that referenced this issue Jun 29, 2024
Fixes: kevin1024#846
Signed-off-by: Mathieu Parent <math.parent@gmail.com>
sathieu added a commit to sathieu/vcrpy that referenced this issue Jun 30, 2024
Fixes: kevin1024#846
Signed-off-by: Mathieu Parent <math.parent@gmail.com>
sathieu added a commit to sathieu/vcrpy that referenced this issue Jul 2, 2024
Fixes: kevin1024#846
Signed-off-by: Mathieu Parent <math.parent@gmail.com>
sathieu added a commit to sathieu/vcrpy that referenced this issue Jul 7, 2024
Fixes: kevin1024#846
Signed-off-by: Mathieu Parent <math.parent@gmail.com>
sathieu added a commit to sathieu/vcrpy that referenced this issue Jul 14, 2024
Fixes: kevin1024#846
Signed-off-by: Mathieu Parent <math.parent@gmail.com>
sathieu added a commit to sathieu/vcrpy that referenced this issue Jul 21, 2024
Fixes: kevin1024#846
Signed-off-by: Mathieu Parent <math.parent@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant