Skip to content

Commit 3f78330

Browse files
titojairhenrique
andauthored
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 <[email protected]> --------- Co-authored-by: Jair Henrique <[email protected]>
1 parent e8818e5 commit 3f78330

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

tests/integration/test_aiohttp.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import io
12
import logging
23
import ssl
34
import urllib.parse
@@ -462,3 +463,19 @@ def test_filter_query_parameters(tmpdir, httpbin):
462463
cassette_content = f.read()
463464
assert "password" not in cassette_content
464465
assert "secret" not in cassette_content
466+
467+
468+
@pytest.mark.online
469+
def test_use_cassette_with_io(tmpdir, caplog, httpbin):
470+
url = httpbin.url + "/post"
471+
472+
# test without cassettes
473+
data = io.BytesIO(b"hello")
474+
_, response_json = request("POST", url, output="json", data=data)
475+
assert response_json["data"] == "hello"
476+
477+
# test with cassettes
478+
data = io.BytesIO(b"hello")
479+
with vcr.use_cassette(str(tmpdir.join("post.yaml"))):
480+
_, response_json = request("POST", url, output="json", data=data)
481+
assert response_json["data"] == "hello"

vcr/request.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ def __init__(self, method, uri, body, headers):
2020
self._was_file = hasattr(body, "read")
2121
self._was_iter = _is_nonsequence_iterator(body)
2222
if self._was_file:
23-
self.body = body.read()
23+
if hasattr(body, "tell"):
24+
tell = body.tell()
25+
self.body = body.read()
26+
body.seek(tell)
27+
else:
28+
self.body = body.read()
2429
elif self._was_iter:
2530
self.body = list(body)
2631
else:

0 commit comments

Comments
 (0)