Skip to content

Commit

Permalink
feat: support raise_for_status as callable
Browse files Browse the repository at this point in the history
  • Loading branch information
chs2 committed Dec 18, 2023
1 parent 444aa49 commit 91019b4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion aioresponses/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,10 @@ async def _request_mock(self, orig_self: ClientSession,
raise_for_status = getattr(
orig_self, '_raise_for_status', False
)
if raise_for_status:

if callable(raise_for_status):
await raise_for_status(response)
elif raise_for_status:
response.raise_for_status()

return response
Expand Down
15 changes: 15 additions & 0 deletions tests/test_aioresponses.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,21 @@ async def test_do_not_raise_for_status(self, m):

self.assertEqual(response.status, 400)

@aioresponses()
@skipIf(condition=AIOHTTP_VERSION < Version('3.9.0'),
reason='aiohttp<3.9.0 does not support callable raise_for_status '
'arguments for requests')
async def test_callable_raise_for_status(self, m):
async def raise_for_status(response: ClientResponse):
if response.status >= 400:
raise Exception("callable raise_for_status")

m.get(self.url, status=400)
with self.assertRaises(Exception) as cm:
await self.session.get(self.url,
raise_for_status=raise_for_status)
self.assertEqual(str(cm.exception), "callable raise_for_status")


class AIOResponseRedirectTest(AsyncTestCase):

Expand Down

0 comments on commit 91019b4

Please sign in to comment.