Skip to content

Commit

Permalink
Allow for nested mocking
Browse files Browse the repository at this point in the history
The double mocking solution we have now of mocking send and get_adapter
is a bit unusual. One of the problems is that if you have a nested mock
situation then send calling the last send means get_adapter is remocked
again and the outer adapter ends up being the last installed.

To avoid this always go to the real original send function to bypass any
outer installed mockers and make sure our get_adapter function is
triggered.

Change-Id: I47bff5e7da98e5238bd926e22845a7ca34f21940
Closes-Bug: #1642690
  • Loading branch information
jamielennox committed Nov 18, 2016
1 parent 81b72f4 commit 95dffef
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 8 deletions.
28 changes: 20 additions & 8 deletions requests_mock/mocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
POST = 'POST'
PUT = 'PUT'

_original_send = requests.Session.send


class MockerCore(object):
"""A wrapper around common mocking functions.
Expand Down Expand Up @@ -63,7 +65,7 @@ def __init__(self, **kwargs):
self._adapter = adapter.Adapter(case_sensitive=case_sensitive)

self._real_http = kwargs.pop('real_http', False)
self._real_send = None
self._last_send = None

if kwargs:
raise TypeError('Unexpected Arguments: %s' % ', '.join(kwargs))
Expand All @@ -73,10 +75,10 @@ def start(self):
Install the adapter and the wrappers required to intercept requests.
"""
if self._real_send:
if self._last_send:
raise RuntimeError('Mocker has already been started')

self._real_send = requests.Session.send
self._last_send = requests.Session.send

def _fake_get_adapter(session, url):
return self._adapter
Expand All @@ -85,8 +87,18 @@ def _fake_send(session, request, **kwargs):
real_get_adapter = requests.Session.get_adapter
requests.Session.get_adapter = _fake_get_adapter

# NOTE(jamielennox): self._last_send vs _original_send. Whilst it
# seems like here we would use _last_send there is the possibility
# that the user has messed up and is somehow nesting their mockers.
# If we call last_send at this point then we end up calling this
# function again and the outer level adapter ends up winning.
# All we really care about here is that our adapter is in place
# before calling send so we always jump directly to the real
# function so that our most recently patched send call ends up
# putting in the most recent adapter. It feels funny, but it works.

try:
return self._real_send(session, request, **kwargs)
return _original_send(session, request, **kwargs)
except exceptions.NoMockAddress:
if not self._real_http:
raise
Expand All @@ -97,7 +109,7 @@ def _fake_send(session, request, **kwargs):
finally:
requests.Session.get_adapter = real_get_adapter

return self._real_send(session, request, **kwargs)
return _original_send(session, request, **kwargs)

requests.Session.send = _fake_send

Expand All @@ -106,9 +118,9 @@ def stop(self):
This should have no impact if mocking has not been started.
"""
if self._real_send:
requests.Session.send = self._real_send
self._real_send = None
if self._last_send:
requests.Session.send = self._last_send
self._last_send = None

def __getattr__(self, name):
if name in self._PROXY_FUNCS:
Expand Down
70 changes: 70 additions & 0 deletions requests_mock/tests/test_mocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,73 @@ def test_global_case_sensitive(self):

for k, v in query.items():
self.assertEqual([v], m.last_request.qs[k])

def test_nested_mocking(self):
url1 = 'http://url1.com/path1'
url2 = 'http://url2.com/path2'
url3 = 'http://url3.com/path3'

data1 = 'data1'
data2 = 'data2'
data3 = 'data3'

with requests_mock.mock() as m1:

r1 = m1.get(url1, text=data1)

resp1a = requests.get(url1)
self.assertRaises(exceptions.NoMockAddress, requests.get, url2)
self.assertRaises(exceptions.NoMockAddress, requests.get, url3)

self.assertEqual(data1, resp1a.text)

# call count = 3 because there are 3 calls above, url 1-3
self.assertEqual(3, m1.call_count)
self.assertEqual(1, r1.call_count)

with requests_mock.mock() as m2:

r2 = m2.get(url2, text=data2)

self.assertRaises(exceptions.NoMockAddress, requests.get, url1)
resp2a = requests.get(url2)
self.assertRaises(exceptions.NoMockAddress, requests.get, url3)

self.assertEqual(data2, resp2a.text)

with requests_mock.mock() as m3:

r3 = m3.get(url3, text=data3)

self.assertRaises(exceptions.NoMockAddress,
requests.get,
url1)
self.assertRaises(exceptions.NoMockAddress,
requests.get,
url2)
resp3 = requests.get(url3)

self.assertEqual(data3, resp3.text)

self.assertEqual(3, m3.call_count)
self.assertEqual(1, r3.call_count)

resp2b = requests.get(url2)
self.assertRaises(exceptions.NoMockAddress, requests.get, url1)
self.assertEqual(data2, resp2b.text)
self.assertRaises(exceptions.NoMockAddress, requests.get, url3)

self.assertEqual(3, m1.call_count)
self.assertEqual(1, r1.call_count)
self.assertEqual(6, m2.call_count)
self.assertEqual(2, r2.call_count)
self.assertEqual(3, m3.call_count)
self.assertEqual(1, r3.call_count)

resp1b = requests.get(url1)
self.assertEqual(data1, resp1b.text)
self.assertRaises(exceptions.NoMockAddress, requests.get, url2)
self.assertRaises(exceptions.NoMockAddress, requests.get, url3)

self.assertEqual(6, m1.call_count)
self.assertEqual(2, r1.call_count)

0 comments on commit 95dffef

Please sign in to comment.