1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-08 16:53:23 +00:00

aiohttp: Allow both data and json arguments (#624)

If at least one of them is `None`.

Previously, a `data=None` parameter would cause the `json` parameter to
be ignored, resulting in an empty request body payload on the cassette.
This commit is contained in:
Leonardo Rochael Almeida
2025-12-08 15:13:51 +01:00
committed by GitHub
parent b28316ab10
commit 31d8c3498b
2 changed files with 32 additions and 8 deletions

View File

@@ -137,19 +137,29 @@ def test_stream(tmpdir, httpbin):
assert cassette.play_count == 1
POST_DATA = {"key1": "value1", "key2": "value2"}
@pytest.mark.online
@pytest.mark.parametrize("body", ["data", "json"])
def test_post(tmpdir, body, caplog, httpbin):
@pytest.mark.parametrize(
"kwargs",
[
dict(data=POST_DATA),
dict(json=POST_DATA),
dict(data=POST_DATA, json=None),
dict(data=None, json=POST_DATA),
],
)
def test_post(tmpdir, kwargs, caplog, httpbin):
caplog.set_level(logging.INFO)
data = {"key1": "value1", "key2": "value2"}
url = httpbin.url
url = httpbin.url + "/post"
with vcr.use_cassette(str(tmpdir.join("post.yaml"))):
_, response_json = post(url, **{body: data})
_, response_json = post(url, **kwargs)
with vcr.use_cassette(str(tmpdir.join("post.yaml"))) as cassette:
request = cassette.requests[0]
assert request.body == data
_, cassette_response_json = post(url, **{body: data})
assert request.body == POST_DATA
_, cassette_response_json = post(url, **kwargs)
assert cassette_response_json == response_json
assert cassette.play_count == 1
@@ -163,6 +173,16 @@ def test_post(tmpdir, body, caplog, httpbin):
), "Log message not found."
@pytest.mark.online
def test_post_data_plus_json_error(tmpdir, httpbin):
url = httpbin.url + "/post"
with vcr.use_cassette(str(tmpdir.join("post.yaml"))) as cassette, pytest.raises(
ValueError, match="data and json parameters can not be used at the same time"
):
post(url, data=POST_DATA, json=POST_DATA)
assert cassette.requests == []
@pytest.mark.online
def test_params(tmpdir, httpbin):
url = httpbin.url + "/get?d=d"

View File

@@ -245,7 +245,11 @@ def vcr_request(cassette, real_request):
headers = kwargs.get("headers")
auth = kwargs.get("auth")
headers = self._prepare_headers(headers)
data = kwargs.get("data", kwargs.get("json"))
data = kwargs.get("data")
if data is None:
data = kwargs.get("json")
elif kwargs.get("json") is not None:
raise ValueError("data and json parameters can not be used at the same time")
params = kwargs.get("params")
cookies = kwargs.get("cookies")