Skip to content

Commit

Permalink
Merge pull request #54 from CircleUp/request-body-history
Browse files Browse the repository at this point in the history
fix: ensure request body is available in history
  • Loading branch information
brycedrennan authored Dec 21, 2020
2 parents 7c9f83c + f032975 commit 18b6506
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion 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 Expand Up @@ -176,7 +179,7 @@ async def _find_response(self, request):
return route, response

self._unmatched_requests.append(request)
return route, None
return None, None

async def passthrough(self, request):
"""Make non-mocked network request"""
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 18b6506

Please sign in to comment.