Skip to content

Commit f1858da

Browse files
committed
Properly decode the aiohttp.ClientRequest body
The body of `aiohttp.ClientRequest` is a `aiohttp.payload.Payload` and thus it won't compare correctly when used by the `BodyMatcher`. `aiohttp.payload.Payload` provides a `decode` method we can use to turn the underlying payload into a string representation suitable for matching.
1 parent c7dcf2d commit f1858da

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/pook/interceptors/aiohttp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async def __call__(
2626
req = Request(
2727
method=request.method,
2828
headers=request.headers.items(),
29-
body=request.body,
29+
body=request.body.decode(),
3030
url=str(request.url),
3131
)
3232

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
import aiohttp
3+
4+
import pook
5+
6+
pytestmark = [pytest.mark.pook]
7+
8+
9+
async def test_aiohttp_match_body():
10+
body = {"foo": "bar"}
11+
pook.post("http://example.com", body="foo=1").reply(200).json(body)
12+
res = await aiohttp.ClientSession().post("http://example.com", data={"foo": 1})
13+
assert res.status == 200
14+
assert res.headers == {"Content-Type": "application/json"}
15+
assert await res.json() == body

0 commit comments

Comments
 (0)