From ac1e6a011aab5697f65bcf69e1934b09ac1e90fd Mon Sep 17 00:00:00 2001 From: Markis Taylor Date: Wed, 13 Sep 2023 14:31:00 -0400 Subject: [PATCH 1/2] test: demonstrate the json bug --- tests/integration/test_aiohttp.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/integration/test_aiohttp.py b/tests/integration/test_aiohttp.py index 49cf99343..90ff675f1 100644 --- a/tests/integration/test_aiohttp.py +++ b/tests/integration/test_aiohttp.py @@ -158,6 +158,36 @@ def test_post(tmpdir, body, caplog, mockbin_request_url): ), "Log message not found." +@pytest.mark.online +@pytest.mark.asyncio +async def test_post_data_vs_json(tmpdir, caplog, mockbin_request_url): + caplog.set_level(logging.INFO) + data = {"key1": "value1", "key2": "value2"} + url = mockbin_request_url + with vcr.use_cassette(str(tmpdir.join("post.yaml"))): + async with aiohttp.ClientSession() as session: + async with await session.post(url, json=data) as request: + response_json = await request.json() + + with vcr.use_cassette(str(tmpdir.join("post.yaml"))) as cassette: + request = cassette.requests[0] + assert request.body == data + async with aiohttp.ClientSession() as session: + async with await session.post(url, json=data) as request: + cassette_response_json = await request.json() + assert cassette_response_json == response_json + assert cassette.play_count == 1 + + assert next( + ( + log + for log in caplog.records + if log.getMessage() == f" not in cassette, sending to real server" + ), + None, + ), "Log message not found." + + @pytest.mark.online def test_params(tmpdir, mockbin_request_url): url = mockbin_request_url + "?d=d" From aa988daf2bbb03c0c11aa457492decd5508e1a35 Mon Sep 17 00:00:00 2001 From: Markis Taylor Date: Wed, 13 Sep 2023 14:34:06 -0400 Subject: [PATCH 2/2] fix: address json kwarg propagation --- vcr/stubs/aiohttp_stubs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vcr/stubs/aiohttp_stubs.py b/vcr/stubs/aiohttp_stubs.py index 50faf221e..2b6dde93f 100644 --- a/vcr/stubs/aiohttp_stubs.py +++ b/vcr/stubs/aiohttp_stubs.py @@ -244,7 +244,7 @@ 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("json", kwargs.get("data")) params = kwargs.get("params") cookies = kwargs.get("cookies")