Skip to content

Commit

Permalink
fix: ensure request body is available in history
Browse files Browse the repository at this point in the history
  • Loading branch information
brycedrennan committed Dec 21, 2020
1 parent 7c9f83c commit 3a6f217
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aresponses/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ async def _make_runner(self, debug=True, **kwargs):
async def _handler(self, request):
self._request_count += 1
route, response = await self._find_response(request)
# ensures the request content is loaded even if the handler didn't need it. This makes it available in the
# `aresponses.history`
await request.text()
self._history.append(RoutingLog(request, route, response))
return response

Expand Down
18 changes: 18 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,21 @@ async def test_history(aresponses):
assert aresponses.history[1].request.host == "bar.com"
assert "Route(" in repr(aresponses.history[0].route)
aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_history_post(aresponses):
"""Ensure the request contets exist in the history"""
aresponses.add(method_pattern="POST", response={"some": "response"})

async with aiohttp.ClientSession() as session:
async with session.post("http://bar.com/zzz", json={"greeting": "hello"}) as response:
response_data = await response.json()
assert response_data == {"some": "response"}

assert len(aresponses.history) == 1
assert aresponses.history[0].request.host == "bar.com"
request_data = await aresponses.history[0].request.json()
assert request_data == {"greeting": "hello"}
assert "Route(" in repr(aresponses.history[0].route)
aresponses.assert_plan_strictly_followed()

0 comments on commit 3a6f217

Please sign in to comment.