Skip to content

Commit

Permalink
Take raise_for_status into account.
Browse files Browse the repository at this point in the history
When this argument is passed to either a request or a session, call
response.raise_for_status() automatically.
  • Loading branch information
ilai-deutel committed Jun 16, 2019
1 parent cc3c817 commit 67bd41e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
14 changes: 14 additions & 0 deletions aioresponses/core.py
Expand Up @@ -334,4 +334,18 @@ async def _request_mock(self, orig_self: ClientSession,
key = (method, url)
self.requests.setdefault(key, [])
self.requests[key].append(RequestCall(args, kwargs))

# Automatically call response.raise_for_status() on a request if the
# request was initialized with raise_for_status=True. Also call
# response.raise_for_status() if the client session was initialized
# with raise_for_status=True, unless the request was called with
# raise_for_status=False.
raise_for_status = kwargs.get('raise_for_status')
if raise_for_status is None:
raise_for_status = getattr(
orig_self, '_raise_for_status', False
)
if raise_for_status:
response.raise_for_status()

return response
60 changes: 58 additions & 2 deletions tests/test_aioresponses.py
Expand Up @@ -8,7 +8,7 @@
from aiohttp import http
from aiohttp.client import ClientSession
from aiohttp.client_reqrep import ClientResponse
from asynctest import fail_on
from asynctest import fail_on, skipIf
from asynctest.case import TestCase
from ddt import ddt, data

Expand All @@ -25,7 +25,7 @@
)
from aiohttp.http_exceptions import HttpProcessingError

from aioresponses.compat import URL
from aioresponses.compat import AIOHTTP_VERSION, URL
from aioresponses import CallbackResult, aioresponses


Expand Down Expand Up @@ -117,6 +117,17 @@ def test_raise_for_status(self, m):
response.raise_for_status()
self.assertEqual(cm.exception.message, http.RESPONSES[400][0])

@aioresponses()
@asyncio.coroutine
@skipIf(condition=AIOHTTP_VERSION < '3.4.0',
reason='aiohttp<3.4.0 does not support raise_for_status '
'arguments for requests')
def test_request_raise_for_status(self, m):
m.get(self.url, status=400)
with self.assertRaises(ClientResponseError) as cm:
yield from self.session.get(self.url, raise_for_status=True)
self.assertEqual(cm.exception.message, http.RESPONSES[400][0])

@aioresponses()
@asyncio.coroutine
def test_returned_instance_and_params_handling(self, m):
Expand Down Expand Up @@ -351,3 +362,48 @@ def callback(url, **kwargs):
response = future.result()
data = self.run_async(response.read())
assert data == body


class AIOResponsesRaiseForStatusSessionTestCase(TestCase):
"""Test case for sessions with raise_for_status=True.
This flag, introduced in aiohttp v2.0.0, automatically calls
`raise_for_status()`.
It is overriden by the `raise_for_status` argument of the request since
aiohttp v3.4.a0.
"""
use_default_loop = False

@asyncio.coroutine
def setUp(self):
self.url = 'http://example.com/api?foo=bar#fragment'
self.session = ClientSession(raise_for_status=True)
super().setUp()

@asyncio.coroutine
def tearDown(self):
close_result = self.session.close()
if close_result is not None:
yield from close_result
super().tearDown()

@aioresponses()
@asyncio.coroutine
def test_raise_for_status(self, m):
m.get(self.url, status=400)
with self.assertRaises(ClientResponseError) as cm:
yield from self.session.get(self.url)
self.assertEqual(cm.exception.message, http.RESPONSES[400][0])

@aioresponses()
@asyncio.coroutine
@skipIf(condition=AIOHTTP_VERSION < '3.4.0',
reason='aiohttp<3.4.0 does not support raise_for_status '
'arguments for requests')
def test_do_not_raise_for_status(self, m):
m.get(self.url, status=400)
response = yield from self.session.get(self.url,
raise_for_status=False)

self.assertEqual(response.status, 400)

0 comments on commit 67bd41e

Please sign in to comment.