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:
committed by
GitHub
parent
b28316ab10
commit
31d8c3498b
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user