Skip to content

Commit

Permalink
Send a RuntimeError indicating that session is closed, the same way a…
Browse files Browse the repository at this point in the history
…s aiohttp does
  • Loading branch information
Colin-b committed Nov 21, 2019
1 parent c02431d commit f69183e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Before you submit a pull request, check that it meets these guidelines:
2. If the pull request adds functionality, the docs should be updated. Put
your new functionality into a function with a docstring, and add the
feature to the list in README.rst.
3. The pull request should work for Python 2.6, 2.7, 3.3, 3.4 and 3.5, and for PyPy. Check
3. The pull request should work for Python 3.5, 3.6, 3.7 and for PyPy. Check
https://travis-ci.org/pnuckowski/aioresponses/pull_requests
and make sure that the tests pass for all supported Python versions.

Expand Down
3 changes: 3 additions & 0 deletions aioresponses/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ async def _request_mock(self, orig_self: ClientSession,
*args: Tuple,
**kwargs: Dict) -> 'ClientResponse':
"""Return mocked response object or raise connection error."""
if orig_self.closed:
raise RuntimeError('Session is closed')

url = normalize_url(merge_params(url, kwargs.get('params')))
url_str = str(url)
for prefix in self._passthrough:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_aioresponses.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@ def test_request_retrieval_in_case_no_response(self):
self.assertEqual(m.requests[key][0].kwargs,
{'allow_redirects': True})

async def test_request_failure_in_case_session_is_closed(self):
async def do_request(session):
return await session.get(self.url)

with aioresponses():
async with ClientSession() as session:
coro = do_request(session)

with self.assertRaises(RuntimeError) as exception_info:
await coro
assert str(exception_info.exception) == "Session is closed"

@asyncio.coroutine
def test_address_as_instance_of_url_combined_with_pass_through(self):
external_api = 'http://httpbin.org/status/201'
Expand Down

0 comments on commit f69183e

Please sign in to comment.