⚙API documentation¶
pytest plugin¶
- pytest_response.plugin.pytest_addoption(parser)[source]¶
Pytest hook for adding cmd-line options.
Adds relevent cmd-line and ini-config options.
- pytest_response.plugin.pytest_configure(config)[source]¶
Pytest hook for setting up
pytest_response.app.Response
Response¶
- class pytest_response.app.BaseMockResponse(status: int, data: bytes, headers: dict = {})[source]¶
Bases:
object
Basic response for mocking requests.
- Parameters
status (int) – Status code of the response.
data (bytes) – Response data.
headers (dict, optional) – Default to {}.
- class pytest_response.app.Response(path: str = 'interceptors', capture: bool = False, remote: bool = False, response: bool = False, log_level: str = 'debug', database: str = 'database.db')[source]¶
Bases:
object
Controlling and configuration application for
pytest-remote-response
- Parameters
path (str, optional) – Path for the interceptors. Defaults to pytest_response.interceptors
capture (bool, optional) – if True captures data and headers in the database. Defaults to False
remote (bool, optional) – if False blocks connection requests. Defaults to False
response (bool, optional) – if True responds with data and headers from the database. Defaults to False
log_level (str, optional) – Log level. Defaults to debug
Examples
>>> from pytest_response import response >>> response.setup_database({{ Path to the database}}) >>> response.post({{ Interceptor }}) >>> ... >>> response.unpost()
- activate(interceptors: Union[str, list])[source]¶
Wrapper to apply the interceptor decorator.
- Parameters
interceptor (str, list) – interceptors to apply
Examples
>>> @response.activate("urllib_quick") >>> def test_urllib(): >>> url = "https://www.python.org" >>> r = urlopen(url) >>> assert r.status == 200
- apply(mock: Optional[Union[str, list]] = None) → bool[source]¶
Activates intercepter module provided in mock otherwise activates all.
- Parameters
mock (str, optional) – Applies the mock.
- property available: List[str]¶
- property capture: bool¶
- configure(remote: bool = False, capture: bool = False, response: bool = False) → None[source]¶
Helper method for configuring interceptors.
- Parameters
remote (bool, optional) – If False blocks connection requests. Defaults to False.
capture (bool, optional) – If True captures data and headers in the database. Defaults to False.
response (bool, optional) – If True responds with data and headers from the database. Defaults to False.
- get(url: str, *args, **kwargs)[source]¶
Wrapper function for
pytest_response.database.ResponseDB.get()
- Parameters
url (str) – URL to be queried.
- Returns
status (int) – Status code
data (bytes) – Response data.
headers (dict) – Response header.
- insert(url: str, response: bytes, headers: str, status: int, *args, **kwargs) → bool[source]¶
Wrapper function for
pytest_response.database.ResponseDB.insert()
- Parameters
url (str) – URL of the dump.
response (bytes) – Data captured.
headers (str) – Headers captured.
status (int) – Status code of the response.
**kwargs (dict) – Any additional parameter to be dumped.
- post(mock: Union[str, list]) → None[source]¶
Registers and applies the mock under the same hood.
Internally uses
Response.register()
followed byResponse.apply()
- Parameters
mock (str) – Registers and applies the mock.
- register(mock: Union[str, list]) → None[source]¶
Registers interceptor modules; applies using
Response.apply()
- Parameters
mock (str, list) – Interceptor; check
Response.available()
for more info.
- registered() → Dict[str, module][source]¶
Returns registered modules.
- Returns
Returns the list of registered interceptors.
- Return type
list of pathlib.Path
- property remote: bool¶
- property response: bool¶
- setup_database(path: str) → bool[source]¶
Method to setup-up database.
- Parameters
path (str) – Path for the database.
- unpost() → bool[source]¶
Unapplied and unregisters mocks under the same hood.
Internally uses
Response.unapply()
followed byResponse.unregister()
Database¶
- class pytest_response.database.ResponseDB(path: str)[source]¶
Bases:
object
Wrapper class for sqlite3
- Parameters
path (str) – Path for the database
Examples
>>> db = ResponseDB("database.db")
- all() → dict[source]¶
Method to return all records in the database.
- Returns
- Return type
Return list of all elements.
- get(url: str, **kwargs) → Tuple[int, bytes, dict][source]¶
Method for getting response and header for a perticular query url. Currently working by locating url only; multi-query to be implemented later.
- Parameters
url (str) – URL to be queried.
- Returns
status (int) – Status code
data (bytes) – Response data.
headers (dict) – Response header.
- index(index: Optional[str] = 'url') → List[str][source]¶
Returns all occurances of the column index.
- Parameters
index (str) – Defaults to url.
- Returns
_occurances – All occurances of the selected column index.
- Return type
list
- insert(url: str, response: bytes, headers: dict, status: Optional[int] = 200, **kwargs) → None[source]¶
Method for dumping url, headers and responses to the database. All additonal kwargs are dumped as well.
- Parameters
url (str) – URL of the dump.
response (bytes) – Data captured.
headers (str) – Headers captured.
status (int) – Status code of the response.
kwargs (dict) – Any additional parameter to be dumped.
- today = '2023-04-27'¶
Interceptors¶
urllib Interceptor module¶
Functions¶
|
Wrapper for |
|
Method to monkey patch the library call with the wrapped one. |
Method to undo all monkey patches. |
Classes¶
|
Class Inheritance Diagram¶
urllib Interceptor module (unstable)¶
Classes¶
|
Provides a method to write the value of the buffer into another var for dumping. |
|
Socket implementation of Pytest-Response |
|
SSLSocket implementation of Pytest-Response |
|
Wrapper for ~http.client.HTTPConnection |
|
Override the default HTTPHandler class with one that uses the ResponseHTTPConnection class to open HTTP URLs. |
|
Provides a way to capture or respond with a saved response. |
|
Override the default ~HTTPSConnection to use ~ResponseSocket |
|
Override the default HTTPSHandler class with one that uses the ResponseHTTPSConnection class to open HTTP URLs. |
Class Inheritance Diagram¶
urllib3 Interceptor module¶
Functions¶
|
Wrapper for |
|
Method to monkey patch the library call with the wrapped one. |
Method to undo all monkey patches. |
Classes¶
|
Class Inheritance Diagram¶
urllib3 Interceptor module (unstable)¶
Classes¶
|
|
|
Class Inheritance Diagram¶
requests Interceptor Module¶
Functions¶
|
Wrapper for |
|
Method to monkey patch the library call with the wrapped one. |
Method to undo all monkey patches. |
Classes¶
|
Class Inheritance Diagram¶
aiohttp Interceptor Module¶
Functions¶
|
Wrapper for |
|
Wrapper for |
|
Method to monkey patch the library call with the wrapped one. |
Method to undo all monkey patches. |
Classes¶
|
Class Inheritance Diagram¶
Exceptions¶
|
Exception raised when Remote connections are blocked. |
|
Exception raised when response is not locally available. |
|
Exception raised when a malformed URL is encountered. |
|
Exception raised when the requested interceptor is not available. |
|
Exception raised when database is not initialized properly. |