requests_mock.mock() fails to nest

Bug #1642690 reported by Marius Gedminas
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
requests-mock
Fix Released
Medium
Jamie Lennox

Bug Description

Here's a test case you can run with py.test:

def test_nested_mocks():
    import requests_mock, requests
    with requests_mock.mock() as m1:
        m1.get('/', json=lambda *a: {'m': 1})
        assert requests.get('mock://test/').json() == {'m': 1} # works fine!
        with requests_mock.mock() as m2:
            m2.get('/', json=lambda *a: {'m': 2})
            assert requests.get('mock://test/').json() == {'m': 2} # returns {'m': 1}

In my actual test suite the nesting is hidden by a mixture of pytest fixtures (some function-scoped, some session-scoped) and it took me a while to discover what was happening.

Revision history for this message
Marius Gedminas (mgedmin) wrote :

A patch like this should fix it:

diff --git a/requests_mock/mocker.py b/requests_mock/mocker.py
index bb9e1e5..36326ed 100644
--- a/requests_mock/mocker.py
+++ b/requests_mock/mocker.py
@@ -26,6 +26,9 @@ POST = 'POST'
 PUT = 'PUT'

+original_send = requests.Session.send
+
+
 class MockerCore(object):
     """A wrapper around common mocking functions.

@@ -86,7 +89,7 @@ class MockerCore(object):
             requests.Session.get_adapter = _fake_get_adapter

             try:
- return self._real_send(session, request, **kwargs)
+ return original_send(session, request, **kwargs)
             except exceptions.NoMockAddress:
                 if not self._real_http:
                     raise

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to requests-mock (master)

Fix proposed to branch: master
Review: https://review.openstack.org/399376

Changed in requests-mock:
assignee: nobody → Jamie Lennox (jamielennox)
status: New → In Progress
Revision history for this message
Marius Gedminas (mgedmin) wrote :

Is there anything I can do to help this along?

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to requests-mock (master)

Reviewed: https://review.openstack.org/399376
Committed: https://git.openstack.org/cgit/openstack/requests-mock/commit/?id=95dffefe838cdbde713f4b243d32a18581a7edcc
Submitter: Jenkins
Branch: master

commit 95dffefe838cdbde713f4b243d32a18581a7edcc
Author: Jamie Lennox <email address hidden>
Date: Fri Nov 18 11:41:23 2016 +1100

    Allow for nested mocking

    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

Changed in requests-mock:
status: In Progress → Fix Released
Revision history for this message
Jamie Lennox (jamielennox) wrote :

Fix released as part of v1.2. Please let me know how it goes.

Changed in requests-mock:
importance: Undecided → Medium
Revision history for this message
Marius Gedminas (mgedmin) wrote :

Thank you, requests-mock 1.2.0 works fine for me!

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.