Skip to content

Commit

Permalink
Add strict kwarg to Mocker
Browse files Browse the repository at this point in the history
  • Loading branch information
phy1729 committed Jan 6, 2024
1 parent 0f133ee commit 61aae60
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions requests_mock/mocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def __init__(self, session=None, **kwargs):

self._json_encoder = kwargs.pop('json_encoder', None)
self.real_http = kwargs.pop('real_http', False)
self.strict = kwargs.pop('strict', False)
self._last_send = None

if kwargs:
Expand Down Expand Up @@ -213,6 +214,8 @@ def stop(self):
if self._last_send:
self._mock_target.send = self._last_send
self._last_send = None
if self.strict:
assert self._adapter.all_exhausted

# for familiarity with MagicMock
def reset_mock(self):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_mocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
except ImportError:
import mock
import requests
import testtools

import requests_mock
from requests_mock import adapter
Expand Down Expand Up @@ -122,6 +123,19 @@ def test_with_context_manager(self):
self._do_test(m)
self.assertMockStopped()

def test_strict_with_context_manager(self):
self.assertMockStopped()
with requests_mock.Mocker(strict=True) as m:
self._do_test(m)
self.assertMockStopped()

def test_strict_with_context_manager_raises(self):
self.assertMockStopped()
with testtools.ExpectedException(AssertionError):
with requests_mock.Mocker(strict=True) as m:
m.register_uri('GET', 'http://www.example.com', text='resp')
self._do_test(m)

@mock.patch('requests.adapters.HTTPAdapter.send')
@requests_mock.mock(real_http=True)
def test_real_http(self, real_send, mocker):
Expand Down

0 comments on commit 61aae60

Please sign in to comment.