Skip to content

Commit

Permalink
Merge pull request #251 from chs2/feat/callable-raise-for-status
Browse files Browse the repository at this point in the history
feat: support raise_for_status as callable
  • Loading branch information
pnuckowski authored Apr 14, 2024
2 parents bd79310 + 91019b4 commit 64a4190
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 64a4190

Please sign in to comment.