Skip to content

Nested activation of Mocket doesn't cleanly restore patches #326

Description

@ento

Noticed this while trying to upgrade mocket. The project I'm working with has tests that have the @mocketize decorator and also depend on a pytest fixture that enables Mocket in strict mode. Subsequent tests that make actual HTTP requests fail due to Mocket's strict mode being still active, even though the tests don't use Mocket.

Minimal test case

def test_mocketize_twice_nested():
    with Mocketizer(strict_mode=True):
        with Mocketizer(strict_mode=True):
            pass

    url = "http://httpbin.local/ip"
    assert httpx.get(url).status_code == 200

Failing CI run: https://github.com/ento/python-mocket/actions/runs/32912004754/job/98007893977?pr=1

>       raise StrictMocketException(msg)
E       mocket.exceptions.StrictMocketException: Mocket tried to use the real `socket` module while STRICT mode was active.
E       Attempted address: httpbin.local:80
E       Sent data: GET /ip HTTP/1.1
E       Registered entries:

Mocket version: 3.14.3, seems to have regressed since 3.13.3

Prior to 3.13.3, references to true socket objects were kept as module-level variables in mocket.py, while the new approach in inject.py overwrites original values when Mocket gets enabled when it's already active.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions