diff --git a/aioresponses/core.py b/aioresponses/core.py index 2bb6d57..1edb7bd 100644 --- a/aioresponses/core.py +++ b/aioresponses/core.py @@ -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 diff --git a/tests/test_aioresponses.py b/tests/test_aioresponses.py index 23d3b46..706e89e 100644 --- a/tests/test_aioresponses.py +++ b/tests/test_aioresponses.py @@ -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):