From 3f78330c1eedcc6ffd5e7bc39c0a4e401d333dfc Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Fri, 5 Dec 2025 14:45:49 -0600 Subject: [PATCH] fix: usage of io-like interface with VCR.py (#906) * fix: usage of io-like interface with VCR.py * Update tests/integration/test_aiohttp.py Co-authored-by: Jair Henrique --------- Co-authored-by: Jair Henrique --- tests/integration/test_aiohttp.py | 17 +++++++++++++++++ vcr/request.py | 7 ++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_aiohttp.py b/tests/integration/test_aiohttp.py index dfcc5ab..d942fbd 100644 --- a/tests/integration/test_aiohttp.py +++ b/tests/integration/test_aiohttp.py @@ -1,3 +1,4 @@ +import io import logging import ssl import urllib.parse @@ -462,3 +463,19 @@ def test_filter_query_parameters(tmpdir, httpbin): cassette_content = f.read() assert "password" not in cassette_content assert "secret" not in cassette_content + + +@pytest.mark.online +def test_use_cassette_with_io(tmpdir, caplog, httpbin): + url = httpbin.url + "/post" + + # test without cassettes + data = io.BytesIO(b"hello") + _, response_json = request("POST", url, output="json", data=data) + assert response_json["data"] == "hello" + + # test with cassettes + data = io.BytesIO(b"hello") + with vcr.use_cassette(str(tmpdir.join("post.yaml"))): + _, response_json = request("POST", url, output="json", data=data) + assert response_json["data"] == "hello" diff --git a/vcr/request.py b/vcr/request.py index a4011b0..533ce59 100644 --- a/vcr/request.py +++ b/vcr/request.py @@ -20,7 +20,12 @@ class Request: self._was_file = hasattr(body, "read") self._was_iter = _is_nonsequence_iterator(body) if self._was_file: - self.body = body.read() + if hasattr(body, "tell"): + tell = body.tell() + self.body = body.read() + body.seek(tell) + else: + self.body = body.read() elif self._was_iter: self.body = list(body) else: