From f3a7903d2677df9d510fb41fd595241b3e0241fe Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Tue, 21 Jan 2025 19:10:05 -0600 Subject: [PATCH 1/2] fix: usage of io-like interface with VCR.py --- tests/integration/test_aiohttp.py | 18 ++++++++++++++++++ vcr/request.py | 7 ++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_aiohttp.py b/tests/integration/test_aiohttp.py index c1ae7484..a76539b0 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 @@ -466,3 +467,20 @@ 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 +@pytest.mark.asyncio +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 bc569f4b..d72de2a8 100644 --- a/vcr/request.py +++ b/vcr/request.py @@ -19,7 +19,12 @@ def __init__(self, method, uri, body, headers): 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: From e2b1566f656b634483e98bb5a302b482784c3c92 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Wed, 22 Jan 2025 23:36:17 +0100 Subject: [PATCH 2/2] Update tests/integration/test_aiohttp.py Co-authored-by: Jair Henrique --- tests/integration/test_aiohttp.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/integration/test_aiohttp.py b/tests/integration/test_aiohttp.py index a76539b0..fdb6f8bf 100644 --- a/tests/integration/test_aiohttp.py +++ b/tests/integration/test_aiohttp.py @@ -470,7 +470,6 @@ def test_filter_query_parameters(tmpdir, httpbin): @pytest.mark.online -@pytest.mark.asyncio def test_use_cassette_with_io(tmpdir, caplog, httpbin): url = httpbin.url + "/post"