mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 09:13: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
|
assert cassette.play_count == 1
|
||||||
|
|
||||||
|
|
||||||
|
POST_DATA = {"key1": "value1", "key2": "value2"}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.online
|
@pytest.mark.online
|
||||||
@pytest.mark.parametrize("body", ["data", "json"])
|
@pytest.mark.parametrize(
|
||||||
def test_post(tmpdir, body, caplog, httpbin):
|
"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)
|
caplog.set_level(logging.INFO)
|
||||||
data = {"key1": "value1", "key2": "value2"}
|
url = httpbin.url + "/post"
|
||||||
url = httpbin.url
|
|
||||||
with vcr.use_cassette(str(tmpdir.join("post.yaml"))):
|
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:
|
with vcr.use_cassette(str(tmpdir.join("post.yaml"))) as cassette:
|
||||||
request = cassette.requests[0]
|
request = cassette.requests[0]
|
||||||
assert request.body == data
|
assert request.body == POST_DATA
|
||||||
_, cassette_response_json = post(url, **{body: data})
|
_, cassette_response_json = post(url, **kwargs)
|
||||||
assert cassette_response_json == response_json
|
assert cassette_response_json == response_json
|
||||||
assert cassette.play_count == 1
|
assert cassette.play_count == 1
|
||||||
|
|
||||||
@@ -163,6 +173,16 @@ def test_post(tmpdir, body, caplog, httpbin):
|
|||||||
), "Log message not found."
|
), "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
|
@pytest.mark.online
|
||||||
def test_params(tmpdir, httpbin):
|
def test_params(tmpdir, httpbin):
|
||||||
url = httpbin.url + "/get?d=d"
|
url = httpbin.url + "/get?d=d"
|
||||||
|
|||||||
@@ -245,7 +245,11 @@ def vcr_request(cassette, real_request):
|
|||||||
headers = kwargs.get("headers")
|
headers = kwargs.get("headers")
|
||||||
auth = kwargs.get("auth")
|
auth = kwargs.get("auth")
|
||||||
headers = self._prepare_headers(headers)
|
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")
|
params = kwargs.get("params")
|
||||||
cookies = kwargs.get("cookies")
|
cookies = kwargs.get("cookies")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user