This document provides a high-level introduction to the Mocket library, explaining its purpose as a socket mock framework for testing network-dependent Python applications. It covers the fundamental concepts, architecture, and capabilities of the system. For installation and setup instructions, see Getting Started. For detailed technical architecture, see Core Architecture. For practical usage examples, see User Guide.
Sources: README.rst1-58
Mocket (pronounced /ˈmɔ.kɛt/) is a socket mock framework that intercepts network operations at the Python standard library level. It works by monkey-patching the socket and ssl modules, allowing complete control over network I/O without requiring modifications to application code or client libraries.
The library's tagline summarizes its scope: "A socket mock framework for all kinds of socket animals, web-clients included - with gevent/asyncio/SSL support."
Mocket serves two primary use cases:
| Use Case | Description | Example |
|---|---|---|
| Low-level framework | Building custom mocks for new protocols or databases | Implementing a mock for a proprietary binary protocol |
| Ready-to-use mock | Testing HTTP/HTTPS/Redis calls from any client library | Mocking requests.get() or redis.StrictRedis() calls |
Sources: README.rst27-45 mocket/__init__.py1-34
Mocket provides the following core capabilities:
socket.socket, ssl.SSLContext, and related functions to redirect all network operations@async_mocketizeSources: README.rst33-91 mocket/__init__.py4-32
The following diagram maps Mocket's conceptual layers to the actual code entities that implement them:
Sources: mocket/__init__.py1-34 mocket/mocket.py1-50 mocket/entry.py1-30 mocket/inject.py1-50
Mocket class)The Mocket class serves as the global state manager and central registry for all mocking operations. It maintains:
_entries: Dictionary mapping (host, port) tuples to lists of MocketEntry objects_requests: List of all captured network requests_truesocket_recording_dir: Directory path for VCR-style recordingKey methods include:
| Method | Purpose | File Reference |
|---|---|---|
enable() | Activates mocking by triggering mocket.inject | mocket/mocket.py |
disable() | Restores original socket/ssl modules | mocket/mocket.py |
register() | Adds a MocketEntry to the registry | mocket/mocket.py |
collect() | Records request data for later inspection | mocket/mocket.py |
last_request() | Returns the most recent captured request | mocket/mocket.py |
get_entry() | Finds matching entry for a given host/port/data | mocket/mocket.py |
Sources: mocket/mocket.py1-100
MocketEntry class)MocketEntry defines mock responses and request matching logic. Subclasses specialize for different protocols:
Sources: mocket/entry.py1-50 mocket/mocks/mockhttp.py1-100 mocket/mocks/mockredis.py1-50
mocket.inject)The mocket.inject module performs the critical monkey-patching that enables Mocket to intercept network operations. When Mocket.enable() is called, this module:
socket.socket, ssl.SSLContext, socket.create_connection, etc.MocketSocket, MocketSSLContext, etc.)Mocket.disable() is calledSources: mocket/inject.py1-100
MocketSocket)MocketSocket is a mock implementation of socket.socket that simulates network I/O using in-memory buffers:
connect((host, port)) queries Mocket.get_entry() for matching entriessendall(data) writes to internal buffer and calls Mocket.collect()recv(bufsize) reads from internal buffer populated by entry responses_true_socket for real I/OSources: mocket/socket.py1-200
Mocket supports three activation patterns:
Sources: README.rst142-155
Sources: README.rst158-173
Sources: mocket/mocket.py1-50
Mocket operates in different modes based on configuration:
Sources: README.rst179-208 README.rst263-286
Mocket is appropriate for:
| Scenario | Reason |
|---|---|
| Testing HTTP/HTTPS clients | Works with any library (requests, httpx, urllib) without client-specific mocking |
| Testing custom protocols | Low-level socket control allows mocking any TCP-based protocol |
| Preventing network access | Strict mode ensures tests don't make real network calls |
| Generating test fixtures | Recording mode captures real API responses for replay |
| Testing async code | Native support for asyncio, gevent, trio via @async_mocketize |
| Redis client testing | Built-in Redis protocol mock with mockredis |
Mocket may not be suitable for:
Sources: README.rst33-45 README.rst74-91
As of version 3.7.0, Mocket's major version follows Python's version numbering to indicate the most recent Python version supported. The current version is 3.14.0, indicating Python 3.14 support.
Note: The last version compatible with Python 2.7 is 3.9.4. No further backports or bugfixes will be made for Python 2.7 except through commercial support.
Sources: README.rst60-64 mocket/__init__.py34
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.