Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aiohttp: Allow both data and json arguments #624

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions tests/integration/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,28 @@ def test_stream(tmpdir, scheme):
assert cassette.play_count == 1


@pytest.mark.parametrize("body", ["data", "json"])
def test_post(tmpdir, scheme, body, caplog):
POST_DATA = {"key1": "value1", "key2": "value2"}


@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, scheme, kwargs, caplog):
caplog.set_level(logging.INFO)
data = {"key1": "value1", "key2": "value2"}
url = scheme + "://httpbin.org/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

Expand All @@ -151,6 +161,15 @@ def test_post(tmpdir, scheme, body, caplog):
), "Log message not found."


def test_post_data_plus_json_error(tmpdir, scheme):
url = scheme + "://httpbin.org/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 == []


def test_params(tmpdir, scheme):
url = scheme + "://httpbin.org/get"
headers = {"Content-Type": "application/json"}
Expand Down
6 changes: 5 additions & 1 deletion vcr/stubs/aiohttp_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,11 @@ async def new_request(self, method, url, **kwargs):
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")

Expand Down