Skip to content

Commit 2683231

Browse files
Improve aiohttp JSONMatcher support (#139)
* Improve aiohttp JSONMatcher support. * Lint * Small simplification --------- Co-authored-by: sarayourfriend <git@sarayourfriend.pictures>
1 parent d9e739f commit 2683231

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/pook/interceptors/aiohttp.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ async def _on_request(
7878
str(url) + "?" + urlencode([(x, y) for x, y in kw["params"].items()])
7979
)
8080

81+
# If a json payload is provided, serialize it for JSONMatcher support
82+
if json_body := kw.get("json"):
83+
req.json = json_body
84+
if "Content-Type" not in req.headers:
85+
req.headers["Content-Type"] = "application/json"
86+
8187
# Match the request against the registered mocks in pook
8288
mock = self.engine.match(req)
8389

tests/unit/interceptors/aiohttp_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
import aiohttp
3+
import json
34

45
import pook
56

@@ -55,3 +56,21 @@ async def test_binary_body(URL):
5556
async with aiohttp.ClientSession() as session:
5657
req = await session.get(URL)
5758
assert await req.read() == BINARY_FILE
59+
60+
61+
@pytest.mark.asyncio
62+
async def test_json_matcher_data_payload(URL):
63+
payload = {"foo": "bar"}
64+
pook.post(URL).json(payload).reply(200).body(BINARY_FILE)
65+
async with aiohttp.ClientSession() as session:
66+
req = await session.post(URL, data=json.dumps(payload))
67+
assert await req.read() == BINARY_FILE
68+
69+
70+
@pytest.mark.asyncio
71+
async def test_json_matcher_json_payload(URL):
72+
payload = {"foo": "bar"}
73+
pook.post(URL).json(payload).reply(200).body(BINARY_FILE)
74+
async with aiohttp.ClientSession() as session:
75+
req = await session.post(URL, json=payload)
76+
assert await req.read() == BINARY_FILE

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy